Skip to content

Commit 5f6d6a0

Browse files
authored
fix: add type definition for jest mock (#708)
1 parent a9613c3 commit 5f6d6a0

File tree

1 file changed

+74
-68
lines changed

1 file changed

+74
-68
lines changed

types/index.d.ts

+74-68
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
// CREDITS: This types are based on the original work made by all the people who contributed to @types/react-native
22

3+
interface AsyncStorage {
4+
/**
5+
* Fetches key and passes the result to callback, along with an Error if there is any.
6+
*/
7+
getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null>;
8+
9+
/**
10+
* Sets value for key and calls callback on completion, along with an Error if there is any
11+
*/
12+
setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;
13+
14+
removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;
15+
16+
/**
17+
* Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
18+
* Not supported by all native implementation
19+
*/
20+
mergeItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;
21+
22+
/**
23+
* Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
24+
* Use removeItem or multiRemove to clear only your own keys instead.
25+
*/
26+
clear(callback?: (error?: Error) => void): Promise<void>;
27+
28+
/**
29+
* Gets all keys known to the app, for all callers, libraries, etc
30+
*/
31+
getAllKeys(callback?: (error?: Error, keys?: string[]) => void): Promise<string[]>;
32+
33+
/**
34+
* multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
35+
*/
36+
multiGet(
37+
keys: string[],
38+
callback?: (errors?: Error[], result?: [string, string | null][]) => void
39+
): Promise<[string, string | null][]>;
40+
41+
/**
42+
* multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
43+
*
44+
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
45+
*/
46+
multiSet(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
47+
48+
/**
49+
* Delete all the keys in the keys array.
50+
*/
51+
multiRemove(keys: string[], callback?: (errors?: Error[]) => void): Promise<void>;
52+
53+
/**
54+
* Merges existing values with input values, assuming they are stringified json.
55+
* Returns a Promise object.
56+
*
57+
* Not supported by all native implementations.
58+
*/
59+
multiMerge(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
60+
}
61+
62+
type AsyncStorageHook = {
63+
getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>;
64+
setItem(value: string, callback?: (error?: Error) => void): Promise<void>;
65+
mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>;
66+
removeItem(callback?: (error?: Error) => void): Promise<void>;
67+
}
68+
369
declare module '@react-native-async-storage/async-storage' {
470
/**
571
* AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage
@@ -16,73 +82,13 @@ declare module '@react-native-async-storage/async-storage' {
1682
*
1783
* @see https://react-native-async-storage.github.io/async-storage/docs/api
1884
*/
19-
export interface AsyncStorageStatic {
20-
/**
21-
* Fetches key and passes the result to callback, along with an Error if there is any.
22-
*/
23-
getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null>;
24-
25-
/**
26-
* Sets value for key and calls callback on completion, along with an Error if there is any
27-
*/
28-
setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;
29-
30-
removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;
31-
32-
/**
33-
* Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
34-
* Not supported by all native implementation
35-
*/
36-
mergeItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;
37-
38-
/**
39-
* Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
40-
* Use removeItem or multiRemove to clear only your own keys instead.
41-
*/
42-
clear(callback?: (error?: Error) => void): Promise<void>;
43-
44-
/**
45-
* Gets all keys known to the app, for all callers, libraries, etc
46-
*/
47-
getAllKeys(callback?: (error?: Error, keys?: string[]) => void): Promise<string[]>;
48-
49-
/**
50-
* multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
51-
*/
52-
multiGet(
53-
keys: string[],
54-
callback?: (errors?: Error[], result?: [string, string | null][]) => void
55-
): Promise<[string, string | null][]>;
56-
57-
/**
58-
* multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
59-
*
60-
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
61-
*/
62-
multiSet(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
63-
64-
/**
65-
* Delete all the keys in the keys array.
66-
*/
67-
multiRemove(keys: string[], callback?: (errors?: Error[]) => void): Promise<void>;
68-
69-
/**
70-
* Merges existing values with input values, assuming they are stringified json.
71-
* Returns a Promise object.
72-
*
73-
* Not supported by all native implementations.
74-
*/
75-
multiMerge(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
76-
}
77-
78-
export function useAsyncStorage(key: string): {
79-
getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>;
80-
setItem(value: string, callback?: (error?: Error) => void): Promise<void>;
81-
mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>;
82-
removeItem(callback?: (error?: Error) => void): Promise<void>;
83-
}
84-
85-
const AsyncStorage: AsyncStorageStatic;
85+
export function useAsyncStorage(key: string): AsyncStorageHook
86+
const AsyncStorageLib: AsyncStorage;
87+
export default AsyncStorageLib;
88+
}
8689

87-
export default AsyncStorage;
90+
declare module '@react-native-async-storage/async-storage/jest/async-storage-mock' {
91+
export function useAsyncStorage(key: string): AsyncStorageHook
92+
const AsyncStorageLib: AsyncStorage;
93+
export default AsyncStorageLib;
8894
}

0 commit comments

Comments
 (0)