Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add type definition for jest mock #708

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ default config for android apk builds: &android_defaults
resource_class: 'medium'
working_directory: ~/async_storage
environment:
- _JAVA_OPTIONS: '-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xmx2048m'
- _JAVA_OPTIONS: '-XX:+UnlockExperimentalVMOptions -Xmx2048m'
- GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.jvmargs="-XX:+HeapDumpOnOutOfMemoryError -Xmx2048m"'
- BUILD_THREADS: 2

Expand Down
14 changes: 14 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
{
"branches": [
"master"
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
[
"@semantic-release/git",
{
"assets": ["CHANGELOG.md", "package.json"],
"message": "chore(release): ${nextRelease.version} [skip ci]"
}
]
]
}
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
"@react-native-community/cli-platform-android": "^4.10.0",
"@react-native-community/cli-platform-ios": "^4.10.0",
"@react-native-community/eslint-config": "^3.0.0",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "9.0.0",
"detox": "17.10.6",
"eslint": "^7.0.0",
"expo": "^38.0.10",
Expand All @@ -92,7 +94,7 @@
"react-native-web": "~0.12.0",
"react-native-windows": "^0.63.41",
"react-test-renderer": "16.13.1",
"semantic-release": "^17.2.1"
"semantic-release": "^17.4.6"
},
"jest": {
"preset": "react-native",
Expand Down Expand Up @@ -133,7 +135,12 @@
"source": "src",
"output": "lib",
"targets": [
["commonjs", { "copyFlow": true }],
[
"commonjs",
{
"copyFlow": true
}
],
"module"
]
}
Expand Down
142 changes: 74 additions & 68 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
// CREDITS: This types are based on the original work made by all the people who contributed to @types/react-native

interface AsyncStorage {
/**
* Fetches key and passes the result to callback, along with an Error if there is any.
*/
getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null>;

/**
* Sets value for key and calls callback on completion, along with an Error if there is any
*/
setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;

removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;

/**
* Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
* Not supported by all native implementation
*/
mergeItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;

/**
* Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
* Use removeItem or multiRemove to clear only your own keys instead.
*/
clear(callback?: (error?: Error) => void): Promise<void>;

/**
* Gets all keys known to the app, for all callers, libraries, etc
*/
getAllKeys(callback?: (error?: Error, keys?: string[]) => void): Promise<string[]>;

/**
* multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
*/
multiGet(
keys: string[],
callback?: (errors?: Error[], result?: [string, string | null][]) => void
): Promise<[string, string | null][]>;

/**
* multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
*
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
*/
multiSet(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;

/**
* Delete all the keys in the keys array.
*/
multiRemove(keys: string[], callback?: (errors?: Error[]) => void): Promise<void>;

/**
* Merges existing values with input values, assuming they are stringified json.
* Returns a Promise object.
*
* Not supported by all native implementations.
*/
multiMerge(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
}

type AsyncStorageHook = {
getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>;
setItem(value: string, callback?: (error?: Error) => void): Promise<void>;
mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>;
removeItem(callback?: (error?: Error) => void): Promise<void>;
}

declare module '@react-native-async-storage/async-storage' {
/**
* AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage
Expand All @@ -16,73 +82,13 @@ declare module '@react-native-async-storage/async-storage' {
*
* @see https://react-native-async-storage.github.io/async-storage/docs/api
*/
export interface AsyncStorageStatic {
/**
* Fetches key and passes the result to callback, along with an Error if there is any.
*/
getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null>;

/**
* Sets value for key and calls callback on completion, along with an Error if there is any
*/
setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;

removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;

/**
* Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
* Not supported by all native implementation
*/
mergeItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;

/**
* Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
* Use removeItem or multiRemove to clear only your own keys instead.
*/
clear(callback?: (error?: Error) => void): Promise<void>;

/**
* Gets all keys known to the app, for all callers, libraries, etc
*/
getAllKeys(callback?: (error?: Error, keys?: string[]) => void): Promise<string[]>;

/**
* multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
*/
multiGet(
keys: string[],
callback?: (errors?: Error[], result?: [string, string | null][]) => void
): Promise<[string, string | null][]>;

/**
* multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
*
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
*/
multiSet(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;

/**
* Delete all the keys in the keys array.
*/
multiRemove(keys: string[], callback?: (errors?: Error[]) => void): Promise<void>;

/**
* Merges existing values with input values, assuming they are stringified json.
* Returns a Promise object.
*
* Not supported by all native implementations.
*/
multiMerge(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
}

export function useAsyncStorage(key: string): {
getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>;
setItem(value: string, callback?: (error?: Error) => void): Promise<void>;
mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>;
removeItem(callback?: (error?: Error) => void): Promise<void>;
}

const AsyncStorage: AsyncStorageStatic;
export function useAsyncStorage(key: string): AsyncStorageHook
const AsyncStorageLib: AsyncStorage;
export default AsyncStorageLib;
tido64 marked this conversation as resolved.
Show resolved Hide resolved
}

export default AsyncStorage;
declare module '@react-native-async-storage/async-storage/jest/async-storage-mock' {
export function useAsyncStorage(key: string): AsyncStorageHook
const AsyncStorageLib: AsyncStorage;
export default AsyncStorageLib;
}
36 changes: 30 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,16 @@
dependencies:
applicationinsights "^1.8.8"

"@semantic-release/changelog@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@semantic-release/changelog/-/changelog-5.0.1.tgz#50a84b63e5d391b7debfe021421589fa2bcdafe4"
integrity sha512-unvqHo5jk4dvAf2nZ3aw4imrlwQ2I50eVVvq9D47Qc3R+keNqepx1vDYwkjF8guFXnOYaYcR28yrZWno1hFbiw==
dependencies:
"@semantic-release/error" "^2.1.0"
aggregate-error "^3.0.0"
fs-extra "^9.0.0"
lodash "^4.17.4"

"@semantic-release/commit-analyzer@^8.0.0":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@semantic-release/commit-analyzer/-/commit-analyzer-8.0.1.tgz#5d2a37cd5a3312da0e3ac05b1ca348bf60b90bca"
Expand All @@ -2291,11 +2301,25 @@
lodash "^4.17.4"
micromatch "^4.0.2"

"@semantic-release/error@^2.2.0":
"@semantic-release/error@^2.1.0", "@semantic-release/error@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@semantic-release/error/-/error-2.2.0.tgz#ee9d5a09c9969eade1ec864776aeda5c5cddbbf0"
integrity sha512-9Tj/qn+y2j+sjCI3Jd+qseGtHjOAeg7dU2/lVcqIQ9TV3QDaDXDYXcoOHU+7o2Hwh8L8ymL4gfuO7KxDs3q2zg==

"@semantic-release/git@9.0.0":
version "9.0.0"
resolved "https://registry.yarnpkg.com/@semantic-release/git/-/git-9.0.0.tgz#304c4883c87d095b1faaae93300f1f1e0466e9a5"
integrity sha512-AZ4Zha5NAPAciIJH3ipzw/WU9qLAn8ENaoVAhD6srRPxTpTzuV3NhNh14rcAo8Paj9dO+5u4rTKcpetOBluYVw==
dependencies:
"@semantic-release/error" "^2.1.0"
aggregate-error "^3.0.0"
debug "^4.0.0"
dir-glob "^3.0.0"
execa "^4.0.0"
lodash "^4.17.4"
micromatch "^4.0.0"
p-reduce "^2.0.0"

"@semantic-release/github@^7.0.0":
version "7.2.3"
resolved "https://registry.yarnpkg.com/@semantic-release/github/-/github-7.2.3.tgz#20a83abd42dca43d97f03553de970eac72856c85"
Expand Down Expand Up @@ -9634,7 +9658,7 @@ micromatch@^3.1.10, micromatch@^3.1.4:
snapdragon "^0.8.1"
to-regex "^3.0.2"

micromatch@^4.0.2, micromatch@^4.0.4:
micromatch@^4.0.0, micromatch@^4.0.2, micromatch@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.4.tgz#896d519dfe9db25fce94ceb7a500919bf881ebf9"
integrity sha512-pRmzw/XUcwXGpD9aI9q/0XOwLNygjETJ8y0ao0wdqprrzDa4YnxLcz7fQRZr8voh8V10kGhABbNcHVk5wHgWwg==
Expand Down Expand Up @@ -12514,10 +12538,10 @@ schema-utils@^3.0.0:
ajv "^6.12.5"
ajv-keywords "^3.5.2"

semantic-release@^17.2.1:
version "17.4.4"
resolved "https://registry.yarnpkg.com/semantic-release/-/semantic-release-17.4.4.tgz#650dd50ecb520a5a2bc6811305bc9d4d5c3128b1"
integrity sha512-fQIA0lw2Sy/9+TcoM/BxyzKCSwdUd8EPRwGoOuBLgxKigPCY6kaKs8TOsgUVy6QrlTYwni2yzbMb5Q2107P9eA==
semantic-release@^17.4.6:
version "17.4.7"
resolved "https://registry.yarnpkg.com/semantic-release/-/semantic-release-17.4.7.tgz#88e1dce7294cc43acc54c4e0a83f582264567206"
integrity sha512-3Ghu8mKCJgCG3QzE5xphkYWM19lGE3XjFdOXQIKBM2PBpBvgFQ/lXv31oX0+fuN/UjNFO/dqhNs8ATLBhg6zBg==
dependencies:
"@semantic-release/commit-analyzer" "^8.0.0"
"@semantic-release/error" "^2.2.0"
Expand Down