Skip to content

Commit 106ca7e

Browse files
authored
Merge pull request #2 from m-avagyan/unit-tests-setup
Unit tests setup
2 parents 0e8c3d3 + 9eab97c commit 106ca7e

File tree

11 files changed

+2051
-31
lines changed

11 files changed

+2051
-31
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Build Project
2121
- `yarn clean` - clean the `dist/` directory
2222
- `yarn build` - we can see the output in `dist/` directory
2323

24+
Run Unit Tests
25+
26+
- `yarn test` - run all test cases
27+
- `yarn test:watch` - run all test cases in watch mode
28+
- `yarn test:coverage` - run all test cases and collect coverage for each tested files
29+
2430
Analyze
2531

2632
- `yarn analyze` - build project to `/dist` and analyze your bundle size in [localhost:3006](http://localhost:3006)

jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
coverageProvider: 'v8',
3+
testEnvironment: 'jsdom',
4+
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/dist/'],
5+
transform: {
6+
'^.+\\.(ts|tsx)$': '@swc/jest',
7+
'^.+\\.(js|jsx)$': '@swc/jest',
8+
},
9+
};

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@
3434
"watch": "yarn webpack serve --progress --mode production",
3535
"lint": "eslint src --ext .js,.ts,.tsx --ignore-pattern '!.*.js'",
3636
"format": "yarn lint --fix",
37+
"test": "jest",
38+
"test:watch": "jest --watchAll=true",
39+
"test:coverage": "rm -rf coverage && jest --coverage && open coverage/lcov-report/index.html",
3740
"postinstall": "husky install"
3841
},
3942
"dependencies": {
43+
"classcraft": "^1.0.1",
4044
"i18next": "^21.9.2",
4145
"i18next-browser-languagedetector": "^6.1.5",
4246
"i18next-resources-to-backend": "^1.0.0",
4347
"react": "^18.2.0",
4448
"react-dom": "^18.2.0",
4549
"react-router-dom": "5.3.3",
46-
"styled-components": "^5.3.6"
50+
"styled-components": "^5.3.6",
51+
"ublob": "^1.0.1"
4752
},
4853
"devDependencies": {
4954
"@babel/core": "^7.20.12",
@@ -53,7 +58,12 @@
5358
"@commitlint/cli": "^17.4.4",
5459
"@commitlint/config-conventional": "^17.4.4",
5560
"@svgr/webpack": "^6.5.1",
61+
"@swc/core": "^1.4.2",
62+
"@swc/jest": "^0.2.36",
63+
"@testing-library/jest-dom": "^6.4.2",
64+
"@testing-library/react": "^14.2.1",
5665
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
66+
"@types/jest": "^29.5.12",
5767
"@types/node": "^18.11.18",
5868
"@types/react": "^18.0.27",
5969
"@types/react-dom": "^18.0.10",
@@ -76,6 +86,8 @@
7686
"file-loader": "^6.2.0",
7787
"html-webpack-plugin": "^5.5.0",
7888
"husky": "^8.0.0",
89+
"jest": "^29.7.0",
90+
"jest-environment-jsdom": "^29.7.0",
7991
"mini-css-extract-plugin": "^2.7.2",
8092
"postcss": "^8.4.31",
8193
"postcss-loader": "^7.0.2",

src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { useTranslation } from 'react-i18next';
33
import { Route, Switch } from 'react-router';
44
import { Link } from 'react-router-dom';
55

6+
import Loader from 'components/loader';
7+
68
import Paths from './constants/path';
79
import routes from './routes';
810

@@ -18,7 +20,7 @@ function App() {
1820
<Switch>
1921
{routes.map(({ name, path, Component }, index) => (
2022
<Route key={name + index} path={path} exact>
21-
<Suspense fallback={null}>
23+
<Suspense fallback={<Loader />}>
2224
<Component />
2325
</Suspense>
2426
</Route>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import classcraft from 'classcraft';
2+
3+
describe('Test classcraft joining.', () => {
4+
it('Should merge all arguments if the type of arg is string.', () => {
5+
const result = classcraft('test-1', 'test-2', 'test-3');
6+
const expected = 'test-1 test-2 test-3';
7+
8+
expect(result).toEqual(expected);
9+
});
10+
11+
it('Should merge only truthy valued properties.', () => {
12+
const result = classcraft({ 'test-1': true }, { 'test-2': false, 'test-3': true }, { 'test-4': undefined });
13+
const expected = 'test-1 test-3';
14+
15+
expect(result).toEqual(expected);
16+
});
17+
});

src/__tests__/ublub/file-type.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getFileType } from 'ublob';
2+
3+
describe('Get file type tests.', () => {
4+
it('Should throw exception error if argument is not string.', () => {
5+
try {
6+
// @ts-ignore
7+
const result = getFileType(1);
8+
9+
expect(result).toThrowError('The parameter must be a blob string.');
10+
} catch {}
11+
});
12+
13+
it('Should throw exception error if file type not supported.', () => {
14+
try {
15+
const result = getFileType('unsupported-file');
16+
17+
expect(result).toThrowError('The file type is not supported.');
18+
} catch {}
19+
});
20+
21+
it('Should return file type.', () => {
22+
const result = getFileType('blob-start:application/pdf;string:');
23+
24+
expect(result).toEqual('application/pdf');
25+
});
26+
});

src/components/loader/Loader.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/loader/index.styled.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import styled from 'styled-components';
2+
3+
export const Loader = styled.div`
4+
font-size: 20px;
5+
line-height: 24px;
6+
font-weight: bold;
7+
text-align: center;
8+
color: #000;
9+
`;

src/components/loader/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
3+
import * as Styled from './index.styled';
4+
5+
function Loader() {
6+
return <Styled.Loader>Loading...</Styled.Loader>;
7+
}
8+
9+
export default Loader;

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { lazy, Suspense } from 'react';
22
import { createRoot } from 'react-dom/client';
33
import { BrowserRouter as Router } from 'react-router-dom';
44

5-
import Loader from './components/loader/Loader';
5+
import Loader from './components/loader';
66
import './i18next';
77

88
const root = document.getElementById('root');

0 commit comments

Comments
 (0)