Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 217557a

Browse files
committed
test: refactor tests with describe.each
1 parent 35f298b commit 217557a

File tree

6 files changed

+189
-196
lines changed

6 files changed

+189
-196
lines changed

.eslintrc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module.exports = {
22
parser: 'babel-eslint',
3-
extends: ['standard', 'standard-react']
3+
extends: ['standard', 'standard-react'],
4+
globals: {
5+
it: true,
6+
describe: true,
7+
expect: true
8+
}
49
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"build": "rm -rf dist && rollup -c",
3434
"build:copy": "cp package.json dist/ && cp README.md dist/ && cp LICENSE dist/ && cp lib/index.d.ts dist/",
3535
"release": "yarn semantic-release",
36-
"lint": "eslint --fix \"lib/**/*.js\" *.js",
36+
"lint": "eslint --fix {lib,test}/**/*.js *.js",
3737
"test": "jest",
3838
"report-coverage": "cat ./coverage/lcov.info | coveralls"
3939
},

test/List.spec.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11
import React from 'react'
22
import renderer from 'react-test-renderer'
33

4-
import { createTests } from './utils'
54
import { List as List1 } from '../dist/index.cjs'
65
import { List as List2 } from '../dist/index.esm'
76
import List3 from '../dist/List'
87
import List4 from '../lib/List'
98

10-
const runTests = createTests(List1, List2, List3, List4)
9+
const describeEach = describe.each([List1, List2, List3, List4])
1110

12-
runTests(List => {
13-
describe('with render', () => {
14-
const input = [1, 2, 3]
11+
const input = [1, 2, 3]
1512

16-
test('should return primary content from render', () => {
17-
const element = <List items={input} render={n => <div key={n}>{n}</div>} />
18-
const component = renderer.create(element)
19-
expect(component.toJSON()).toMatchSnapshot()
20-
})
13+
describeEach('with render', List => {
14+
it('should return primary content from render', () => {
15+
const element = <List items={input} render={n => <div key={n}>{n}</div>} />
16+
const component = renderer.create(element)
17+
expect(component.toJSON()).toMatchSnapshot()
2118
})
19+
})
2220

23-
describe('with children', () => {
24-
const input = [1, 2, 3]
25-
26-
test('should return primary content from children', () => {
27-
const element = <List items={input}>{n => <div key={n}>{n}</div>}</List>
28-
const component = renderer.create(element)
29-
expect(component.toJSON()).toMatchSnapshot()
30-
})
21+
describeEach('with children', List => {
22+
it('should return primary content from children', () => {
23+
const element = <List items={input}>{n => <div key={n}>{n}</div>}</List>
24+
const component = renderer.create(element)
25+
expect(component.toJSON()).toMatchSnapshot()
3126
})
27+
})
3228

33-
describe('with children and render', () => {
34-
const input = [1, 2, 3]
35-
36-
test('should return primary content from children and ignore render', () => {
37-
const element = (
38-
<List items={input} render={n => <a key={n}>{n}</a>}>
39-
{n => <div key={n}>{n}</div>}
40-
</List>
41-
)
42-
const component = renderer.create(element)
43-
expect(component.toJSON()).toMatchSnapshot()
44-
})
29+
describeEach('with children and render', List => {
30+
it('should return primary content from children and ignore render', () => {
31+
const element = (
32+
<List items={input} render={n => <a key={n}>{n}</a>}>
33+
{n => <div key={n}>{n}</div>}
34+
</List>
35+
)
36+
const component = renderer.create(element)
37+
expect(component.toJSON()).toMatchSnapshot()
4538
})
39+
})
4640

47-
describe('without children and render', () => {
48-
const input = [1, 2, 3]
49-
50-
test('should return null', () => {
51-
const element = <List items={input} />
52-
const component = renderer.create(element)
53-
expect(component.toJSON()).toMatchSnapshot()
54-
})
41+
describeEach('without children and render', List => {
42+
it('should return null', () => {
43+
const element = <List items={input} />
44+
const component = renderer.create(element)
45+
expect(component.toJSON()).toMatchSnapshot()
5546
})
5647
})

test/Show.spec.js

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,78 @@
11
import React from 'react'
22
import renderer from 'react-test-renderer'
33

4-
import { createTests } from './utils'
54
import { Show as Show1 } from '../dist/index.cjs'
65
import { Show as Show2 } from '../dist/index.esm'
76
import Show3 from '../dist/Show'
87
import Show4 from '../lib/Show'
98

10-
const runTests = createTests(Show1, Show2, Show3, Show4)
9+
const describeEach = describe.each([Show1, Show2, Show3, Show4])
10+
const input = <div>render me</div>
1111

12-
runTests(Show => {
13-
const input = <div>render me</div>
14-
15-
describe('with children', () => {
16-
test('should return primary content from children if when equals true', () => {
17-
const element = <Show when>{input}</Show>
18-
const component = renderer.create(element)
19-
expect(component.toJSON()).toMatchSnapshot()
20-
})
12+
describeEach('with children', Show => {
13+
it('should return primary content from children if when equals true', () => {
14+
const element = <Show when>{input}</Show>
15+
const component = renderer.create(element)
16+
expect(component.toJSON()).toMatchSnapshot()
17+
})
2118

22-
test('should return null if when equals false', () => {
23-
const element = <Show when={false}>{input}</Show>
24-
const component = renderer.create(element)
25-
expect(component.toJSON()).toMatchSnapshot()
26-
})
19+
it('should return null if when equals false', () => {
20+
const element = <Show when={false}>{input}</Show>
21+
const component = renderer.create(element)
22+
expect(component.toJSON()).toMatchSnapshot()
2723
})
24+
})
2825

29-
describe('with render', () => {
30-
test('should return primary content from render if when equals true', () => {
31-
const element = <Show when render={() => input} />
32-
const component = renderer.create(element)
33-
expect(component.toJSON()).toMatchSnapshot()
34-
})
26+
describeEach('with render', Show => {
27+
it('should return primary content from render if when equals true', () => {
28+
const element = <Show when render={() => input} />
29+
const component = renderer.create(element)
30+
expect(component.toJSON()).toMatchSnapshot()
31+
})
3532

36-
test('should return null if when equals false', () => {
37-
const element = <Show when={false} render={() => input} />
38-
const component = renderer.create(element)
39-
expect(component.toJSON()).toMatchSnapshot()
40-
})
33+
it('should return null if when equals false', () => {
34+
const element = <Show when={false} render={() => input} />
35+
const component = renderer.create(element)
36+
expect(component.toJSON()).toMatchSnapshot()
37+
})
4138

42-
test('should not evaluate render if when is undefined', () => {
43-
const obj = undefined
44-
const element = <Show when={!!obj} render={() => <div>{obj.label}</div>} />
45-
const component = renderer.create(element)
46-
expect(component.toJSON()).toMatchSnapshot()
47-
})
39+
it('should not evaluate render if when is undefined', () => {
40+
const obj = undefined
41+
const element = <Show when={!!obj} render={() => <div>{obj.label}</div>} />
42+
const component = renderer.create(element)
43+
expect(component.toJSON()).toMatchSnapshot()
4844
})
45+
})
4946

50-
describe('with children and render', () => {
51-
const renderInput = <div>render</div>
52-
const childrenInput = <div>children</div>
47+
describeEach('with children and render', Show => {
48+
const renderInput = <div>render</div>
49+
const childrenInput = <div>children</div>
5350

54-
test('should return primary content from children and ignore render if when equals true', () => {
55-
const element = (
56-
<Show when render={() => renderInput}>
57-
{childrenInput}
58-
</Show>
59-
)
60-
const component = renderer.create(element)
61-
expect(component.toJSON()).toMatchSnapshot()
62-
})
51+
it('should return primary content from children and ignore render if when equals true', () => {
52+
const element = (
53+
<Show when render={() => renderInput}>
54+
{childrenInput}
55+
</Show>
56+
)
57+
const component = renderer.create(element)
58+
expect(component.toJSON()).toMatchSnapshot()
59+
})
6360

64-
test('should return null if when equals false', () => {
65-
const element = (
66-
<Show when={false} render={() => renderInput}>
67-
{childrenInput}
68-
</Show>
69-
)
70-
const component = renderer.create(element)
71-
expect(component.toJSON()).toMatchSnapshot()
72-
})
61+
it('should return null if when equals false', () => {
62+
const element = (
63+
<Show when={false} render={() => renderInput}>
64+
{childrenInput}
65+
</Show>
66+
)
67+
const component = renderer.create(element)
68+
expect(component.toJSON()).toMatchSnapshot()
7369
})
70+
})
7471

75-
describe('without children and render', () => {
76-
test('should return null', () => {
77-
const element = <Show when />
78-
const component = renderer.create(element)
79-
expect(component.toJSON()).toMatchSnapshot()
80-
})
72+
describeEach('without children and render', Show => {
73+
it('should return null', () => {
74+
const element = <Show when />
75+
const component = renderer.create(element)
76+
expect(component.toJSON()).toMatchSnapshot()
8177
})
8278
})

0 commit comments

Comments
 (0)