Skip to content

Commit 1e4bf82

Browse files
committed
initial testing setup
1 parent 078335d commit 1e4bf82

14 files changed

+1180
-539
lines changed

__tests__/App-test.tsx

-14
This file was deleted.

__tests__/ProfileScreen.test.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'react-native';
2+
import React from 'react';
3+
import { render } from '../src/utils/test-utils/test-utils';
4+
import ProfileScreen from '../src/Profile/ProfileScreen';
5+
//mocking async storage module
6+
const mockedSetItem = jest.fn();
7+
jest.mock('@react-native-community/async-storage', () => ({
8+
setItem: mockedSetItem,
9+
}));
10+
11+
describe('<ProfileScreen />', () => {
12+
test('renders correctly', () => {
13+
const { debug } = render(<ProfileScreen />);
14+
debug();
15+
});
16+
});

jest.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
preset: 'react-native',
5+
setupFilesAfterEnv: [
6+
'@testing-library/jest-native/extend-expect',
7+
'./src/utils/test-utils/setupTests.ts',
8+
],
9+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
10+
clearMocks: true,
11+
moduleDirectories: ['node_modules', path.join(__dirname, 'src')],
12+
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'],
13+
// moduleNameMapper: {
14+
// '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
15+
// '<rootDir>/assetsTransformer.js',
16+
// '\\.(css|less)$': '<rootDir>/assetsTransformer.js',
17+
// },
18+
};

jest.setup.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
import '@testing-library/jest-native/extend-expect';
3+
4+
// jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

package.json

+6-13
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
"@babel/core": "^7.8.4",
4040
"@babel/runtime": "^7.8.4",
4141
"@react-native-community/eslint-config": "^1.1.0",
42+
"@testing-library/jest-native": "^3.3.0",
43+
"@testing-library/react-native": "^7.0.1",
4244
"@types/d3": "^5.7.2",
43-
"@types/jest": "^25.2.3",
45+
"@types/jest": "^26.0.9",
4446
"@types/react-native": "^0.62.0",
4547
"@types/react-native-vector-icons": "^6.4.5",
4648
"@types/react-test-renderer": "^16.9.2",
@@ -49,21 +51,12 @@
4951
"@typescript-eslint/parser": "^2.27.0",
5052
"babel-jest": "^25.1.0",
5153
"eslint": "^6.5.1",
52-
"jest": "^25.1.0",
54+
"jest": "^26.2.2",
5355
"metro-react-native-babel-preset": "^0.59.0",
56+
"msw": "^0.20.3",
5457
"prettier": "^2.0.4",
5558
"react-test-renderer": "16.13.1",
59+
"ts-jest": "^26.1.4",
5660
"typescript": "^3.8.3"
57-
},
58-
"jest": {
59-
"preset": "react-native",
60-
"moduleFileExtensions": [
61-
"ts",
62-
"tsx",
63-
"js",
64-
"jsx",
65-
"json",
66-
"node"
67-
]
6861
}
6962
}

src/RouteList/RouteContent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {useEffect} from 'react';
22
import { View, Text, StyleSheet, ScrollView } from 'react-native';
33
import { useSelector } from 'react-redux';
44
import { convertLength } from '@turf/helpers';

src/utils/test-utils/data.ts

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
export const routes = [
2+
{
3+
id: 53,
4+
user_id: 'CpAq9BwftlMHCLdLngZZDPEghyM2',
5+
name: 'back and forth',
6+
image:
7+
'https://run-tracker-bucket.s3.us-west-1.amazonaws.com/CpAq9BwftlMHCLdLngZZDPEghyM2/f9d24670-ab67-11ea-baea-63769719b2df.png',
8+
start_point: [-117.740999, 34.10719, 363.61],
9+
end_point: [-117.683988, 34.106866, 414.84],
10+
lines: [
11+
[
12+
[-117.740999, 34.10719, 363.61],
13+
[-117.741, 34.107158, 362.69],
14+
[-117.737908, 34.107104, 365.44],
15+
[-117.737687, 34.107065, 365.59],
16+
[-117.735545, 34.107029, 369.67],
17+
[-117.735547, 34.106959, 369.42],
18+
[-117.732307, 34.10693, 375.5],
19+
[-117.732277, 34.106947, 375.5],
20+
[-117.729301, 34.106942, 379.2],
21+
[-117.729232, 34.106914, 379.17],
22+
[-117.728921, 34.106914, 379.44],
23+
[-117.728842, 34.107101, 380.05],
24+
[-117.726817, 34.107104, 382.62],
25+
],
26+
[
27+
[-117.726817, 34.107104, 382.62],
28+
[-117.725436, 34.107101, 384.74],
29+
[-117.725432, 34.107306, 385.02],
30+
[-117.724574, 34.107294, 387.79],
31+
[-117.724367, 34.107315, 388.27],
32+
[-117.723924, 34.107307, 388.3],
33+
[-117.723923, 34.107254, 388.47],
34+
[-117.723703, 34.107252, 389.47],
35+
[-117.723658, 34.107291, 389.55],
36+
[-117.723416, 34.107293, 389.61],
37+
[-117.723282, 34.107272, 389.65],
38+
[-117.723249, 34.107283, 389.7],
39+
[-117.723225, 34.107338, 389.74],
40+
[-117.723222, 34.107564, 389.75],
41+
[-117.72298, 34.107565, 389.71],
42+
[-117.722935, 34.10763, 390.68],
43+
[-117.722384, 34.107625, 390.88],
44+
[-117.722288, 34.107594, 391.01],
45+
[-117.72158, 34.107594, 391.33],
46+
[-117.721577, 34.107353, 390.66],
47+
[-117.720903, 34.107345, 391.63],
48+
[-117.720787, 34.10737, 392.23],
49+
[-117.720679, 34.107447, 392.77],
50+
[-117.72065, 34.107551, 392.8],
51+
[-117.72065, 34.107695, 392.8],
52+
[-117.720579, 34.107745, 393.28],
53+
[-117.720414, 34.107753, 393.46],
54+
[-117.720417, 34.107883, 393.62],
55+
[-117.720248, 34.10787, 393.53],
56+
[-117.720081, 34.107893, 393.63],
57+
[-117.719945, 34.107963, 393.71],
58+
[-117.719181, 34.107968, 393.76],
59+
[-117.71885, 34.107678, 393.73],
60+
[-117.718741, 34.107558, 393.73],
61+
[-117.718434, 34.107517, 394.16],
62+
[-117.718438, 34.107368, 393.87],
63+
[-117.715075, 34.107343, 396.98],
64+
[-117.714973, 34.107317, 396.96],
65+
[-117.713268, 34.1073, 397],
66+
[-117.71321, 34.107262, 397],
67+
[-117.713178, 34.107146, 397],
68+
[-117.713181, 34.106971, 397],
69+
[-117.709394, 34.106909, 399.39],
70+
],
71+
[
72+
[-117.709394, 34.106909, 399.39],
73+
[-117.702862, 34.106843, 408.3],
74+
[-117.70111, 34.106795, 407.69],
75+
],
76+
[
77+
[-117.70111, 34.106795, 407.69],
78+
[-117.699953, 34.106763, 407.08],
79+
[-117.698547, 34.106742, 406.31],
80+
[-117.698338, 34.106751, 406.98],
81+
[-117.691498, 34.106703, 413.1],
82+
],
83+
[
84+
[-117.691498, 34.106703, 413.1],
85+
[-117.689909, 34.106695, 415.12],
86+
[-117.689724, 34.106769, 415.47],
87+
[-117.687067, 34.106813, 416.33],
88+
[-117.687056, 34.106989, 416.31],
89+
[-117.684047, 34.10702, 415.58],
90+
[-117.684041, 34.106865, 415.03],
91+
[-117.683988, 34.106866, 414.84],
92+
],
93+
],
94+
points: [
95+
[-117.740999, 34.10719, 363.61],
96+
[-117.726817, 34.107104, 382.62],
97+
[-117.709394, 34.106909, 399.39],
98+
[-117.70111, 34.106795, 407.69],
99+
[-117.691498, 34.106703, 413.1],
100+
[-117.683988, 34.106866, 414.84],
101+
],
102+
sports: ['walk'],
103+
surfaces: ['trail'],
104+
city: 'Pomona',
105+
state: 'California',
106+
created_at: '2020-06-10T22:16:05.271Z',
107+
distance: '5526.183',
108+
},
109+
{
110+
id: 54,
111+
user_id: 'CpAq9BwftlMHCLdLngZZDPEghyM2',
112+
name: 'new route',
113+
image:
114+
'https://run-tracker-bucket.s3.us-west-1.amazonaws.com/CpAq9BwftlMHCLdLngZZDPEghyM2/d23ee3a0-ab7d-11ea-baea-63769719b2df.png',
115+
start_point: [-117.736883, 34.101469, 353.53],
116+
end_point: [-117.736788, 34.095001, 341.73],
117+
lines: [
118+
[
119+
[-117.736883, 34.101469, 353.53],
120+
[-117.736887, 34.102982, 361.39],
121+
[-117.736763, 34.103084, 362],
122+
[-117.736755, 34.10401, 361.57],
123+
[-117.73661, 34.104011, 361.39],
124+
[-117.736594, 34.105717, 363.49],
125+
[-117.736774, 34.105697, 363.63],
126+
[-117.7369, 34.105693, 363.76],
127+
[-117.73691, 34.107053, 366.58],
128+
[-117.736868, 34.107098, 366.77],
129+
],
130+
[
131+
[-117.736868, 34.107098, 366.77],
132+
[-117.73691, 34.107053, 366.58],
133+
[-117.735545, 34.107029, 369.67],
134+
[-117.735547, 34.106959, 369.42],
135+
[-117.732307, 34.10693, 375.5],
136+
[-117.732277, 34.106947, 375.5],
137+
[-117.729301, 34.106942, 379.2],
138+
[-117.729232, 34.106914, 379.17],
139+
[-117.728921, 34.106914, 379.44],
140+
[-117.728842, 34.107101, 380.05],
141+
[-117.722446, 34.107093, 388.64],
142+
[-117.721574, 34.107105, 389.28],
143+
[-117.720838, 34.107135, 390.53],
144+
],
145+
[
146+
[-117.720838, 34.107135, 390.53],
147+
[-117.720388, 34.107154, 391.77],
148+
[-117.718425, 34.107161, 393.08],
149+
[-117.718438, 34.107368, 393.87],
150+
[-117.715075, 34.107343, 396.98],
151+
[-117.714973, 34.107317, 396.96],
152+
[-117.713268, 34.1073, 397],
153+
[-117.71321, 34.107262, 397],
154+
[-117.713178, 34.107146, 397],
155+
[-117.713181, 34.106971, 397],
156+
[-117.712771, 34.106967, 396.47],
157+
],
158+
[
159+
[-117.712771, 34.106967, 396.47],
160+
[-117.713181, 34.106971, 397],
161+
[-117.713189, 34.106186, 396.03],
162+
[-117.714275, 34.106186, 396.37],
163+
[-117.714294, 34.1044, 390.65],
164+
[-117.714939, 34.104397, 390.69],
165+
[-117.714925, 34.09966, 376.41],
166+
[-117.714788, 34.09966, 376.29],
167+
],
168+
[
169+
[-117.714788, 34.09966, 376.29],
170+
[-117.71998, 34.099646, 371.83],
171+
],
172+
[
173+
[-117.71998, 34.099646, 371.83],
174+
[-117.722434, 34.099641, 366.97],
175+
[-117.722423, 34.098553, 363.84],
176+
[-117.721995, 34.098539, 365],
177+
],
178+
[
179+
[-117.721995, 34.098539, 365],
180+
[-117.724039, 34.098583, 363.28],
181+
[-117.725411, 34.098591, 361.08],
182+
[-117.727361, 34.098628, 360.27],
183+
[-117.72736, 34.098688, 360.53],
184+
[-117.728721, 34.098712, 358.23],
185+
[-117.728707, 34.098642, 358.26],
186+
[-117.732435, 34.098726, 355.44],
187+
[-117.734052, 34.098779, 353.73],
188+
],
189+
[
190+
[-117.734052, 34.098779, 353.73],
191+
[-117.736731, 34.098856, 349.98],
192+
[-117.736716, 34.096333, 347],
193+
[-117.736786, 34.096183, 346.21],
194+
[-117.736788, 34.095001, 341.73],
195+
],
196+
],
197+
points: [
198+
[-117.736883, 34.101469, 353.53],
199+
[-117.736868, 34.107098, 366.77],
200+
[-117.720838, 34.107135, 390.53],
201+
[-117.712771, 34.106967, 396.47],
202+
[-117.714788, 34.09966, 376.29],
203+
[-117.71998, 34.099646, 371.83],
204+
[-117.721995, 34.098539, 365],
205+
[-117.734052, 34.098779, 353.73],
206+
[-117.736788, 34.095001, 341.73],
207+
],
208+
sports: ['bike'],
209+
surfaces: ['unpaved'],
210+
city: 'Pomona',
211+
state: 'California',
212+
created_at: '2020-06-11T00:52:27.864Z',
213+
distance: '6678.942',
214+
},
215+
];

src/utils/test-utils/server.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { rest } from 'msw';
2+
import { setupServer } from 'msw/node';
3+
import { handlers } from './serverHandlers';
4+
5+
const server = setupServer(...handlers);
6+
7+
export { server, rest };
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { rest } from 'msw';
2+
import { routes } from './data';
3+
4+
const handlers = [
5+
rest.get('/api/routes', (req, res, ctx) => {
6+
return res(ctx.json({ routes, units: 'miles' }));
7+
}),
8+
];
9+
10+
export { handlers };

src/utils/test-utils/setupTests.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { server } from './server';
2+
beforeAll(() => server.listen());
3+
// if you need to add a handler after calling setupServer for some specific test
4+
// this will remove that handler for the rest of them
5+
// (which is important for test isolation):
6+
afterEach(() => server.resetHandlers());
7+
afterAll(() => server.close());

src/utils/test-utils/test-utils.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import { configureStore } from '@reduxjs/toolkit';
3+
import { render, RenderOptions } from '@testing-library/react-native';
4+
import { rootReducer } from '../../redux/rootReducer';
5+
import { Provider } from 'react-redux';
6+
7+
const customRender = (
8+
ui: React.ReactElement,
9+
preloadedState = {},
10+
options?: Omit<RenderOptions, 'queries'>,
11+
) => {
12+
const WithAllProviders = ({ children }: { children: React.ReactElement }) => {
13+
const configStore = configureStore({
14+
reducer: rootReducer,
15+
preloadedState,
16+
});
17+
18+
return <Provider store={configStore}>{children}</Provider>;
19+
};
20+
21+
return render(ui, { wrapper: WithAllProviders, ...options });
22+
};
23+
24+
// re-export everything
25+
export * from '@testing-library/react-native';
26+
27+
// override render method
28+
export { customRender as render };

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"node_modules",
6262
"babel.config.js",
6363
"metro.config.js",
64-
"jest.config.js"
64+
"jest.config.js",
65+
"utils/test-utils"
6566
]
6667
}

0 commit comments

Comments
 (0)