Skip to content

Commit 14aca40

Browse files
Cleanup integration tests (#190)
1 parent 55e4af4 commit 14aca40

File tree

1 file changed

+111
-148
lines changed

1 file changed

+111
-148
lines changed

src/__tests__/integration.test.tsx

Lines changed: 111 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { mount } from 'enzyme';
2-
import * as historyHelper from 'history';
2+
import { createMemoryHistory } from 'history';
33
import React from 'react';
44
import { defaultRegistry } from 'react-sweet-state';
55

6-
import { mockRoute } from '../common/mocks';
7-
import { ResourceStore } from '../controllers/resource-store';
6+
import { isServerEnvironment } from '../common/utils/is-server-environment';
7+
import { createResource, ResourceStore } from '../controllers/resource-store';
88
import {
99
RouteComponent,
1010
Router,
@@ -13,150 +13,108 @@ import {
1313
StaticRouter,
1414
} from '../index';
1515

16-
const mockLocation = {
17-
pathname: '/projects/123/board/456',
18-
search: '?foo=hello&bar=world',
19-
hash: '#hash',
20-
};
16+
jest.mock('../common/utils/is-server-environment');
2117

22-
const mockRoutes = [
23-
{
24-
path: '/projects/:projectId/board/:boardId',
25-
component: () => null,
26-
name: '',
27-
},
28-
{
29-
path: '/anotherpath',
30-
component: () => null,
31-
name: '',
32-
},
33-
];
34-
35-
const resolver = (resolveWith: any, delay = 0) =>
36-
new Promise(resolve => setTimeout(() => resolve(resolveWith), delay));
37-
38-
const mockResource = {
39-
type: 'type',
40-
getKey: () => 'entry',
41-
getData: () => Promise.resolve('mock-data'),
42-
maxAge: 0,
43-
maxCache: Infinity,
44-
isBrowserOnly: false,
45-
depends: null,
46-
};
47-
48-
const historyBuildOptions = {
49-
initialEntries: [
50-
`${mockLocation.pathname}${mockLocation.search}${mockLocation.hash}`,
51-
],
52-
};
53-
54-
let history = historyHelper.createMemoryHistory(historyBuildOptions);
55-
let historyPushSpy = jest.spyOn(history, 'push');
56-
let historyReplaceSpy = jest.spyOn(history, 'replace');
57-
const nextTick = () => new Promise(resolve => setTimeout(resolve));
58-
59-
describe('<Router /> integration tests', () => {
18+
describe('<Router /> client-side integration tests', () => {
6019
beforeEach(() => {
61-
history = historyHelper.createMemoryHistory(historyBuildOptions);
62-
historyPushSpy = jest.spyOn(history, 'push');
63-
historyReplaceSpy = jest.spyOn(history, 'replace');
20+
(isServerEnvironment as any).mockReturnValue(false);
6421
});
6522

6623
afterEach(() => {
6724
defaultRegistry.stores.clear();
68-
jest.resetAllMocks();
69-
jest.restoreAllMocks();
7025
});
7126

72-
it('should send the right path to history API', async () => {
73-
let _routerActions = {} as RouterActionsType;
74-
mount(
75-
<Router routes={mockRoutes} history={history}>
76-
<RouterActions>
77-
{routerActions => {
78-
_routerActions = routerActions;
79-
80-
return null;
81-
}}
82-
</RouterActions>
83-
</Router>
84-
);
27+
describe('sends the expected path to history', () => {
28+
function renderRouter(basePath?: string) {
29+
const history = createMemoryHistory();
30+
const push = jest.spyOn(history, 'push');
31+
const replace = jest.spyOn(history, 'replace');
32+
33+
let routerActions = {} as RouterActionsType;
34+
mount(
35+
<Router basePath={basePath} history={history} routes={[]}>
36+
<RouterActions>
37+
{actions => {
38+
routerActions = actions;
39+
40+
return null;
41+
}}
42+
</RouterActions>
43+
</Router>
44+
);
45+
46+
return {
47+
history: {
48+
push,
49+
replace,
50+
},
51+
routerActions,
52+
};
53+
}
8554

86-
_routerActions.push('/hello');
87-
await nextTick();
88-
expect(historyPushSpy).toBeCalledWith(`/hello`);
55+
it('when basePath is set', async () => {
56+
const { history, routerActions } = renderRouter('/basepath');
8957

90-
_routerActions.replace('/world');
91-
await nextTick();
92-
expect(historyReplaceSpy).toBeCalledWith(`/world`);
93-
});
58+
routerActions.push('/push');
59+
expect(history.push).toHaveBeenCalledWith('/basepath/push');
9460

95-
it('should send the right path to history API when basePath is set', async () => {
96-
let _routerActions = {} as RouterActionsType;
97-
mount(
98-
<Router routes={mockRoutes} history={history} basePath="/base">
99-
<RouterActions>
100-
{routerActions => {
101-
_routerActions = routerActions;
102-
103-
return null;
104-
}}
105-
</RouterActions>
106-
</Router>
107-
);
61+
routerActions.replace('/replace');
62+
expect(history.replace).toHaveBeenCalledWith('/basepath/replace');
63+
});
64+
65+
it('when basePath is not set', async () => {
66+
const { history, routerActions } = renderRouter();
10867

109-
_routerActions.push('/hello');
110-
await nextTick();
111-
expect(historyPushSpy).toBeCalledWith(`/base/hello`);
68+
routerActions.push('/push');
69+
expect(history.push).toBeCalledWith('/push');
11270

113-
_routerActions.replace('/world');
114-
await nextTick();
115-
expect(historyReplaceSpy).toBeCalledWith(`/base/world`);
71+
routerActions.replace('/replace');
72+
expect(history.replace).toBeCalledWith('/replace');
73+
});
11674
});
11775

118-
it('should re-trigger requests for timed out resources when mounted', async () => {
119-
const completedResource = {
120-
...mockResource,
121-
...{ type: 'HI', getData: () => resolver('hello world', 250) },
122-
};
123-
const getCompletedDataSpy = jest.spyOn(completedResource, 'getData');
76+
it('re-triggers requests for timed out resources when mounted', async () => {
77+
const resolver = (resolveWith: any, delay = 0) =>
78+
new Promise(resolve => setTimeout(() => resolve(resolveWith), delay));
12479

125-
const timeoutResource = {
126-
...mockResource,
127-
...{
128-
type: 'BYE',
129-
getData: () => resolver('goodbye cruel world', 500),
130-
},
80+
const completedResource = createResource({
81+
getKey: () => 'key',
82+
getData: () => resolver('completed', 250),
83+
type: 'COMPLETED',
84+
});
85+
86+
const timeoutResource = createResource({
87+
getKey: () => 'key',
88+
getData: () => resolver('timeout', 500),
89+
type: 'TIMEOUT',
90+
});
91+
92+
const location = '/pathname?search=search#hash=hash';
93+
const getCompletedData = jest.spyOn(completedResource, 'getData');
94+
const getTimeoutData = jest.spyOn(timeoutResource, 'getData');
95+
96+
const route = {
97+
component: () => <div>foo</div>,
98+
name: 'mock-route',
99+
path: location.substring(0, location.indexOf('?')),
100+
resources: [completedResource, timeoutResource],
131101
};
132-
const getTimeoutDataSpy = jest.spyOn(timeoutResource, 'getData');
133-
134-
const mockedRoutes = [
135-
{
136-
...mockRoute,
137-
name: 'mock-route',
138-
path: mockLocation.pathname,
139-
component: () => <div>foo</div>,
140-
resources: [completedResource, timeoutResource],
141-
},
142-
];
143102

144103
const serverData = await StaticRouter.requestResources({
145-
// @ts-ignore
146-
routes: mockedRoutes,
147-
location: mockLocation.pathname,
104+
location,
105+
routes: [route],
148106
timeout: 350,
149107
});
150108

151-
expect(getCompletedDataSpy).toHaveBeenCalledTimes(1);
152-
expect(getTimeoutDataSpy).toHaveBeenCalledTimes(1);
109+
expect(getCompletedData).toHaveBeenCalledTimes(1);
110+
expect(getTimeoutData).toHaveBeenCalledTimes(1);
153111

154112
expect(serverData).toEqual({
155-
BYE: {
156-
entry: {
113+
TIMEOUT: {
114+
key: {
157115
data: null,
158116
error: {
159-
message: 'Resource timed out: BYE',
117+
message: 'Resource timed out: TIMEOUT',
160118
name: 'TimeoutError',
161119
stack: expect.any(String),
162120
},
@@ -167,9 +125,9 @@ describe('<Router /> integration tests', () => {
167125
accessedAt: null,
168126
},
169127
},
170-
HI: {
171-
entry: {
172-
data: 'hello world',
128+
COMPLETED: {
129+
key: {
130+
data: 'completed',
173131
error: null,
174132
expiresAt: null,
175133
key: undefined,
@@ -183,31 +141,33 @@ describe('<Router /> integration tests', () => {
183141
defaultRegistry.stores.clear();
184142

185143
jest.useFakeTimers();
144+
186145
mount(
187-
<Router routes={mockedRoutes} history={history} resourceData={serverData}>
188-
<RouterActions>
189-
{() => {
190-
return null;
191-
}}
192-
</RouterActions>
193-
</Router>
146+
<Router
147+
history={createMemoryHistory({
148+
initialEntries: [location],
149+
})}
150+
resourceData={serverData}
151+
routes={[route]}
152+
/>
194153
);
154+
195155
jest.runAllTimers();
196156

197157
// Await a fake promise to let route resources to complete
198158
await Promise.resolve();
199159

200-
expect(getCompletedDataSpy).toHaveBeenCalledTimes(1);
201-
expect(getTimeoutDataSpy).toHaveBeenCalledTimes(2);
160+
expect(getCompletedData).toHaveBeenCalledTimes(1);
161+
expect(getTimeoutData).toHaveBeenCalledTimes(2);
202162

203163
const resourceStore = defaultRegistry.getStore(ResourceStore);
204164

205165
expect(resourceStore.storeState.getState()).toEqual({
206166
context: {},
207167
data: {
208-
BYE: {
209-
entry: {
210-
data: 'goodbye cruel world',
168+
TIMEOUT: {
169+
key: {
170+
data: 'timeout',
211171
error: null,
212172
expiresAt: expect.any(Number),
213173
key: undefined,
@@ -216,9 +176,9 @@ describe('<Router /> integration tests', () => {
216176
accessedAt: expect.any(Number),
217177
},
218178
},
219-
HI: {
220-
entry: {
221-
data: 'hello world',
179+
COMPLETED: {
180+
key: {
181+
data: 'completed',
222182
error: null,
223183
expiresAt: expect.any(Number),
224184
key: undefined,
@@ -234,35 +194,38 @@ describe('<Router /> integration tests', () => {
234194
});
235195
});
236196

237-
describe('<StaticRouter /> integration tests', () => {
238-
const basePath = '/base';
197+
describe('<StaticRouter /> server-side integration tests', () => {
239198
const route = {
240-
path: '/anotherpath',
241-
component: () => <>important</>,
199+
component: () => <>route component</>,
242200
name: '',
201+
path: '/path',
243202
};
244203

204+
beforeEach(() => {
205+
(isServerEnvironment as any).mockReturnValue(true);
206+
});
207+
245208
it('should match the right route when basePath is set', async () => {
246209
const wrapper = mount(
247210
<StaticRouter
211+
basePath="/basepath"
212+
location={`/basepath${route.path}`}
248213
routes={[route]}
249-
location={`${basePath}${route.path}`}
250-
basePath={basePath}
251214
>
252215
<RouteComponent />
253216
</StaticRouter>
254217
);
255218

256-
expect(wrapper.text()).toBe('important');
219+
expect(wrapper.text()).toBe('route component');
257220
});
258221

259222
it('should match the right route when basePath is not set', async () => {
260223
const wrapper = mount(
261-
<StaticRouter routes={[route]} location={route.path}>
224+
<StaticRouter location={route.path} routes={[route]}>
262225
<RouteComponent />
263226
</StaticRouter>
264227
);
265228

266-
expect(wrapper.text()).toBe('important');
229+
expect(wrapper.text()).toBe('route component');
267230
});
268231
});

0 commit comments

Comments
 (0)