Skip to content

Commit 30dacb7

Browse files
authored
Merge pull request #3 from openscript/develop
Add unit tests
2 parents 0ca0156 + afd13c3 commit 30dacb7

17 files changed

+2047
-274
lines changed

.codeclimate.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
plugins:
2+
eslint:
3+
enabled: true
4+
channel: "eslint-6"
5+
editorconfig:
6+
enabled: false
7+
fixme:
8+
enabled: true
9+
markdownlint:
10+
enabled: false
11+
12+
exclude_patterns:
13+
- "dist/"
14+
- "**/node_modules/"
15+
- "**/spec/"
16+
- "**/test/"
17+
- "**/tests/"
18+
- "**/vendor/"
19+
- "**/*.d.ts"
20+
- "**/*.stories.*"
21+
- "**/*.test.*"
22+
- "*.js"

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ cache:
1010
install:
1111
- yarn install --frozen-lockfile
1212

13+
before_script:
14+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
15+
- chmod +x ./cc-test-reporter
16+
1317
script:
18+
- yarn test
19+
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then ./cc-test-reporter format-coverage -t lcov coverage/lcov.info -o coverage/codeclimate.json; fi'
1420
- yarn build:rollup
1521
- yarn build:storybook
1622

23+
after_script:
24+
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then ./cc-test-reporter upload-coverage; fi'
25+
1726
deploy:
1827
- provider: pages
1928
local_dir: storybook-static

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Add the package with the package manager of choice to your project:
1212

1313
### TypeScript
1414
```
15-
import { DSVImport, ColumnsType, TablePreview, TextareaInput } from 'react-dsv-import';
15+
import { DSVImport, ColumnsType } from 'react-dsv-import';
1616
1717
type BasicType = { forename: string; surname: string; email: string };
1818
@@ -23,14 +23,14 @@ const columns: ColumnsType<BasicType> = [
2323
];
2424
2525
<DSVImport<BasicType> columns={columns}>
26-
<TextareaInput />
27-
<TablePreview />
26+
<DSVImport.TextareaInput />
27+
<DSVImport.TablePreview />
2828
</DSVImport>
2929
```
3030

3131
### JavaScript
3232
```
33-
import { DSVImport, TablePreview, TextareaInput } from 'react-dsv-import';
33+
import { DSVImport } from 'react-dsv-import';
3434
3535
const columns = [
3636
{ key: 'forename', label: 'Forename' },
@@ -39,8 +39,8 @@ const columns = [
3939
];
4040
4141
<DSVImport columns={columns}>
42-
<TextareaInput />
43-
<TablePreview />
42+
<DSVImport.TextareaInput />
43+
<DSVImport.TablePreview />
4444
</DSVImport>
4545
```
4646

@@ -53,6 +53,7 @@ The most important features of this component are:
5353
- ✅ Type definitions and type safety
5454
- ✅ DSV format detection
5555
- ✅ Fully compositable
56+
- ✅ Automatic testing with >90% coverage
5657
- ❌ Input validation
5758
-[Material UI](https://material-ui.com/) integration
5859
-[ant.design](https://ant.design/) integration

jest.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
roots: ['./src'],
3+
setupFilesAfterEnv: ['./jest.setup.ts'],
4+
moduleFileExtensions: ['ts', 'tsx', 'js'],
5+
testPathIgnorePatterns: ['node_modules/'],
6+
transform: {
7+
'^.+\\.tsx?$': 'ts-jest'
8+
},
9+
testMatch: ['**/*.test.(ts|tsx)'],
10+
collectCoverageFrom: ['**/!(*.stories).(ts|tsx)'],
11+
moduleNameMapper: {}
12+
};

jest.setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"react"
88
],
99
"homepage": "https://openscript.github.io/react-dsv-import/",
10-
"version": "0.1.1",
10+
"version": "0.1.2",
1111
"main": "dist/index.js",
1212
"module": "dist/es/index.js",
1313
"types": "dist/index.d.ts",
@@ -23,6 +23,9 @@
2323
"@storybook/addons": "^5.3.18",
2424
"@storybook/preset-typescript": "^3.0.0",
2525
"@storybook/react": "^5.3.18",
26+
"@testing-library/jest-dom": "^5.5.0",
27+
"@testing-library/react": "^10.0.2",
28+
"@types/jest": "^25.2.1",
2629
"@types/node": "^13.11.1",
2730
"@types/react": "^16.9.34",
2831
"@types/react-dom": "^16.9.6",
@@ -34,18 +37,21 @@
3437
"eslint-config-prettier": "^6.10.1",
3538
"eslint-plugin-prettier": "^3.1.3",
3639
"eslint-plugin-react": "^7.19.0",
40+
"jest": "^25.3.0",
3741
"prettier": "^2.0.4",
3842
"react-is": "^16.13.1",
3943
"rollup": "^2.6.1",
44+
"ts-jest": "^25.4.0",
4045
"ts-node": "^8.8.2",
4146
"tslib": "^1.11.1",
4247
"typescript": "^3.8.3"
4348
},
4449
"scripts": {
4550
"build": "yarn build:rollup",
4651
"dev": "yarn dev:storybook",
47-
"test": "ts-node test/test.ts",
52+
"test": "jest --coverage",
4853
"pretest": "yarn build",
54+
"dev:test": "jest --watchAll --coverage",
4955
"dev:rollup": "rollup -c --w",
5056
"dev:storybook": "start-storybook -p 6006",
5157
"build:rollup": "rollup -c",

src/DSVImport.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { DSVImport, ColumnsType, TablePreview, TextareaInput } from './';
2+
import { DSVImport, ColumnsType } from './';
33
import { action } from '@storybook/addon-actions';
44

55
export default { title: 'Usage' };
@@ -17,8 +17,8 @@ const onChangeAction = action('Parsed value has changed');
1717
export const BasicUsage = () => {
1818
return (
1919
<DSVImport<BasicType> columns={columns} onChange={onChangeAction}>
20-
<TextareaInput />
21-
<TablePreview />
20+
<DSVImport.TextareaInput />
21+
<DSVImport.TablePreview />
2222
</DSVImport>
2323
);
2424
};

src/DSVImport.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ColumnsType } from './models/column';
2+
import { DSVImport } from '.';
3+
import React from 'react';
4+
import { render, fireEvent } from '@testing-library/react';
5+
6+
describe('DSVImport', () => {
7+
type TestType = { forename: string; surname: string; email: string };
8+
9+
const columns: ColumnsType<TestType> = [
10+
{ key: 'forename', label: 'Forename' },
11+
{ key: 'surname', label: 'Surname' },
12+
{ key: 'email', label: 'Email' }
13+
];
14+
15+
const onChangeMock = jest.fn();
16+
17+
const renderComponent = () => {
18+
return render(
19+
<DSVImport<TestType> columns={columns} onChange={onChangeMock}>
20+
<DSVImport.TextareaInput />
21+
<DSVImport.TablePreview />
22+
</DSVImport>
23+
);
24+
};
25+
26+
it('should invoke the onChange callback on context change', () => {
27+
const { container } = renderComponent();
28+
const textarea = container.querySelector('textarea');
29+
30+
if (textarea) {
31+
fireEvent.change(textarea, { target: { value: 'Max' } });
32+
}
33+
34+
expect(onChangeMock).toBeCalledTimes(2);
35+
expect(onChangeMock).toBeCalledWith([{ email: undefined, forename: 'Max', surname: undefined }]);
36+
});
37+
});

src/DSVImport.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getDSVImportContext } from './features/context';
44
import { createSimpleParserMiddleware } from './middlewares/simpleParserMiddleware';
55
import { State } from './models/state';
66

7-
interface Props<T> {
7+
export interface Props<T> {
88
onChange?: (value: T[]) => void;
99
columns: ColumnsType<T>;
1010
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { render, fireEvent } from '@testing-library/react';
2+
import { TextareaInput } from './TextareaInput';
3+
import React from 'react';
4+
import { ColumnsType } from '../../models/column';
5+
import { getDSVImportContext } from '../../features/context';
6+
import { State } from '../../models/state';
7+
8+
describe('TextareaInput', () => {
9+
type TestType = {};
10+
const columns: ColumnsType<TestType> = [];
11+
const state: State<TestType> = { columns };
12+
const Context = getDSVImportContext<TestType>();
13+
14+
const dispatchMock = jest.fn(() => state);
15+
16+
const renderComponent = () => {
17+
return render(
18+
<Context.Provider value={[state, dispatchMock]}>
19+
<TextareaInput />
20+
</Context.Provider>
21+
);
22+
};
23+
24+
it('should dispatch the setRaw action on input change', () => {
25+
const { container } = renderComponent();
26+
const textarea = container.querySelector('textarea');
27+
28+
if (textarea) {
29+
fireEvent.change(textarea, { target: { value: 'Change' } });
30+
}
31+
32+
expect(dispatchMock).toBeCalledTimes(1);
33+
expect(dispatchMock).toBeCalledWith({ raw: 'Change', type: 'setRaw' });
34+
});
35+
});

0 commit comments

Comments
 (0)