|
1 | 1 | import * as React from 'react';
|
2 | 2 | import { makeTestRouter, muteConsoleErrors } from '../../__tests__/util';
|
3 | 3 | import { useSref } from '../useSref';
|
4 |
| -import { UISref } from '../../UISref'; |
5 |
| -import { UISrefActiveContext } from '../../UISrefActive'; |
6 |
| - |
7 |
| -const state = { |
8 |
| - name: 'state', |
9 |
| - url: '', |
10 |
| - component: () => ( |
11 |
| - <UISref to="state2"> |
12 |
| - <a>state2</a> |
13 |
| - </UISref> |
14 |
| - ), |
15 |
| -}; |
16 |
| - |
17 |
| -const state2 = { |
18 |
| - name: 'state2', |
19 |
| - url: '/state2', |
20 |
| - component: () => <span>state2</span>, |
21 |
| -}; |
| 4 | +import { UISrefActive, UISrefActiveContext } from '../../UISrefActive'; |
| 5 | + |
| 6 | +const state = { name: 'state', url: '/state' }; |
| 7 | +const state2 = { name: 'state2', url: '/state2' }; |
| 8 | +const state3 = { name: 'state3', url: '/state3/:param' }; |
22 | 9 |
|
23 | 10 | describe('useUiSref', () => {
|
24 | 11 | let { router, routerGo, mountInRouter } = makeTestRouter([]);
|
25 |
| - beforeEach(() => ({ router, routerGo, mountInRouter } = makeTestRouter([state, state2]))); |
| 12 | + beforeEach(() => ({ router, routerGo, mountInRouter } = makeTestRouter([state, state2, state3]))); |
26 | 13 |
|
27 | 14 | it('throws if to is not a string', () => {
|
28 | 15 | const Component = () => {
|
@@ -70,37 +57,142 @@ describe('useUiSref', () => {
|
70 | 57 | expect(spy).not.toHaveBeenCalled();
|
71 | 58 | });
|
72 | 59 |
|
73 |
| - it('registers itself with the parent UISrefActive addStateInfo callback', () => { |
74 |
| - const spy = jest.fn(); |
75 |
| - const Component = () => { |
76 |
| - const uiSref = useSref('state', {}); |
| 60 | + it('updates the href when the stateName changes', () => { |
| 61 | + const Component = props => { |
| 62 | + const uiSref = useSref(props.state); |
77 | 63 | return <a {...uiSref} />;
|
78 | 64 | };
|
79 | 65 |
|
80 |
| - mountInRouter( |
81 |
| - <UISrefActiveContext.Provider value={spy}> |
82 |
| - <Component /> |
83 |
| - </UISrefActiveContext.Provider> |
84 |
| - ); |
| 66 | + const wrapper = mountInRouter(<Component state="state" />); |
| 67 | + expect(wrapper.html()).toBe('<a href="/state"></a>'); |
85 | 68 |
|
86 |
| - expect(spy).toBeCalledTimes(1); |
| 69 | + wrapper.setProps({ state: 'state2' }); |
| 70 | + expect(wrapper.html()).toBe('<a href="/state2"></a>'); |
87 | 71 | });
|
88 | 72 |
|
89 |
| - it('deregisters itself with the parent UISrefActive addStateInfo callback when unmounted', () => { |
90 |
| - const spy = jest.fn(); |
91 |
| - const Component = () => { |
92 |
| - const uiSref = useSref('state', {}); |
93 |
| - return <a {...uiSref} />; |
| 73 | + it('updates the href when the params changes', () => { |
| 74 | + const State3Link = props => { |
| 75 | + const sref = useSref('state3', { param: props.param }); |
| 76 | + return <a {...sref} />; |
| 77 | + }; |
| 78 | + |
| 79 | + const wrapper = mountInRouter(<State3Link param="123" />); |
| 80 | + expect(wrapper.html()).toBe('<a href="/state3/123"></a>'); |
| 81 | + |
| 82 | + wrapper.setProps({ param: '456' }); |
| 83 | + expect(wrapper.html()).toBe('<a href="/state3/456"></a>'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('participates in parent UISrefActive component active state', async () => { |
| 87 | + await routerGo('state2'); |
| 88 | + |
| 89 | + const State2Link = props => { |
| 90 | + const sref = useSref('state2'); |
| 91 | + return ( |
| 92 | + <a {...sref} className={props.className}> |
| 93 | + state2 |
| 94 | + </a> |
| 95 | + ); |
94 | 96 | };
|
95 | 97 |
|
96 | 98 | const wrapper = mountInRouter(
|
97 |
| - <UISrefActiveContext.Provider value={() => spy}> |
98 |
| - <Component /> |
99 |
| - </UISrefActiveContext.Provider> |
| 99 | + <UISrefActive class="active"> |
| 100 | + <State2Link /> |
| 101 | + </UISrefActive> |
100 | 102 | );
|
| 103 | + expect(wrapper.html()).toBe('<a href="/state2" class="active">state2</a>'); |
| 104 | + }); |
101 | 105 |
|
102 |
| - expect(spy).toBeCalledTimes(0); |
103 |
| - wrapper.unmount(); |
104 |
| - expect(spy).toBeCalledTimes(1); |
| 106 | + it('participates in grandparent UISrefActive component active state', async () => { |
| 107 | + await routerGo('state2'); |
| 108 | + |
| 109 | + const State2Link = props => { |
| 110 | + const sref = useSref('state2'); |
| 111 | + return ( |
| 112 | + <a {...sref} className={props.className}> |
| 113 | + state2 |
| 114 | + </a> |
| 115 | + ); |
| 116 | + }; |
| 117 | + |
| 118 | + const Component = () => { |
| 119 | + return ( |
| 120 | + <UISrefActive class="grandparentactive"> |
| 121 | + <div> |
| 122 | + <UISrefActive class="active"> |
| 123 | + <State2Link /> |
| 124 | + </UISrefActive> |
| 125 | + </div> |
| 126 | + </UISrefActive> |
| 127 | + ); |
| 128 | + }; |
| 129 | + |
| 130 | + const wrapper = mountInRouter(<Component />); |
| 131 | + expect(wrapper.html()).toBe('<div class="grandparentactive"><a href="/state2" class="active">state2</a></div>'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('stops participating in parent/grandparent UISrefActive component active state when unmounted', async () => { |
| 135 | + await routerGo('state2'); |
| 136 | + |
| 137 | + const State2Link = props => { |
| 138 | + const sref = useSref('state2'); |
| 139 | + return ( |
| 140 | + <a {...sref} className={props.className}> |
| 141 | + state2 |
| 142 | + </a> |
| 143 | + ); |
| 144 | + }; |
| 145 | + |
| 146 | + const Component = props => { |
| 147 | + return ( |
| 148 | + <UISrefActive class="grandparentactive"> |
| 149 | + <div> |
| 150 | + <UISrefActive class="active">{props.show ? <State2Link /> : <span />}</UISrefActive> |
| 151 | + </div> |
| 152 | + </UISrefActive> |
| 153 | + ); |
| 154 | + }; |
| 155 | + |
| 156 | + const wrapper = mountInRouter(<Component show={true} />); |
| 157 | + expect(wrapper.html()).toBe('<div class="grandparentactive"><a href="/state2" class="active">state2</a></div>'); |
| 158 | + |
| 159 | + wrapper.setProps({ show: false }); |
| 160 | + expect(wrapper.html()).toBe('<div><span></span></div>'); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('implementation detail: ', () => { |
| 164 | + it('registers itself with the parent UISrefActive addStateInfo callback', () => { |
| 165 | + const spy = jest.fn(); |
| 166 | + const Component = () => { |
| 167 | + const uiSref = useSref('state', {}); |
| 168 | + return <a {...uiSref} />; |
| 169 | + }; |
| 170 | + |
| 171 | + mountInRouter( |
| 172 | + <UISrefActiveContext.Provider value={spy}> |
| 173 | + <Component /> |
| 174 | + </UISrefActiveContext.Provider> |
| 175 | + ); |
| 176 | + |
| 177 | + expect(spy).toBeCalledTimes(1); |
| 178 | + }); |
| 179 | + |
| 180 | + it('deregisters itself with the parent UISrefActive addStateInfo callback when unmounted', () => { |
| 181 | + const spy = jest.fn(); |
| 182 | + const Component = () => { |
| 183 | + const uiSref = useSref('state', {}); |
| 184 | + return <a {...uiSref} />; |
| 185 | + }; |
| 186 | + |
| 187 | + const wrapper = mountInRouter( |
| 188 | + <UISrefActiveContext.Provider value={() => spy}> |
| 189 | + <Component /> |
| 190 | + </UISrefActiveContext.Provider> |
| 191 | + ); |
| 192 | + |
| 193 | + expect(spy).toBeCalledTimes(0); |
| 194 | + wrapper.unmount(); |
| 195 | + expect(spy).toBeCalledTimes(1); |
| 196 | + }); |
105 | 197 | });
|
106 | 198 | });
|
0 commit comments