Skip to content

Commit a096273

Browse files
SalakarSalakar
authored andcommitted
tests(app): add async storage tests
1 parent 030eea9 commit a096273

8 files changed

Lines changed: 158 additions & 13 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import asyncStorage from '@react-native-async-storage/async-storage';
2+
import {
3+
isMemoryStorage,
4+
memoryStorage,
5+
getItem,
6+
setItem,
7+
removeItem,
8+
setReactNativeAsyncStorageInternal,
9+
prefix,
10+
} from '@react-native-firebase/app/lib/internal/asyncStorage';
11+
12+
describe('firebase.setReactNativeAsyncStorage()', function () {
13+
beforeEach(async function () {
14+
setReactNativeAsyncStorageInternal();
15+
await asyncStorage.clear();
16+
});
17+
18+
it('throws if asyncStorage is not an object', function () {
19+
try {
20+
firebase.setReactNativeAsyncStorage(123);
21+
return Promise.reject(new Error('Did not throw an Error.'));
22+
} catch (error) {
23+
error.message.should.containEql("'asyncStorage' must be an object");
24+
return Promise.resolve();
25+
}
26+
});
27+
28+
it('throws if asyncStorage.setItem is not a function', function () {
29+
try {
30+
firebase.setReactNativeAsyncStorage({ setItem: 123 });
31+
return Promise.reject(new Error('Did not throw an Error.'));
32+
} catch (error) {
33+
error.message.should.containEql("'asyncStorage.setItem' must be a function");
34+
return Promise.resolve();
35+
}
36+
});
37+
38+
it('throws if asyncStorage.getItem is not a function', function () {
39+
try {
40+
firebase.setReactNativeAsyncStorage({ setItem: sinon.spy(), getItem: 123 });
41+
return Promise.reject(new Error('Did not throw an Error.'));
42+
} catch (error) {
43+
error.message.should.containEql("'asyncStorage.getItem' must be a function");
44+
return Promise.resolve();
45+
}
46+
});
47+
48+
it('throws if asyncStorage.removeItem is not a function', function () {
49+
try {
50+
firebase.setReactNativeAsyncStorage({
51+
setItem: sinon.spy(),
52+
getItem: sinon.spy(),
53+
removeItem: 123,
54+
});
55+
return Promise.reject(new Error('Did not throw an Error.'));
56+
} catch (error) {
57+
error.message.should.containEql("'asyncStorage.removeItem' must be a function");
58+
return Promise.resolve();
59+
}
60+
});
61+
62+
it('sets the async storage instance', async function () {
63+
isMemoryStorage().should.equal(true);
64+
65+
const setItemSpy = sinon.spy();
66+
const getItemSpy = sinon.spy();
67+
const removeItemSpy = sinon.spy();
68+
69+
firebase.setReactNativeAsyncStorage({
70+
setItem: setItemSpy,
71+
getItem: getItemSpy,
72+
removeItem: removeItemSpy,
73+
});
74+
75+
isMemoryStorage().should.equal(false);
76+
77+
await setItem('foo', 'bar');
78+
await getItem('foo');
79+
await removeItem('foo');
80+
81+
setItemSpy.should.be.calledOnce();
82+
getItemSpy.should.be.calledOnce();
83+
removeItemSpy.should.be.calledOnce();
84+
});
85+
86+
it('works with @react-native-async-storage/async-storage', async function () {
87+
isMemoryStorage().should.equal(true);
88+
const key = Date.now().toString();
89+
const value = 'bar';
90+
firebase.setReactNativeAsyncStorage(asyncStorage);
91+
isMemoryStorage().should.equal(false);
92+
93+
// Through our internals,
94+
await setItem(key, value);
95+
// Get the value from async-storage, which is prefixed,
96+
const valueFromAsyncStorage = await asyncStorage.getItem(`${prefix}${key}`);
97+
// Get the value from our internals, which is not prefixed,
98+
valueFromAsyncStorage.should.equal(value);
99+
const valueFromInternal = await getItem(key);
100+
valueFromInternal.should.equal(value);
101+
// Values should be identical
102+
valueFromInternal.should.equal(valueFromAsyncStorage);
103+
await removeItem(key);
104+
const valueAfterRemoveInternal = await getItem(key);
105+
const valueAfterRemoveAsyncStorage = await asyncStorage.getItem(`${prefix}${key}`);
106+
should.equal(valueAfterRemoveInternal, null);
107+
should.equal(valueAfterRemoveInternal, valueAfterRemoveAsyncStorage);
108+
});
109+
110+
it('works with memory storage', async function () {
111+
isMemoryStorage().should.equal(true);
112+
const key = Date.now().toString();
113+
const value = 'bar';
114+
await setItem(key, value);
115+
memoryStorage.has(`${prefix}${key}`).should.equal(true);
116+
memoryStorage.get(`${prefix}${key}`).should.equal(value);
117+
(await getItem(key)).should.equal(value);
118+
await removeItem(key);
119+
memoryStorage.has(`${prefix}${key}`).should.equal(false);
120+
should.equal(await getItem(key), null);
121+
});
122+
});

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
},
5959
"dependencies": {
6060
"firebase": "10.12.2",
61-
"opencollective-postinstall": "^2.0.3",
6261
"superstruct": "^0.6.2"
6362
},
6463
"devDependencies": {
64+
"@react-native-async-storage/async-storage": "^1.24.0",
6565
"expo": "^50.0.19"
6666
},
6767
"peerDependenciesMeta": {

tests/macos/Podfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ target 'io.invertase.testing-macOS' do
2020
:app_path => "#{Pod::Config.instance.installation_root}/.."
2121
)
2222

23+
pod 'RNCAsyncStorage', :path => '../node_modules/@react-native-async-storage/async-storage'
24+
2325
post_install do |installer|
2426
react_native_post_install(installer)
2527
end

tests/macos/Podfile.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,8 @@ PODS:
10151015
- React-jsi (= 0.73.32)
10161016
- React-logger (= 0.73.32)
10171017
- React-perflogger (= 0.73.32)
1018+
- RNCAsyncStorage (1.24.0):
1019+
- React-Core
10181020
- SocketRocket (0.7.0)
10191021
- Yoga (1.14.0)
10201022

@@ -1068,6 +1070,7 @@ DEPENDENCIES:
10681070
- React-runtimescheduler (from `../node_modules/react-native-macos/ReactCommon/react/renderer/runtimescheduler`)
10691071
- React-utils (from `../node_modules/react-native-macos/ReactCommon/react/utils`)
10701072
- ReactCommon/turbomodule/core (from `../node_modules/react-native-macos/ReactCommon`)
1073+
- "RNCAsyncStorage (from `../node_modules/@react-native-async-storage/async-storage`)"
10711074
- SocketRocket (from `../node_modules/react-native-macos/third-party-podspecs/SocketRocket.podspec`)
10721075
- Yoga (from `../node_modules/react-native-macos/ReactCommon/yoga`)
10731076

@@ -1166,6 +1169,8 @@ EXTERNAL SOURCES:
11661169
:path: "../node_modules/react-native-macos/ReactCommon/react/utils"
11671170
ReactCommon:
11681171
:path: "../node_modules/react-native-macos/ReactCommon"
1172+
RNCAsyncStorage:
1173+
:path: "../node_modules/@react-native-async-storage/async-storage"
11691174
SocketRocket:
11701175
:podspec: "../node_modules/react-native-macos/third-party-podspecs/SocketRocket.podspec"
11711176
Yoga:
@@ -1219,9 +1224,10 @@ SPEC CHECKSUMS:
12191224
React-runtimescheduler: 508ec4adf3a3a5ffb7b3d10d1f236a2a9022ce5a
12201225
React-utils: 52afeb88f90c5a40fa42a145df7877595302f6cf
12211226
ReactCommon: ddcf7d2e41fbb7d047c169f7c56364e69f5036a8
1227+
RNCAsyncStorage: ec53e44dc3e75b44aa2a9f37618a49c3bc080a7a
12221228
SocketRocket: f6c6249082c011e6de2de60ed641ef8bbe0cfac9
12231229
Yoga: b734a95cb36e2b7e9e7b5733ce679a97e4f37da4
12241230

1225-
PODFILE CHECKSUM: 0e3d4e19f503b3fd21110dfec7c03e5e583c2cd8
1231+
PODFILE CHECKSUM: 719f065f3a2d6d82a8890049d7de13039d11f851
12261232

12271233
COCOAPODS: 1.15.2

tests/macos/io.invertase.testing.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@
176176
);
177177
inputPaths = (
178178
"${PODS_ROOT}/Target Support Files/Pods-io.invertase.testing-macOS/Pods-io.invertase.testing-macOS-resources.sh",
179+
"${PODS_CONFIGURATION_BUILD_DIR}/RNCAsyncStorage/RNCAsyncStorage_resources.bundle",
179180
"${PODS_CONFIGURATION_BUILD_DIR}/React-Core/RCTI18nStrings.bundle",
180181
);
181182
name = "[CP] Copy Pods Resources";
182183
outputPaths = (
184+
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RNCAsyncStorage_resources.bundle",
183185
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RCTI18nStrings.bundle",
184186
);
185187
runOnlyForDeploymentPostprocessing = 0;

tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"prepare": "patch-package"
99
},
1010
"dependencies": {
11+
"@react-native-async-storage/async-storage": "^1.24.0",
1112
"@react-native-firebase/analytics": "20.2.1",
1213
"@react-native-firebase/app": "20.2.1",
1314
"@react-native-firebase/app-check": "20.2.1",

tests/react-native.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ module.exports = {
88
sourceDir: './macos',
99
},
1010
},
11-
};
11+
};

yarn.lock

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5436,6 +5436,17 @@ __metadata:
54365436
languageName: node
54375437
linkType: hard
54385438

5439+
"@react-native-async-storage/async-storage@npm:^1.24.0":
5440+
version: 1.24.0
5441+
resolution: "@react-native-async-storage/async-storage@npm:1.24.0"
5442+
dependencies:
5443+
merge-options: "npm:^3.0.4"
5444+
peerDependencies:
5445+
react-native: ^0.0.0-0 || >=0.60 <1.0
5446+
checksum: 10/5a6b7ac8bd7a9e537a53a3f2301530c284fd885a45ce4a4e0014859bc0f7c89bee5c4b5a6b3740b8d83751561159b237474d18f32fad75ea7d56d4ddb2180d91
5447+
languageName: node
5448+
linkType: hard
5449+
54395450
"@react-native-community/cli-clean@npm:12.3.2":
54405451
version: 12.3.2
54415452
resolution: "@react-native-community/cli-clean@npm:12.3.2"
@@ -5833,9 +5844,9 @@ __metadata:
58335844
version: 0.0.0-use.local
58345845
resolution: "@react-native-firebase/app@workspace:packages/app"
58355846
dependencies:
5847+
"@react-native-async-storage/async-storage": "npm:^1.24.0"
58365848
expo: "npm:^50.0.19"
58375849
firebase: "npm:10.12.2"
5838-
opencollective-postinstall: "npm:^2.0.3"
58395850
superstruct: "npm:^0.6.2"
58405851
peerDependencies:
58415852
expo: ">=47.0.0"
@@ -16072,6 +16083,15 @@ __metadata:
1607216083
languageName: node
1607316084
linkType: hard
1607416085

16086+
"merge-options@npm:^3.0.4":
16087+
version: 3.0.4
16088+
resolution: "merge-options@npm:3.0.4"
16089+
dependencies:
16090+
is-plain-obj: "npm:^2.1.0"
16091+
checksum: 10/d86ddb3dd6e85d558dbf25dc944f3527b6bacb944db3fdda6e84a3f59c4e4b85231095f58b835758b9a57708342dee0f8de0dffa352974a48221487fe9f4584f
16092+
languageName: node
16093+
linkType: hard
16094+
1607516095
"merge-stream@npm:^2.0.0":
1607616096
version: 2.0.0
1607716097
resolution: "merge-stream@npm:2.0.0"
@@ -18015,15 +18035,6 @@ __metadata:
1801518035
languageName: node
1801618036
linkType: hard
1801718037

18018-
"opencollective-postinstall@npm:^2.0.3":
18019-
version: 2.0.3
18020-
resolution: "opencollective-postinstall@npm:2.0.3"
18021-
bin:
18022-
opencollective-postinstall: index.js
18023-
checksum: 10/69d63778087cd10c9d707d9ed360556780cfdd0cd6241ded0e26632f467f1d5a064f4a9aec19a30c187770c17adba034d988f7684b226f3a73e79f44e73fab0e
18024-
languageName: node
18025-
linkType: hard
18026-
1802718038
"optionator@npm:^0.9.3":
1802818039
version: 0.9.3
1802918040
resolution: "optionator@npm:0.9.3"
@@ -19362,6 +19373,7 @@ __metadata:
1936219373
resolution: "react-native-firebase-tests@workspace:tests"
1936319374
dependencies:
1936419375
"@firebase/rules-unit-testing": "npm:^3.0.3"
19376+
"@react-native-async-storage/async-storage": "npm:^1.24.0"
1936519377
"@react-native-firebase/analytics": "npm:20.2.1"
1936619378
"@react-native-firebase/app": "npm:20.2.1"
1936719379
"@react-native-firebase/app-check": "npm:20.2.1"

0 commit comments

Comments
 (0)