Skip to content

Commit 837b1b1

Browse files
author
Krzysztof Borowy
committed
tests: move tests to root, native module mock, ci
1 parent e21c604 commit 837b1b1

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed

.circleci/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ jobs:
137137
name: Flow check
138138
command: yarn test:flow
139139

140+
"Test: units":
141+
<<: *js_defaults
142+
steps:
143+
- *addWorkspace
144+
- run:
145+
name: Unit tests
146+
command: yarn test:units
147+
140148
"Test: iOS e2e":
141149
<<: *macos_defaults
142150
steps:
@@ -310,10 +318,12 @@ workflows:
310318
requires:
311319
- "Test: lint"
312320
- "Test: flow"
321+
- "Test: units"
313322
- "Build: Android release apk":
314323
requires:
315324
- "Test: lint"
316325
- "Test: flow"
326+
- "Test: units"
317327
# - "Test: Android e2e":
318328
# requires:
319329
# - "Test: lint"

example/jest.setup.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"test": "yarn test:lint && yarn test:flow",
5050
"test:flow": "flow check",
5151
"test:lint": "eslint src/**/*.js example/**/*.js jest/*.js",
52+
"test:unit": "jest",
5253
"test:e2e:ios": "detox test -c ios",
5354
"test:e2e:android": "detox test -c android",
5455
"test:e2e:macos": "scripts/run_macos_e2e.sh 'test'"
@@ -86,10 +87,7 @@
8687
"react-test-renderer": "16.9.0"
8788
},
8889
"jest": {
89-
"preset": "react-native",
90-
"setupFiles": [
91-
"./example/jest.setup.js"
92-
]
90+
"preset": "react-native"
9391
},
9492
"detox": {
9593
"test-runner": "jest",

src/__mocks__/RCTAsyncStorage.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @format
3+
* @flow
4+
*/
5+
import merge from 'deep-assign';
6+
7+
let __storage = {};
8+
9+
const AsyncStorageNativeModuleFake = {
10+
multiGet(keys: string[], cb: (?Object, ?(string[][])) => void) {
11+
if (!keys) {
12+
cb({message: 'No keys provided'});
13+
}
14+
15+
const keysValues = keys.reduce(
16+
(acc, key) => [...acc, [key, __storage[key]]],
17+
[],
18+
);
19+
20+
// error, value
21+
cb(null, keysValues);
22+
},
23+
24+
multiSet(keyValues: string[][], cb: (?Object) => void) {
25+
if (!keyValues) {
26+
cb({message: 'no keys provided'});
27+
}
28+
try {
29+
keyValues.forEach(pair => {
30+
if (!pair[0] === undefined || !pair[1] === undefined) {
31+
throw 'pairs not matching';
32+
}
33+
__storage[pair[0]] = pair[1];
34+
});
35+
} catch (e) {
36+
cb({message: e});
37+
}
38+
cb();
39+
},
40+
41+
multiRemove(keys: string[], cb: (?Object) => void) {
42+
if (!keys) {
43+
cb({message: 'keys not provided'});
44+
}
45+
keys.forEach(k => {
46+
delete __storage[k];
47+
});
48+
49+
cb();
50+
},
51+
52+
multiMerge(keyValues: string[], cb: (?Object) => void) {
53+
if (!keyValues) {
54+
cb({message: 'keys not provided'});
55+
}
56+
57+
try {
58+
keyValues.forEach(pair => {
59+
if (!pair[0] || !pair[1]) {
60+
throw 'pairs not matching';
61+
}
62+
63+
const storedValue = __storage[pair[0]];
64+
const merged = merge({}, JSON.parse(storedValue), JSON.parse(pair[1]));
65+
66+
__storage[pair[0]] = JSON.stringify(merged);
67+
});
68+
69+
cb();
70+
} catch (e) {
71+
cb({message: e});
72+
}
73+
74+
cb();
75+
},
76+
77+
clear(cb: (?Object) => void) {
78+
__storage = {};
79+
80+
cb();
81+
},
82+
};
83+
84+
export default AsyncStorageNativeModuleFake;

example/__tests__/App.js renamed to src/__tests__/AsyncStorage.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
/* eslint-disable no-shadow */
55

66
import 'react-native';
7+
import AsyncStorage from '../AsyncStorage.native';
78

8-
import AsyncStorage from '@react-native-community/async-storage';
9+
jest.mock('../RCTAsyncStorage');
910

1011
describe('Async Storage mock functionality', () => {
1112
describe('Promise based', () => {
1213
it('can read/write data to/from storage', async () => {
13-
const newData = Math.floor(Math.random() * 1000);
14+
const newData = String(Math.floor(Math.random() * 1000));
1415

1516
await AsyncStorage.setItem('key', newData);
1617

@@ -20,7 +21,7 @@ describe('Async Storage mock functionality', () => {
2021
});
2122

2223
it('can clear storage', async () => {
23-
await AsyncStorage.setItem('temp_key', Math.random() * 1000);
24+
await AsyncStorage.setItem('temp_key', String(Math.random() * 1000));
2425

2526
let currentValue = await AsyncStorage.getItem('temp_key');
2627

@@ -34,8 +35,8 @@ describe('Async Storage mock functionality', () => {
3435
});
3536

3637
it('can clear entries in storage', async () => {
37-
await AsyncStorage.setItem('random1', Math.random() * 1000);
38-
await AsyncStorage.setItem('random2', Math.random() * 1000);
38+
await AsyncStorage.setItem('random1', String(Math.random() * 1000));
39+
await AsyncStorage.setItem('random2', String(Math.random() * 1000));
3940

4041
let data1 = await AsyncStorage.getItem('random1');
4142
let data2 = await AsyncStorage.getItem('random2');
@@ -80,7 +81,7 @@ describe('Async Storage mock functionality', () => {
8081

8182
describe('Callback based', () => {
8283
it('can read/write data to/from storage', done => {
83-
const newData = Math.floor(Math.random() * 1000);
84+
const newData = String(Math.floor(Math.random() * 1000));
8485

8586
AsyncStorage.setItem('key', newData, function() {
8687
AsyncStorage.getItem('key', function(_, value) {
@@ -90,7 +91,7 @@ describe('Async Storage mock functionality', () => {
9091
});
9192
});
9293
it('can clear storage', done => {
93-
AsyncStorage.setItem('temp_key', Math.random() * 1000, () => {
94+
AsyncStorage.setItem('temp_key', String(Math.random() * 1000), () => {
9495
AsyncStorage.getItem('temp_key', (_, currentValue) => {
9596
expect(currentValue).not.toBeNull();
9697
AsyncStorage.clear(() => {
@@ -104,8 +105,8 @@ describe('Async Storage mock functionality', () => {
104105
});
105106

106107
it('can clear entries in storage', done => {
107-
AsyncStorage.setItem('random1', Math.random() * 1000, () => {
108-
AsyncStorage.setItem('random2', Math.random() * 1000, () => {
108+
AsyncStorage.setItem('random1', String(Math.random() * 1000), () => {
109+
AsyncStorage.setItem('random2', String(Math.random() * 1000), () => {
109110
AsyncStorage.getItem('random1', (_, data1) => {
110111
AsyncStorage.getItem('random2', (_, data2) => {
111112
expect(data1).not.toBeNull();

0 commit comments

Comments
 (0)