-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery-state.js
66 lines (62 loc) · 1.41 KB
/
query-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React from 'react';
import { mount } from 'enzyme';
import { QueryState } from '../src';
import { MemoryRouter, Route } from 'react-router';
const Example = QueryState(({ state, setState }) => (
<div>
<input
value={state.test}
onChange={e => setState({ test: e.target.value })}
/>
</div>
));
test('should render with state from url', () => {
const wrapper = mount(
<MemoryRouter
initialEntries={[{ pathname: '/test', search: '?test=Awesome' }]}
initialIndex={0}
>
<Example />
</MemoryRouter>
);
expect(
wrapper
.find('input')
.first()
.props().value
).toEqual('Awesome');
});
test('should update url state', done => {
const wrapper = mount(
<MemoryRouter
initialEntries={[{ pathname: '/test', search: '?test=Awesome' }]}
initialIndex={0}
>
<div>
<Example />
<Route
path="/test"
render={props => {
if (props.location.search === '?test=testing%20%26%20ampersand%21')
done();
return null;
}}
/>
</div>
</MemoryRouter>
);
wrapper
.find('input')
.first()
.simulate('change', { target: { value: 'testing & ampersand!' } });
setTimeout(
() =>
expect(
wrapper
.find('input')
.first()
.props().value
).toEqual('testing & ampersand!'),
400
);
});