Skip to content

Commit 011af1c

Browse files
committed
test: add <LocationSensor> tests
1 parent 8cad408 commit 011af1c

File tree

4 files changed

+169
-22
lines changed

4 files changed

+169
-22
lines changed

package.json

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,34 @@
2828
"prop-types": "15"
2929
},
3030
"devDependencies": {
31-
"jest": "22.1.2",
32-
"jest-tap-reporter": "1.9.0",
33-
"ts-jest": "22.0.1",
34-
"typescript": "2.6.2",
35-
"ts-loader": "3.2.0",
36-
"webpack": "3.10.0",
37-
"ts-node": "4.1.0",
38-
"rimraf": "2.6.2",
31+
"@storybook/addon-actions": "^3.3.10",
32+
"@storybook/addon-links": "^3.3.10",
33+
"@storybook/react": "^3.3.10",
34+
"@types/chai": "4.1.1",
35+
"@types/jest": "22.0.1",
36+
"@types/node": "9.3.0",
37+
"@types/react": "15",
38+
"chai": "4.1.2",
3939
"enzyme": "3.3.0",
4040
"enzyme-adapter-react-15": "1.0.5",
4141
"enzyme-to-json": "3.3.0",
42-
"react-test-renderer": "15",
43-
"mol-conventional-changelog": "1.2.0",
42+
"gulp": "3.9.1",
43+
"gulp-typescript": "3",
44+
"jest": "22.1.2",
45+
"jest-environment-jsdom": "^22.1.4",
46+
"jest-environment-jsdom-global": "^1.0.3",
47+
"jest-tap-reporter": "1.9.0",
4448
"mocha": "5.0.0",
45-
"chai": "4.1.2",
49+
"mol-conventional-changelog": "1.2.0",
4650
"react-markdown": "3.1.4",
51+
"react-test-renderer": "15",
52+
"rimraf": "2.6.2",
4753
"to-string-loader": "1.1.5",
48-
"gulp": "3.9.1",
49-
"gulp-typescript": "3",
50-
"@types/node": "9.3.0",
51-
"@types/jest": "22.0.1",
52-
"@types/chai": "4.1.1",
53-
"@types/react": "15",
54-
"@storybook/react": "^3.3.10",
55-
"@storybook/addon-actions": "^3.3.10",
56-
"@storybook/addon-links": "^3.3.10"
54+
"ts-jest": "22.0.1",
55+
"ts-loader": "3.2.0",
56+
"ts-node": "4.1.0",
57+
"typescript": "2.6.2",
58+
"webpack": "3.10.0"
5759
},
5860
"config": {
5961
"commitizen": {
@@ -89,6 +91,7 @@
8991
],
9092
"reporters": [
9193
"jest-tap-reporter"
92-
]
94+
],
95+
"testEnvironment": "jest-environment-jsdom-global"
9396
}
9497
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<LocationSensor> re-renders on popstate 1`] = `
4+
Object {
5+
"hash": "",
6+
"host": "example.com",
7+
"hostname": "example.com",
8+
"href": "https://example.com/foobar",
9+
"length": 1,
10+
"origin": "https://example.com",
11+
"pathname": "/foobar",
12+
"port": "",
13+
"protocol": "https:",
14+
"search": "",
15+
"state": null,
16+
"trigger": "popstate",
17+
}
18+
`;
19+
20+
exports[`<LocationSensor> re-renders on pushstate 1`] = `
21+
Object {
22+
"hash": "",
23+
"host": "example.com",
24+
"hostname": "example.com",
25+
"href": "https://example.com/foobar",
26+
"length": 1,
27+
"origin": "https://example.com",
28+
"pathname": "/foobar",
29+
"port": "",
30+
"protocol": "https:",
31+
"search": "",
32+
"state": null,
33+
"trigger": "pushstate",
34+
}
35+
`;
36+
37+
exports[`<LocationSensor> re-renders on replacestate 1`] = `
38+
Object {
39+
"hash": "",
40+
"host": "example.com",
41+
"hostname": "example.com",
42+
"href": "https://example.com/foobar",
43+
"length": 1,
44+
"origin": "https://example.com",
45+
"pathname": "/foobar",
46+
"port": "",
47+
"protocol": "https:",
48+
"search": "",
49+
"state": null,
50+
"trigger": "replacestate",
51+
}
52+
`;
53+
54+
exports[`<LocationSensor> returns expected default state 1`] = `
55+
Object {
56+
"hash": "",
57+
"host": "example.com",
58+
"hostname": "example.com",
59+
"href": "https://example.com/",
60+
"length": 1,
61+
"origin": "https://example.com",
62+
"pathname": "/",
63+
"port": "",
64+
"protocol": "https:",
65+
"search": "",
66+
"state": null,
67+
"trigger": "load",
68+
}
69+
`;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {mount} from 'enzyme';
2+
import {h} from '../../util';
3+
import {LocationSensor} from '..';
4+
5+
declare const jsdom;
6+
7+
describe('<LocationSensor>', () => {
8+
beforeEach(() => {
9+
jsdom.reconfigure({
10+
windowTop: window,
11+
url: "https://example.com/"
12+
});
13+
});
14+
15+
it('is a component', () => {
16+
expect(LocationSensor).toBeInstanceOf(Function);
17+
});
18+
19+
it('renders without crashing', () => {
20+
const wrapper = mount(
21+
<LocationSensor>{(location) =>
22+
null
23+
}</LocationSensor>
24+
);
25+
});
26+
27+
it('returns expected default state', () => {
28+
const wrapper = mount(
29+
<LocationSensor>{(location) => {
30+
expect(location).toMatchSnapshot();
31+
32+
return null;
33+
}}</LocationSensor>
34+
);
35+
});
36+
37+
38+
describe('re-renders on', () => {
39+
const events = [
40+
'pushstate',
41+
'replacestate',
42+
'popstate'
43+
];
44+
45+
events.forEach((eventName) => {
46+
it(eventName, () => {
47+
const paths = [];
48+
const triggers = [];
49+
50+
const wrapper = mount(
51+
<LocationSensor>{(location) => {
52+
triggers.push(location.trigger);
53+
paths.push(location.pathname);
54+
55+
return null;
56+
}}</LocationSensor>
57+
);
58+
59+
jsdom.reconfigure({
60+
windowTop: window,
61+
url: "https://example.com/foobar"
62+
});
63+
64+
const event = new Event(eventName);
65+
66+
window.dispatchEvent(event);
67+
68+
wrapper.update();
69+
70+
expect(triggers).toEqual(['load', eventName]);
71+
expect(paths).toEqual(['/', '/foobar']);
72+
});
73+
});
74+
});
75+
});

src/LocationSensor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class LocationSensor extends Component<ILocationSensorProps, ILocationSen
100100
port,
101101
protocol,
102102
search
103-
} = location;
103+
} = window.location;
104104

105105
return {
106106
trigger,

0 commit comments

Comments
 (0)