Skip to content

Commit e46e8c6

Browse files
committed
test: more cases
1 parent 0342c95 commit e46e8c6

File tree

3 files changed

+129
-33
lines changed

3 files changed

+129
-33
lines changed

examples/common-tests/src/tests/BasicTests.tsx

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function useBasicTest(storage: AsyncStorage) {
8686
}
8787
};
8888

89-
const saveBigData = async () => {
89+
const testSavingBigData = async () => {
9090
try {
9191
tests.clear();
9292
const key = "big-data";
@@ -104,7 +104,7 @@ export function useBasicTest(storage: AsyncStorage) {
104104
}
105105
};
106106

107-
const clearStorage = async () => {
107+
const testClearingStorage = async () => {
108108
try {
109109
tests.clear();
110110
await storage.clear();
@@ -122,6 +122,88 @@ export function useBasicTest(storage: AsyncStorage) {
122122
}
123123
};
124124

125+
const testRemoveNonExistentKeys = async () => {
126+
try {
127+
tests.clear();
128+
await storage.clear();
129+
130+
const missingKeys = ["missing1", "missing2"];
131+
tests.info(`Attempting to remove non-existent keys: ${missingKeys}`);
132+
await storage.removeMany(missingKeys);
133+
tests.assert(
134+
[],
135+
await storage.getAllKeys(),
136+
"Removing missing keys is safe"
137+
);
138+
} catch (e) {
139+
tests.report(e);
140+
}
141+
};
142+
143+
const testConcurrentSetAndGet = async () => {
144+
try {
145+
tests.clear();
146+
await storage.clear();
147+
148+
const key1 = "concurrent1";
149+
const key2 = "concurrent2";
150+
const key3 = "concurrent3";
151+
const key4 = "concurrent4";
152+
const key5 = "concurrent5";
153+
154+
tests.info("Setting multiple keys concurrently");
155+
await Promise.all([
156+
storage.setItem(key1, key1),
157+
storage.setItem(key2, key2),
158+
storage.setItem(key3, key3),
159+
storage.setItem(key4, key4),
160+
storage.setItem(key5, key5),
161+
]);
162+
163+
const values = await storage.getMany([key1, key2, key3, key4, key5]);
164+
tests.assert(
165+
{
166+
[key1]: key1,
167+
[key2]: key2,
168+
[key3]: key3,
169+
[key4]: key4,
170+
[key5]: key5,
171+
},
172+
values,
173+
"Concurrent set/get works"
174+
);
175+
} catch (e) {
176+
tests.report(e);
177+
}
178+
};
179+
180+
const testLargeNumberOfKeys = async () => {
181+
try {
182+
tests.clear();
183+
await storage.clear();
184+
185+
const entries: Record<string, string> = {};
186+
for (let i = 0; i < 1000; i++) {
187+
entries[`key-${i}`] = `value-${i}`;
188+
}
189+
190+
tests.info("Saving 1000 keys at once");
191+
await storage.setMany(entries);
192+
193+
const keys = await storage.getAllKeys();
194+
tests.assert(
195+
Object.keys(entries).sort(),
196+
keys.sort(),
197+
"All keys are stored"
198+
);
199+
200+
const fetched = await storage.getMany(Object.keys(entries));
201+
tests.assert(entries, fetched, "All values match");
202+
} catch (e) {
203+
tests.report(e);
204+
}
205+
};
206+
125207
return {
126208
logs: tests.logs,
127209
clearLogs: tests.clear,
@@ -136,11 +218,23 @@ export function useBasicTest(storage: AsyncStorage) {
136218
},
137219
{
138220
name: "Big data set",
139-
run: saveBigData,
221+
run: testSavingBigData,
140222
},
141223
{
142224
name: "Storage clearance",
143-
run: clearStorage,
225+
run: testClearingStorage,
226+
},
227+
{
228+
name: "Safe removal",
229+
run: testRemoveNonExistentKeys,
230+
},
231+
{
232+
name: "Concurrent set and get",
233+
run: testConcurrentSetAndGet,
234+
},
235+
{
236+
name: "Test large number of keys",
237+
run: testLargeNumberOfKeys,
144238
},
145239
],
146240
};

examples/react-native/src/tests/BasicTests.tsx

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,44 @@ const BasicTests: React.FC<Props> = ({ storage }) => {
1212

1313
return (
1414
<View style={{ paddingHorizontal: 16, flex: 1 }}>
15-
<View style={{ gap: 8 }}>
15+
<View style={{ gap: 8, flexDirection: "row", flexWrap: "wrap" }}>
1616
{tests.map((test) => {
1717
return (
1818
<Button key={test.name} title={test.name} onPress={test.run} />
1919
);
2020
})}
2121
</View>
2222

23-
<View style={{ width: "100%", alignItems: "flex-end" }}>
24-
<Pressable
25-
onPress={clearLogs}
26-
style={({ pressed }) => [
27-
{
28-
padding: 12,
29-
width: 90,
30-
backgroundColor: pressed ? "rgb(210, 230, 255)" : "transparent",
31-
},
32-
]}
33-
>
34-
<Text>clear logs</Text>
35-
</Pressable>
36-
</View>
37-
38-
<ScrollView contentContainerStyle={{ gap: 12 }}>
39-
{logs.map((l, i) => (
40-
<Text
41-
style={{
42-
fontSize: 14,
43-
color: l.type === "ok" ? "green" : l.type === "err" ? "red" : "",
44-
}}
45-
key={i}
23+
<View style={{ width: "100%", flex: 1 }}>
24+
<View style={{ width: "100%", alignItems: "flex-end" }}>
25+
<Pressable
26+
onPress={clearLogs}
27+
style={({ pressed }) => [
28+
{
29+
padding: 12,
30+
width: 90,
31+
backgroundColor: pressed ? "rgb(210, 230, 255)" : "transparent",
32+
},
33+
]}
4634
>
47-
{l.message}
48-
</Text>
49-
))}
50-
</ScrollView>
35+
<Text>clear logs</Text>
36+
</Pressable>
37+
</View>
38+
<ScrollView contentContainerStyle={{ gap: 12 }}>
39+
{logs.map((l, i) => (
40+
<Text
41+
style={{
42+
fontSize: 14,
43+
color:
44+
l.type === "ok" ? "green" : l.type === "err" ? "red" : "",
45+
}}
46+
key={i}
47+
>
48+
{l.message}
49+
</Text>
50+
))}
51+
</ScrollView>
52+
</View>
5153
</View>
5254
);
5355
};

examples/web/src/tests/BasicTests.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const BasicTests: React.FC<Props> = ({ storage }) => {
1111

1212
return (
1313
<div className="flex flex-col items-center px-4">
14-
<div className="flex flex-col max-w-64 items-center gap-2">
14+
<div className="flex flex-row flex-wrap gap-2">
1515
{tests.map((test) => {
1616
return (
1717
<button

0 commit comments

Comments
 (0)