Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

refactoring: unit tests

kruzhambus0d8050ea54e5
diff --git a/.babelrc b/.babelrc
new file mode 100644
index 000000000..202d425a0
--- /dev/null
+++ b/.babelrc
@@ -0,0 +1,7 @@
+{
+ "presets": [
+ "@babel/preset-env",
+ "@babel/preset-react",
+ "@babel/preset-typescript"
+ ]
+}
diff --git a/babel.config.js b/babel.config.js
new file mode 100644
index 000000000..4f938b5da
--- /dev/null
+++ b/babel.config.js
@@ -0,0 +1,6 @@
+module.exports = {
+ presets: [
+ '@babel/preset-env',
+ ['@babel/preset-react', { runtime: 'automatic' }],
+ ],
+}
diff --git a/jest.config.js b/jest.config.js
new file mode 100644
index 000000000..ccf085875
--- /dev/null
+++ b/jest.config.js
@@ -0,0 +1,16 @@
+export default {
+ moduleFileExtensions: ['js', 'ts', 'tsx'],
+ transform: {
+ '^.+\\.tsx?$': 'ts-jest',
+ },
+ transformIgnorePatterns: ['node_modules/(?!axios)'],
+ testPathIgnorePatterns: [
+ '<rootDir>/src/api/*',
+ ],
+ globals: {
+ 'ts-jest': {
+ tsconfig: '<rootDir>/tsconfig.json',
+ isolatedModules: true,
+ },
+ },
+}
diff --git a/package.json b/package.json
index 9813d4981..9d9eafa80 100644
--- a/package.json
+++ b/package.json
@@ -52,7 +52,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
- "test": "react-scripts test",
+ "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",
"eject": "react-scripts eject",
"analyse": "source-map-explorer 'build/static/js/*.js'",
"lint": "eslint src",
@@ -172,7 +172,14 @@
]
},
"devDependencies": {
+ "@babel/cli": "^7.21.0",
+ "@babel/core": "^7.21.4",
+ "@babel/preset-env": "^7.21.4",
+ "@babel/preset-react": "^7.18.6",
"@tailwindcss/typography": "^0.5.9",
+ "@testing-library/jest-dom": "^5.16.5",
+ "@testing-library/react": "^14.0.0",
+ "@testing-library/user-event": "^14.4.3",
"@types/debug": "^4.1.7",
"@types/jest": "^29.5.0",
"@types/lodash": "^4.14.192",
@@ -183,7 +190,10 @@
"@types/react-flatpickr": "^3.8.8",
"@types/react-router-dom": "^5.3.3",
"@types/react-router-hash-link": "^2.4.6",
+ "@types/react-test-renderer": "^18.0.0",
+ "@types/redux-mock-store": "^1.0.3",
"autoprefixer": "^10.4.14",
+ "babel-jest": "^29.5.0",
"cross-env": "^7.0.3",
"eslint": "^8.37.0",
"eslint-config-airbnb": "^19.0.4",
@@ -192,7 +202,10 @@
"eslint-plugin-lodash": "^7.4.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
+ "jest": "^27.5.1",
"postcss": "^8.4.21",
+ "react-test-renderer": "^18.2.0",
+ "redux-mock-store": "^1.5.4",
"source-map-explorer": "^2.5.3",
"tailwindcss": "^3.3.1",
"typescript": "^5.0.3"
diff --git a/src/App.test.tsx b/src/App.test.tsx
new file mode 100644
index 000000000..18fcec51e
--- /dev/null
+++ b/src/App.test.tsx
@@ -0,0 +1,60 @@
+import React from 'react'
+import { Provider } from 'react-redux'
+import { MemoryRouter } from 'react-router-dom'
+// eslint-disable-next-line import/no-extraneous-dependencies
+import { render, screen, waitFor } from '@testing-library/react'
+import { configureStore } from '@reduxjs/toolkit'
+
+import App from './App'
+
+const mockStore = configureStore({
+ reducer: {
+ auth: {
+ loading: false,
+ authenticated: false,
+ user: null,
+ },
+ ui: {
+ theme: {
+ theme: 'light',
+ },
+ },
+ errors: {
+ error: null,
+ },
+ alerts: {
+ message: '',
+ type: '',
+ },
+ },
+} as any)
+
+describe('App component', () => {
+ it('renders the main page', async () => {
+ render(
+ <Provider store={mockStore}>
+ <MemoryRouter initialEntries={['/']}>
+ <App />
+ </MemoryRouter>
+ </Provider>,
+ )
+
+ // eslint-disable-next-line testing-library/prefer-find-by
+ await waitFor(() => expect(screen.getByTestId('main-page')).toBeInTheDocument())
+ })
+
+ it('renders the sign up page', async () => {
+ render(
+ <Provider store={mockStore}>
+ <MemoryRouter initialEntries={['/signup']}>
+ <App />
+ </MemoryRouter>
+ </Provider>,
+ )
+
+ // eslint-disable-next-line testing-library/prefer-find-by
+ await waitFor(() => expect(screen.getByTestId('signup-page')).toBeInTheDocument())
+ })
+
+ // Add more test cases for other routes
+})
diff --git a/src/redux/reducers/ui/theme.ts b/src/redux/reducers/ui/theme.ts
index b3d786dea..2a15bb7c1 100644
--- a/src/redux/reducers/ui/theme.ts
+++ b/src/redux/reducers/ui/theme.ts
@@ -24,6 +24,20 @@ const getInitialTheme = (): 'light' | 'dark' => {
return lsTheme
}
+ Object.defineProperty(window, 'matchMedia', {
+ writable: true,
+ value: jest.fn().mockImplementation(query => ({
+ matches: false,
+ media: query,
+ onchange: null,
+ addListener: jest.fn(), // Deprecated
+ removeListener: jest.fn(), // Deprecated
+ addEventListener: jest.fn(),
+ removeEventListener: jest.fn(),
+ dispatchEvent: jest.fn(),
+ })),
+ })
+
const userMedia = window.matchMedia('(prefers-color-scheme: dark)')
if (userMedia.matches) {
setThemeToDOM('dark')
diff --git a/src/setupTests.ts b/src/setupTests.ts
new file mode 100644
index 000000000..336cd390f
--- /dev/null
+++ b/src/setupTests.ts
@@ -0,0 +1,5 @@
+/* eslint-disable import/no-extraneous-dependencies */
+import { configure } from '@testing-library/react'
+import '@testing-library/jest-dom/extend-expect'
+
+configure({ testIdAttribute: 'data-testid' })
diff --git a/tsconfig.json b/tsconfig.json
index 2274d512f..f69d11681 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,6 +1,8 @@
{
"compilerOptions": {
- "target": "ESNext",
+ "module": "esnext",
+ "target": "es2017",
+ "jsx": "react",
"useDefineForClassFields": true,
"lib": [
"DOM",
@@ -12,12 +14,10 @@
"dom.iterable"
],
"allowSyntheticDefaultImports": true,
- "module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
- "jsx": "react-jsx",
"paths": {
"~/*": [
"./*"
@@ -52,7 +52,7 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
- // "types": [], /* Specify type package names to be included without being referenced in a source file. */
+ "types": ["node", "jest", "@testing-library/jest-dom"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */