Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b7e16f
commit ac248d8
Showing
6 changed files
with
1,788 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
npm-debug.log | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
Oops, something went wrong.