Skip to content

Commit 489b467

Browse files
author
Raice Hannay
committed
initial commit
0 parents  commit 489b467

23 files changed

+5567
-0
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
indent_size = 2
6+
indent_style = space
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
#project
6+
coverage
7+
dist
8+
lib
9+
10+
#node
11+
node_modules
12+
npm-debug.log
13+
yarn.error.l
14+
15+
#IDEs
16+
.idea
17+
*.sublime-*
18+
.tern-project
19+
20+
#Mac OS
21+
.DS_Store
22+
.DS_Store?
23+
._*
24+
.Spotlight-V100
25+
.Trashes
26+
27+
#git hooks
28+
pre-commit.sh
29+
30+
#.teamcity
31+
.teamcity/target/
32+
.teamcity/.classpath
33+
.teamcity/.settings/
34+
.teamcity/.project

.prettierignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coverage/*
2+
dist/*
3+
package.json
4+
yarn.lock
5+
README.md

jest.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
bail: true,
3+
collectCoverageFrom: ["src/**/*.{ts,tsx}", "!**/node_modules/**"],
4+
coverageThreshold: {
5+
global: {
6+
branches: 100,
7+
functions: 100,
8+
lines: 100,
9+
statements: 100
10+
}
11+
},
12+
globals: {
13+
"ts-jest": {
14+
tsConfig: {
15+
target: "es6"
16+
}
17+
}
18+
},
19+
moduleDirectories: ["node_modules"],
20+
preset: "ts-jest",
21+
roots: ["<rootDir>/src"],
22+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
23+
snapshotSerializers: ["enzyme-to-json/serializer"],
24+
testEnvironment: "jsdom",
25+
testMatch: ["**/*.test.{ts,tsx}"],
26+
testURL: "http://localhost",
27+
transformIgnorePatterns: [
28+
"/node_modules/(?!intl-messageformat|intl-messageformat-parser).+\\.js$"
29+
],
30+
verbose: true
31+
};

jest.setup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configure } from "enzyme";
2+
import EnzymeAdapter from "enzyme-adapter-react-16";
3+
4+
configure({ adapter: new EnzymeAdapter() });

package.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"name": "react-test-wrapper",
3+
"author": "Raice Hannay <[email protected]>",
4+
"description": "A set of classes to make setting up React components for unit tests easy.",
5+
"license": "ISC",
6+
"version": "1.0.0",
7+
"keywords": [
8+
"component",
9+
"enzyme",
10+
"jest",
11+
"react",
12+
"react-intl",
13+
"react-redux",
14+
"redux",
15+
"test",
16+
"tests",
17+
"tester",
18+
"testing",
19+
"unit-test",
20+
"unit-tests",
21+
"unit-testing"
22+
],
23+
"main": "dist/index.js",
24+
"module": "lib/index.js",
25+
"scripts": {
26+
"build": "rollup -c && tsc",
27+
"format": "prettier --write \"**/*.{js,jsx,json,ts,tsx}\"",
28+
"lint": "tslint \"./{types,src,pages}/**/*.ts?(x)\"",
29+
"test": "cross-env NODE_ENV=test jest --no-cache --config ./jest.config.js",
30+
"test:all": "npm-run-all format typecheck lint test:coverage",
31+
"test:coverage": "cross-env NODE_ENV=test jest --no-cache --coverage --config ./jest.config.js",
32+
"typecheck": "tsc"
33+
},
34+
"prepublish": "build",
35+
"repository": {
36+
"type": "git",
37+
"url": "[email protected]:voodoocreation/react-test-wrapper.git"
38+
},
39+
"bugs": {
40+
"url": "https://github.com/voodoocreation/react-test-wrapper/issues"
41+
},
42+
"types": "dist/index.d.ts",
43+
"peerDependencies": {
44+
"enzyme": "^3.10.0",
45+
"react": "^16.12.0",
46+
"react-intl": "^3.9.1",
47+
"react-redux": "^7.1.3",
48+
"redux": "^4.0.4"
49+
},
50+
"peerDependenciesMeta": {
51+
"react-intl": {
52+
"optional": true
53+
},
54+
"react-redux": {
55+
"optional": true
56+
},
57+
"redux": {
58+
"optional": true
59+
}
60+
},
61+
"devDependencies": {
62+
"@types/enzyme": "^3.10.3",
63+
"@types/enzyme-adapter-react-16": "^1.0.5",
64+
"@types/jest": "^24.0.23",
65+
"@types/lodash.merge": "^4.6.6",
66+
"@types/react": "^16.9.15",
67+
"@types/react-redux": "^7.1.5",
68+
"cross-env": "^6.0.3",
69+
"enzyme": "^3.10.0",
70+
"enzyme-adapter-react-16": "^1.15.1",
71+
"enzyme-to-json": "^3.4.3",
72+
"jest": "^24.9.0",
73+
"npm-run-all": "^4.1.5",
74+
"prettier": "^1.19.1",
75+
"react": "^16.12.0",
76+
"react-dom": "^16.12.0",
77+
"react-intl": "^3.9.1",
78+
"react-redux": "^7.1.3",
79+
"redux": "^4.0.4",
80+
"rollup": "^1.27.9",
81+
"rollup-plugin-terser": "^5.1.2",
82+
"rollup-plugin-typescript2": "^0.25.3",
83+
"ts-jest": "^24.2.0",
84+
"tslint": "^5.20.0",
85+
"tslint-config-prettier": "^1.18.0",
86+
"tslint-react": "^4.1.0",
87+
"typescript": "^3.7.3",
88+
"typescript-fsa": "^3.0.0",
89+
"typescript-fsa-reducers": "^1.2.1"
90+
},
91+
"dependencies": {
92+
"lodash.merge": "^4.6.2"
93+
}
94+
}

rollup.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import typescript from "rollup-plugin-typescript2";
2+
import pkg from "./package.json";
3+
import { terser } from "rollup-plugin-terser";
4+
5+
export default {
6+
input: "src/index.ts", // our source file
7+
output: [
8+
{
9+
file: pkg.main,
10+
format: "cjs"
11+
},
12+
{
13+
file: pkg.module,
14+
format: "es" // the preferred format
15+
}
16+
],
17+
external: [...Object.keys(pkg.dependencies || {})],
18+
plugins: [
19+
typescript({
20+
typescript: require("typescript")
21+
}),
22+
terser() // minifies generated bundles
23+
]
24+
};

src/Wrapper.test.tsx

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { ReactWrapper, ShallowWrapper } from "enzyme";
2+
import * as React from "react";
3+
4+
import { Dummy } from "../test/Dummy";
5+
import Wrapper from "./Wrapper";
6+
7+
describe("Wrapper", () => {
8+
describe("when using the different render methods", () => {
9+
const component = new Wrapper(Dummy).withDefaultProps({
10+
value: "Default value"
11+
});
12+
13+
describe("when using 'shallow'", () => {
14+
const { wrapper } = component.shallow();
15+
16+
it("returns ShallowWrapper", () => {
17+
expect(wrapper).toBeInstanceOf(ShallowWrapper);
18+
});
19+
20+
it("has the correct root node", () => {
21+
expect(wrapper.instance()).toBeInstanceOf(Dummy);
22+
});
23+
24+
it("matches snapshot", () => {
25+
expect(wrapper).toMatchSnapshot();
26+
});
27+
});
28+
29+
describe("when using 'mount'", () => {
30+
const { wrapper } = component.mount();
31+
32+
it("returns ReactWrapper", () => {
33+
expect(wrapper).toBeInstanceOf(ReactWrapper);
34+
});
35+
36+
it("has the correct root node", () => {
37+
expect(wrapper.instance()).toBeInstanceOf(Dummy);
38+
});
39+
40+
it("matches snapshot", () => {
41+
expect(wrapper).toMatchSnapshot();
42+
});
43+
});
44+
45+
describe("when using 'render'", () => {
46+
const { wrapper } = component.render();
47+
48+
it("returns Cheerio wrapper", () => {
49+
expect(wrapper.cheerio).toBe("[cheerio object]");
50+
});
51+
52+
it("has the correct root node", () => {
53+
expect(wrapper.hasClass("Dummy")).toBe(true);
54+
});
55+
56+
it("matches snapshot", () => {
57+
expect(wrapper).toMatchSnapshot();
58+
});
59+
});
60+
});
61+
62+
describe("when using the children API", () => {
63+
const component = new Wrapper(Dummy).withDefaultChildren(
64+
<div>Default children</div>
65+
);
66+
67+
it("renders default children correctly", () => {
68+
const { wrapper } = component.render();
69+
70+
expect(wrapper.find(".Dummy--children").html()).toBe(
71+
"<div>Default children</div>"
72+
);
73+
});
74+
75+
it("renders test-specific children correctly", () => {
76+
const { wrapper } = component
77+
.withChildren(<span>Test children</span>)
78+
.render();
79+
80+
expect(wrapper.find(".Dummy--children").html()).toBe(
81+
"<span>Test children</span>"
82+
);
83+
});
84+
85+
it("clears test-specific children after previous test and renders default children again", () => {
86+
const { wrapper } = component.render();
87+
88+
expect(wrapper.find(".Dummy--children").html()).toBe(
89+
"<div>Default children</div>"
90+
);
91+
});
92+
});
93+
94+
describe("when using the props API", () => {
95+
const component = new Wrapper(Dummy).withDefaultProps({
96+
value: "Default value"
97+
});
98+
99+
it("renders with default props correctly", () => {
100+
const { wrapper } = component.render();
101+
102+
expect(wrapper.find(".Dummy--value").text()).toBe("Default value");
103+
});
104+
105+
it("renders with test-specific props correctly", () => {
106+
const { wrapper } = component
107+
.withProps({
108+
value: "Test value"
109+
})
110+
.render();
111+
112+
expect(wrapper.find(".Dummy--value").text()).toBe("Test value");
113+
});
114+
115+
it("clears test-specific props after previous test and uses default props again", () => {
116+
const { wrapper } = component.render();
117+
118+
expect(wrapper.find(".Dummy--value").text()).toBe("Default value");
119+
});
120+
});
121+
});

0 commit comments

Comments
 (0)