Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
newyork-anthonyng committed Sep 8, 2017
1 parent 0b7e16f commit ac248d8
Show file tree
Hide file tree
Showing 6 changed files with 1,788 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "react"]
}
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
node_modules/
npm-debug.log
coverage/
30 changes: 27 additions & 3 deletions package.json
Expand Up @@ -11,21 +11,45 @@
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-jest": "^21.0.0",
"babel-preset-es2015": "^6.24.1",
"react": "^15.6.1"
"babel-preset-react": "^6.24.1",
"enzyme": "^2.9.1",
"enzyme-to-json": "^1.5.1",
"jest": "^21.0.1",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-test-renderer": "^15.6.1"
},
"bugs": {
"url": "https://github.com/chrisbolin/react-detect-offline/issues"
},
"homepage": "https://github.com/chrisbolin/react-detect-offline#readme",
"scripts": {
"compile": "babel --presets es2015 -d dist/ src/"
"compile": "babel src/index.js -o dist/index.js",
"test": "jest spec --watch",
"test.cover": "jest spec --coverage"
},
"keywords": [
"react",
"offline",
"online",
"detect",
"disconnect"
]
],
"jest": {
"collectCoverageFrom": [
"src/**/*.js",
"!<rootDir>/node_modules/",
"!<rootDir>/demo/"
],
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
}
}
51 changes: 51 additions & 0 deletions src/__snapshots__/index.spec.js.snap
@@ -0,0 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Offline should remove event listeners when unmounting 1`] = `
Array [
Array [
"online",
[Function],
],
Array [
"offline",
[Function],
],
Array [
"keydown",
[Function],
],
]
`;

exports[`Offline should render children when offline 1`] = `
<Offline>
<h1>
Hello World
</h1>
</Offline>
`;

exports[`Online should remove event listeners when unmounting 1`] = `
Array [
Array [
"online",
[Function],
],
Array [
"offline",
[Function],
],
Array [
"keydown",
[Function],
],
]
`;

exports[`Online should render children when online 1`] = `
<Online>
<h1>
Hello World
</h1>
</Online>
`;
124 changes: 124 additions & 0 deletions src/index.spec.js
@@ -0,0 +1,124 @@
import React from 'react';
import { mount } from 'enzyme';
import toJSON from 'enzyme-to-json';
import { Online, Offline } from './';

const eventListenerMap = {};
beforeEach(() => {
window.addEventListener = jest.fn((event, cb) => {
eventListenerMap[event] = cb;
});
window.removeEventListener = jest.fn(event => {
eventListenerMap[event] = undefined;
});
});

describe('Online', () => {
beforeEach(() => {
Object.defineProperty(navigator, 'onLine', {
configurable: true,
value: true
});
});

it('should render children when online', () => {
const wrapper = mount(
<Online>
<h1>Hello World</h1>
</Online>
);

expect(toJSON(wrapper)).toMatchSnapshot();
});

it('should not render children when offline', () => {
Object.defineProperty(navigator, 'onLine', { value: false });

const wrapper = mount(
<Online>
<h1>Hello World</h1>
</Online>
);

expect(wrapper.html()).toBeNull();
});

it('should not render children when going from online to offline', () => {
const wrapper = mount(
<Online>
<h1>Hello World</h1>
</Online>
);

eventListenerMap.offline();

expect(wrapper.html()).toBeNull();
});

it('should remove event listeners when unmounting', () => {
const wrapper = mount(
<Online>
<h1>Hello World</h1>
</Online>
);
wrapper.unmount();

expect(window.removeEventListener).toHaveBeenCalledTimes(3);
expect(window.removeEventListener.mock.calls).toMatchSnapshot();
});
});

describe('Offline', () => {
beforeEach(() => {
Object.defineProperty(navigator, 'onLine', {
configurable: true,
value: false
});
});

it('should render children when offline', () => {
const wrapper = mount(
<Offline>
<h1>Hello World</h1>
</Offline>
);

expect(toJSON(wrapper)).toMatchSnapshot();
});

it('should not render children when online', () => {
Object.defineProperty(navigator, 'onLine', { value: true });

const wrapper = mount(
<Offline>
<h1>Hello World</h1>
</Offline>
);

expect(wrapper.html()).toBeNull();
});

it('should not render children when going from offline to online', () => {
const wrapper = mount(
<Offline>
<h1>Hello World</h1>
</Offline>
);

eventListenerMap.online();

expect(wrapper.html()).toBeNull();
});

it('should remove event listeners when unmounting', () => {
const wrapper = mount(
<Offline>
<h1>Hello World</h1>
</Offline>
);
wrapper.unmount();

expect(window.removeEventListener).toHaveBeenCalledTimes(3);
expect(window.removeEventListener.mock.calls).toMatchSnapshot();
});
});

0 comments on commit ac248d8

Please sign in to comment.