Skip to content

Commit 61120f6

Browse files
pshrmnmjackson
authored andcommitted
NavLink exact & strict tests (#4454)
1 parent ea18d6b commit 61120f6

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

packages/react-router-dom/modules/__tests__/NavLink-test.js

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,13 @@ describe('NavLink', () => {
7474
})
7575

7676
describe('isActive', () => {
77-
const activeExact = (match, location) => !!match && match.isExact
78-
7977
it('applies active props when isActive returns true', () => {
8078
ReactDOM.render((
8179
<MemoryRouter initialEntries={['/pizza']}>
8280
<NavLink
8381
to='/pizza'
8482
activeClassName='active'
85-
isActive={activeExact}
83+
isActive={() => true}
8684
>
8785
Pizza!
8886
</NavLink>
@@ -96,9 +94,9 @@ describe('NavLink', () => {
9694
ReactDOM.render((
9795
<MemoryRouter initialEntries={['/pizza']}>
9896
<NavLink
99-
to='/pizza/anchovies'
97+
to='/pizza'
10098
activeClassName='active'
101-
isActive={activeExact}
99+
isActive={() => false}
102100
>
103101
Pizza!
104102
</NavLink>
@@ -109,4 +107,69 @@ describe('NavLink', () => {
109107
})
110108
})
111109

110+
describe('exact', () => {
111+
it('does not do exact matching by default', () => {
112+
ReactDOM.render((
113+
<MemoryRouter initialEntries={['/pizza/anchovies']}>
114+
<NavLink to='/pizza' activeClassName='active'>Pizza!</NavLink>
115+
</MemoryRouter>
116+
), node)
117+
const a = node.getElementsByTagName('a')[0]
118+
expect(a.className).toContain('active')
119+
})
120+
121+
it('sets active value only for exact matches', () => {
122+
ReactDOM.render((
123+
<MemoryRouter initialEntries={['/pizza']}>
124+
<NavLink exact to='/pizza' exact activeClassName='active'>Pizza!</NavLink>
125+
</MemoryRouter>
126+
), node)
127+
const a = node.getElementsByTagName('a')[0]
128+
expect(a.className).toContain('active')
129+
})
130+
131+
it('does not set active value for partial matches', () => {
132+
ReactDOM.render((
133+
<MemoryRouter initialEntries={['/pizza/anchovies']}>
134+
<NavLink exact to='/pizza' activeClassName='active'>Pizza!</NavLink>
135+
</MemoryRouter>
136+
), node)
137+
const a = node.getElementsByTagName('a')[0]
138+
expect(a.className).toNotContain('active')
139+
})
140+
})
141+
142+
describe('strict (enforce path\'s trailing slash)', () => {
143+
const PATH = '/pizza/'
144+
it('does not do strict matching by default', () => {
145+
ReactDOM.render((
146+
<MemoryRouter initialEntries={['/pizza']}>
147+
<NavLink to={PATH} activeClassName='active'>Pizza!</NavLink>
148+
</MemoryRouter>
149+
), node)
150+
const a = node.getElementsByTagName('a')[0]
151+
expect(a.className).toContain('active')
152+
})
153+
154+
it('does not set active value when location.pathname has no trailing slash', () => {
155+
ReactDOM.render((
156+
<MemoryRouter initialEntries={['/pizza']}>
157+
<NavLink strict to={PATH} activeClassName='active'>Pizza!</NavLink>
158+
</MemoryRouter>
159+
), node)
160+
const a = node.getElementsByTagName('a')[0]
161+
expect(a.className).toNotContain('active')
162+
})
163+
164+
it('sets active when pathname has trailing slash', () => {
165+
ReactDOM.render((
166+
<MemoryRouter initialEntries={['/pizza/']}>
167+
<NavLink strict to={PATH} activeClassName='active'>Pizza!</NavLink>
168+
</MemoryRouter>
169+
), node)
170+
const a = node.getElementsByTagName('a')[0]
171+
expect(a.className).toContain('active')
172+
})
173+
})
174+
112175
})

0 commit comments

Comments
 (0)