-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improving test and documentation for Link
- Loading branch information
1 parent
99cffee
commit 21344b0
Showing
5 changed files
with
159 additions
and
7 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
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
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,90 @@ | ||
import React from 'react'; | ||
import Link from '../src/Link'; | ||
import renderer from 'react-test-renderer'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { setCurrentUrl } from './helper.lib'; | ||
|
||
test('should be rendered correctly', () => { | ||
const component = renderer.create( | ||
<Link to="/about">About</Link> | ||
); | ||
let tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
test('should call goto if prop "to" is set', () => { | ||
const component = shallow( | ||
<Link to="/about">About</Link> | ||
); | ||
|
||
const oldPushState = window.history.pushState; | ||
const oldOnPopState = window.onpopstate; | ||
window.history.pushState = jest.fn(); | ||
window.onpopstate = jest.fn(); | ||
|
||
component.find('a').simulate('click', { preventDefault() {} }); | ||
|
||
// FIXME: should mock router | ||
expect(window.history.pushState.mock.calls.length).toBe(1); | ||
expect(window.history.pushState.mock.calls[0][2]).toBe('/about'); | ||
expect(window.onpopstate.mock.calls.length).toBe(1); | ||
|
||
window.history.pushState = oldPushState; | ||
window.onpopstate = oldOnPopState; | ||
}); | ||
|
||
test('should call onClick if it is set', () => { | ||
let counter = 0; | ||
const component = shallow( | ||
<Link onClick={() => counter++}>About</Link> | ||
); | ||
|
||
component.find('a').simulate('click', { preventDefault() {} }); | ||
|
||
expect(counter).toBe(1); | ||
}); | ||
|
||
test('should set queryString if prop "queryTo" is set', () => { | ||
const component = shallow( | ||
<Link queryTo={{ type: 'test' }}>About</Link> | ||
); | ||
|
||
setCurrentUrl('http://www.example.com/tasks'); | ||
const oldPushState = window.history.pushState; | ||
const oldOnPopState = window.onpopstate; | ||
window.history.pushState = jest.fn(); | ||
window.onpopstate = jest.fn(); | ||
|
||
component.find('a').simulate('click', { preventDefault() {} }); | ||
|
||
// FIXME: should mock router | ||
expect(window.history.pushState.mock.calls.length).toBe(1); | ||
expect(window.history.pushState.mock.calls[0][2]).toBe('/tasks?type=test'); | ||
expect(window.onpopstate.mock.calls.length).toBe(1); | ||
|
||
window.history.pushState = oldPushState; | ||
window.onpopstate = oldOnPopState; | ||
}); | ||
|
||
test('should set keep query if prop "keepQuery" is set', () => { | ||
const component = shallow( | ||
<Link to="/tasks" keepQuery>About</Link> | ||
); | ||
|
||
setCurrentUrl('http://www.example.com/?type=test'); | ||
const oldPushState = window.history.pushState; | ||
const oldOnPopState = window.onpopstate; | ||
window.history.pushState = jest.fn(); | ||
window.onpopstate = jest.fn(); | ||
|
||
component.find('a').simulate('click', { preventDefault() {} }); | ||
|
||
// FIXME: should mock router | ||
expect(window.history.pushState.mock.calls.length).toBe(1); | ||
expect(window.history.pushState.mock.calls[0][2]).toBe('/tasks?type=test'); | ||
expect(window.onpopstate.mock.calls.length).toBe(1); | ||
|
||
window.history.pushState = oldPushState; | ||
window.onpopstate = oldOnPopState; | ||
}); |
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,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should be rendered correctly 1`] = ` | ||
<a | ||
href="/about" | ||
onClick={[Function]} | ||
> | ||
About | ||
</a> | ||
`; |