From 217557a8ccae28cf7e655bdc2d9c25ad8fe9306b Mon Sep 17 00:00:00 2001 From: Christian Svenkerud Date: Tue, 7 Apr 2020 15:19:38 +0200 Subject: [PATCH] test: refactor tests with describe.each --- .eslintrc.js | 7 +- package.json | 2 +- test/List.spec.js | 67 +++++++--------- test/Show.spec.js | 116 +++++++++++++-------------- test/Switch.spec.js | 189 +++++++++++++++++++++++--------------------- test/utils/index.js | 4 - 6 files changed, 189 insertions(+), 196 deletions(-) delete mode 100644 test/utils/index.js diff --git a/.eslintrc.js b/.eslintrc.js index ad6986b5..9fa25a13 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,4 +1,9 @@ module.exports = { parser: 'babel-eslint', - extends: ['standard', 'standard-react'] + extends: ['standard', 'standard-react'], + globals: { + it: true, + describe: true, + expect: true + } } diff --git a/package.json b/package.json index b3639516..2972cb61 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "build": "rm -rf dist && rollup -c", "build:copy": "cp package.json dist/ && cp README.md dist/ && cp LICENSE dist/ && cp lib/index.d.ts dist/", "release": "yarn semantic-release", - "lint": "eslint --fix \"lib/**/*.js\" *.js", + "lint": "eslint --fix {lib,test}/**/*.js *.js", "test": "jest", "report-coverage": "cat ./coverage/lcov.info | coveralls" }, diff --git a/test/List.spec.js b/test/List.spec.js index f5ad88a7..9a012db2 100644 --- a/test/List.spec.js +++ b/test/List.spec.js @@ -1,56 +1,47 @@ import React from 'react' import renderer from 'react-test-renderer' -import { createTests } from './utils' import { List as List1 } from '../dist/index.cjs' import { List as List2 } from '../dist/index.esm' import List3 from '../dist/List' import List4 from '../lib/List' -const runTests = createTests(List1, List2, List3, List4) +const describeEach = describe.each([List1, List2, List3, List4]) -runTests(List => { - describe('with render', () => { - const input = [1, 2, 3] +const input = [1, 2, 3] - test('should return primary content from render', () => { - const element =
{n}
} /> - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('with render', List => { + it('should return primary content from render', () => { + const element =
{n}
} /> + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) +}) - describe('with children', () => { - const input = [1, 2, 3] - - test('should return primary content from children', () => { - const element = {n =>
{n}
}
- const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('with children', List => { + it('should return primary content from children', () => { + const element = {n =>
{n}
}
+ const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) +}) - describe('with children and render', () => { - const input = [1, 2, 3] - - test('should return primary content from children and ignore render', () => { - const element = ( - {n}}> - {n =>
{n}
} -
- ) - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('with children and render', List => { + it('should return primary content from children and ignore render', () => { + const element = ( + {n}}> + {n =>
{n}
} +
+ ) + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) +}) - describe('without children and render', () => { - const input = [1, 2, 3] - - test('should return null', () => { - const element = - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('without children and render', List => { + it('should return null', () => { + const element = + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) }) diff --git a/test/Show.spec.js b/test/Show.spec.js index d50d93e0..be4f1333 100644 --- a/test/Show.spec.js +++ b/test/Show.spec.js @@ -1,82 +1,78 @@ import React from 'react' import renderer from 'react-test-renderer' -import { createTests } from './utils' import { Show as Show1 } from '../dist/index.cjs' import { Show as Show2 } from '../dist/index.esm' import Show3 from '../dist/Show' import Show4 from '../lib/Show' -const runTests = createTests(Show1, Show2, Show3, Show4) +const describeEach = describe.each([Show1, Show2, Show3, Show4]) +const input =
render me
-runTests(Show => { - const input =
render me
- - describe('with children', () => { - test('should return primary content from children if when equals true', () => { - const element = {input} - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('with children', Show => { + it('should return primary content from children if when equals true', () => { + const element = {input} + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() + }) - test('should return null if when equals false', () => { - const element = {input} - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return null if when equals false', () => { + const element = {input} + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) +}) - describe('with render', () => { - test('should return primary content from render if when equals true', () => { - const element = input} /> - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('with render', Show => { + it('should return primary content from render if when equals true', () => { + const element = input} /> + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() + }) - test('should return null if when equals false', () => { - const element = input} /> - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return null if when equals false', () => { + const element = input} /> + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() + }) - test('should not evaluate render if when is undefined', () => { - const obj = undefined - const element =
{obj.label}
} /> - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should not evaluate render if when is undefined', () => { + const obj = undefined + const element =
{obj.label}
} /> + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) +}) - describe('with children and render', () => { - const renderInput =
render
- const childrenInput =
children
+describeEach('with children and render', Show => { + const renderInput =
render
+ const childrenInput =
children
- test('should return primary content from children and ignore render if when equals true', () => { - const element = ( - renderInput}> - {childrenInput} - - ) - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return primary content from children and ignore render if when equals true', () => { + const element = ( + renderInput}> + {childrenInput} + + ) + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() + }) - test('should return null if when equals false', () => { - const element = ( - renderInput}> - {childrenInput} - - ) - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return null if when equals false', () => { + const element = ( + renderInput}> + {childrenInput} + + ) + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) +}) - describe('without children and render', () => { - test('should return null', () => { - const element = - const component = renderer.create(element) - expect(component.toJSON()).toMatchSnapshot() - }) +describeEach('without children and render', Show => { + it('should return null', () => { + const element = + const component = renderer.create(element) + expect(component.toJSON()).toMatchSnapshot() }) }) diff --git a/test/Switch.spec.js b/test/Switch.spec.js index 6764b86d..2d080e7f 100644 --- a/test/Switch.spec.js +++ b/test/Switch.spec.js @@ -1,108 +1,113 @@ import React from 'react' import renderer from 'react-test-renderer' -import { createTests } from './utils' import { Switch as Switch1 } from '../dist/index.cjs' import { Switch as Switch2 } from '../dist/index.esm' import Switch3 from '../dist/Switch' import Switch4 from '../lib/Switch' -const runTests = createTests(Switch1, Switch2, Switch3, Switch4) - -runTests(Switch => { - const createElement = (value, values, withDefault = true) => ( - - {values.map((item, index) => ( - -
{`case ${index}`}
-
- ))} - {withDefault ? ( - -
default
-
- ) : null} -
- ) - - describe('without default case', () => { - test('should return content from first case', () => { - const component = renderer.create(createElement(0, [0, 1, 2], false)) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content from second case', () => { - const component = renderer.create(createElement(1, [0, 1, 2], false)) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content from third case', () => { - const component = renderer.create(createElement(2, [0, 1, 2], false)) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return null', () => { - const component = renderer.create(createElement(3, [0, 1, 2], false)) - expect(component.toJSON()).toMatchSnapshot() - }) +const describeEach = describe.each([Switch1, Switch2, Switch3, Switch4]) + +const makeCreateElement = (Switch) => (value, values, withDefault = true) => ( + + {values.map((item, index) => ( + +
{`case ${index}`}
+
+ ))} + {withDefault ? ( + +
default
+
+ ) : null} +
+) + +describeEach('without default case', Switch => { + const createElement = makeCreateElement(Switch) + + it('should return content from first case', () => { + const component = renderer.create(createElement(0, [0, 1, 2], false)) + expect(component.toJSON()).toMatchSnapshot() }) - describe('with default case', () => { - test('should return content from first case', () => { - const component = renderer.create(createElement(0, [0, 1, 2])) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content from second case', () => { - const component = renderer.create(createElement(1, [0, 1, 2])) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content from third case', () => { - const component = renderer.create(createElement(2, [0, 1, 2])) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return null', () => { - const component = renderer.create(createElement(3, [0, 1, 2])) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return content from second case', () => { + const component = renderer.create(createElement(1, [0, 1, 2], false)) + expect(component.toJSON()).toMatchSnapshot() }) - describe('input tests', () => { - test('should return content when value is number', () => { - const component = renderer.create(createElement(3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content when value is string', () => { - const component = renderer.create( - createElement('salsa', [0, 1, 2, 3, 'salsa', 5, 6, 7, 8]) - ) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content when value is boolean (true)', () => { - const component = renderer.create( - createElement(true, [0, 1, 2, 3, 4, 5, true, 7, 8, 9]) - ) - expect(component.toJSON()).toMatchSnapshot() - }) - - test('should return content when value is boolean (false)', () => { - const component = renderer.create( - createElement(false, [0, 1, 2, 3, 4, 5, false, 7, 8, 9]) - ) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return content from third case', () => { + const component = renderer.create(createElement(2, [0, 1, 2], false)) + expect(component.toJSON()).toMatchSnapshot() }) - describe('misc tests', () => { - test('should return content from first occurence when multiple matches', () => { - const component = renderer.create( - createElement(true, [0, 1, 2, true, 4, 5, true, 7, 8, 9]) - ) - expect(component.toJSON()).toMatchSnapshot() - }) + it('should return null', () => { + const component = renderer.create(createElement(3, [0, 1, 2], false)) + expect(component.toJSON()).toMatchSnapshot() + }) +}) + +describeEach('with default case', Switch => { + const createElement = makeCreateElement(Switch) + + it('should return content from first case', () => { + const component = renderer.create(createElement(0, [0, 1, 2])) + expect(component.toJSON()).toMatchSnapshot() + }) + + it('should return content from second case', () => { + const component = renderer.create(createElement(1, [0, 1, 2])) + expect(component.toJSON()).toMatchSnapshot() + }) + + it('should return content from third case', () => { + const component = renderer.create(createElement(2, [0, 1, 2])) + expect(component.toJSON()).toMatchSnapshot() + }) + + it('should return null', () => { + const component = renderer.create(createElement(3, [0, 1, 2])) + expect(component.toJSON()).toMatchSnapshot() + }) +}) + +describeEach('input tests', Switch => { + const createElement = makeCreateElement(Switch) + + it('should return content when value is number', () => { + const component = renderer.create(createElement(3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) + expect(component.toJSON()).toMatchSnapshot() + }) + + it('should return content when value is string', () => { + const component = renderer.create( + createElement('salsa', [0, 1, 2, 3, 'salsa', 5, 6, 7, 8]) + ) + expect(component.toJSON()).toMatchSnapshot() + }) + + it('should return content when value is boolean (true)', () => { + const component = renderer.create( + createElement(true, [0, 1, 2, 3, 4, 5, true, 7, 8, 9]) + ) + expect(component.toJSON()).toMatchSnapshot() + }) + + it('should return content when value is boolean (false)', () => { + const component = renderer.create( + createElement(false, [0, 1, 2, 3, 4, 5, false, 7, 8, 9]) + ) + expect(component.toJSON()).toMatchSnapshot() + }) +}) + +describeEach('misc tests', Switch => { + const createElement = makeCreateElement(Switch) + + it('should return content from first occurence when multiple matches', () => { + const component = renderer.create( + createElement(true, [0, 1, 2, true, 4, 5, true, 7, 8, 9]) + ) + expect(component.toJSON()).toMatchSnapshot() }) }) diff --git a/test/utils/index.js b/test/utils/index.js deleted file mode 100644 index a5352044..00000000 --- a/test/utils/index.js +++ /dev/null @@ -1,4 +0,0 @@ - -export const createTests = (...components) => (test) => { - components.forEach(test) -}