-
Notifications
You must be signed in to change notification settings - Fork 55
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
Showing
2 changed files
with
184 additions
and
175 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import ActionTypes from '../src/ActionTypes'; | ||
|
||
|
||
it('UPDATE_MATCH, RESOLVE_MATCH are defined', () => { | ||
expect(ActionTypes.UPDATE_MATCH).toBeDefined(); | ||
expect(ActionTypes.RESOLVE_MATCH).toBeDefined(); | ||
describe('ActionTypes', () => { | ||
it('should have the correct exports', () => { | ||
expect(ActionTypes.UPDATE_MATCH).toBeDefined(); | ||
expect(ActionTypes.RESOLVE_MATCH).toBeDefined(); | ||
}); | ||
}); |
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,201 +1,209 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
|
||
import { mount } from 'enzyme'; | ||
import React from 'react'; | ||
import { | ||
mount, | ||
} from 'enzyme'; | ||
|
||
import BaseLink from '../src/BaseLink'; | ||
|
||
const CustomComponent = () => <div />; | ||
|
||
let router; | ||
|
||
beforeEach(() => { | ||
router = { | ||
push: jest.fn(), | ||
replace: jest.fn(), | ||
go: jest.fn(), | ||
isActive: jest.fn(), | ||
createHref: jest.fn(), | ||
createLocation: jest.fn(), | ||
addTransitionHook: jest.fn(), | ||
}; | ||
}); | ||
|
||
it('renders <a> as default', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
expect(link.find('a')).toHaveLength(1); | ||
}); | ||
|
||
it('can use custom Component as link', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
Component={CustomComponent} | ||
/> | ||
); | ||
expect(link.find('a')).toHaveLength(0); | ||
expect(link.find(CustomComponent)).toHaveLength(1); | ||
}); | ||
|
||
describe('when clicked', () => { | ||
it('calls a user defined click handler', () => { | ||
const handleClick = jest.fn(); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
onClick={handleClick} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click'); | ||
expect(handleClick).toBeCalled(); | ||
describe('<BaseLink>', () => { | ||
let router; | ||
|
||
beforeEach(() => { | ||
router = { | ||
push: jest.fn(), | ||
replace: jest.fn(), | ||
go: jest.fn(), | ||
createHref: jest.fn(), | ||
createLocation: jest.fn(), | ||
isActive: jest.fn(), | ||
matcher: { | ||
match: jest.fn(), | ||
getRoutes: jest.fn(), | ||
isActive: jest.fn(), | ||
format: jest.fn(), | ||
}, | ||
addTransitionHook: jest.fn(), | ||
}; | ||
}); | ||
|
||
it('push location with `to`', () => { | ||
it('should render <a> by default', () => { | ||
const link = mount( | ||
<BaseLink | ||
to="/path-to-another-page" | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 0 }); | ||
expect(router.push).toBeCalledWith('/path-to-another-page'); | ||
expect(link.find('a')).toHaveLength(1); | ||
}); | ||
|
||
it('should not push location if handler call preventDefault', () => { | ||
const handleClick = event => event.preventDefault(); | ||
it('should support a custom Component', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
onClick={handleClick} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 0 }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
|
||
it('should not push location if click be modified', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
const a = link.find('a'); | ||
|
||
a.simulate('click', { button: 0, metaKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
|
||
a.simulate('click', { button: 0, altKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
|
||
a.simulate('click', { button: 0, ctrlKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
|
||
a.simulate('click', { button: 0, shiftKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
|
||
it('should not push location if it was not a left click', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 2 }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
|
||
it('should not push location if target exists', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
target="_blank" | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 0 }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('active state', () => { | ||
it('should not call isActive when all of activeClassName, activeStyle, activePropName were absence', () => { // eslint-disable-line max-len | ||
mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
expect(router.isActive).not.toBeCalled(); | ||
}); | ||
|
||
it('passed activeClassName down when active', () => { | ||
router.isActive.mockReturnValueOnce(true); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
activeClassName="active" | ||
Component={CustomComponent} | ||
/> | ||
); | ||
|
||
expect(link.find('a').prop('className')).toMatch(/active/); | ||
expect(link.find('a')).toHaveLength(0); | ||
expect(link.find(CustomComponent)).toHaveLength(1); | ||
}); | ||
|
||
it('passed activeStyle down when active', () => { | ||
router.isActive.mockReturnValueOnce(true); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
activeStyle={{ color: '#fff' }} | ||
/> | ||
); | ||
|
||
expect(link.find('a').prop('style').color).toBe('#fff'); | ||
describe('when clicked', () => { | ||
it('should call a custom click handler', () => { | ||
const handleClick = jest.fn(); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
onClick={handleClick} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click'); | ||
expect(handleClick).toBeCalled(); | ||
}); | ||
|
||
it('should navigate to the destination location', () => { | ||
const link = mount( | ||
<BaseLink | ||
to="/path-to-another-page" | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 0 }); | ||
expect(router.push).toBeCalledWith('/path-to-another-page'); | ||
}); | ||
|
||
it('should not navigate if the click handler calls preventDefault', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
onClick={(event) => { event.preventDefault(); }} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 0 }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
|
||
it('should not navigate on modified clicks', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
|
||
const a = link.find('a'); | ||
|
||
a.simulate('click', { button: 0, metaKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
|
||
a.simulate('click', { button: 0, altKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
|
||
a.simulate('click', { button: 0, ctrlKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
|
||
a.simulate('click', { button: 0, shiftKey: true }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
|
||
it('should not navigate on non-left clicks', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 2 }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
|
||
it('should not navigate if target is defined', () => { | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
target="_blank" | ||
/> | ||
); | ||
|
||
link.find('a').simulate('click', { button: 0 }); | ||
expect(router.push).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
it('passed activePropName to Component when it exists', () => { | ||
router.isActive.mockReturnValueOnce(true); | ||
let link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
Component={CustomComponent} | ||
activePropName="active" | ||
/> | ||
); | ||
|
||
expect(link.find(CustomComponent).prop('active')).toBe(true); | ||
|
||
router.isActive.mockReturnValueOnce(false); | ||
|
||
link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
Component={CustomComponent} | ||
activePropName="active" | ||
/> | ||
); | ||
|
||
expect(link.find(CustomComponent).prop('active')).toBe(false); | ||
describe('active state', () => { | ||
it('should not call isActive when not showing active state', () => { | ||
mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
/> | ||
); | ||
|
||
expect(router.isActive).not.toBeCalled(); | ||
}); | ||
|
||
it('should set activeClassName when active', () => { | ||
router.isActive.mockReturnValueOnce(true); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
activeClassName="active" | ||
/> | ||
); | ||
|
||
expect(link.find('a').prop('className')).toMatch(/active/); | ||
}); | ||
|
||
it('should set activeStyle when active', () => { | ||
router.isActive.mockReturnValueOnce(true); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
activeStyle={{ color: '#fff' }} | ||
/> | ||
); | ||
|
||
expect(link.find('a').prop('style').color).toBe('#fff'); | ||
}); | ||
|
||
it('should set activePropName when active', () => { | ||
router.isActive.mockReturnValueOnce(true); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
Component={CustomComponent} | ||
activePropName="active" | ||
/> | ||
); | ||
|
||
expect(link.find(CustomComponent).prop('active')).toBe(true); | ||
}); | ||
|
||
it('should not set activePropName when not active', () => { | ||
router.isActive.mockReturnValueOnce(false); | ||
const link = mount( | ||
<BaseLink | ||
match={{}} | ||
router={router} | ||
Component={CustomComponent} | ||
activePropName="active" | ||
/> | ||
); | ||
|
||
expect(link.find(CustomComponent).prop('active')).toBe(false); | ||
}); | ||
}); | ||
}); |