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

Conversation

lafest
Copy link
Contributor

@lafest lafest commented Nov 9, 2021

Summary

According to with-jest-setup-file jest integration, I tried to test app.test.tsx file using ts-jest and followed

import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

throws ts(7016) error.
스크린샷 2021-11-09 오후 2 58 29

Test Plan

If you use this change with ts-jest, the error disappears.

@lafest
Copy link
Contributor Author

lafest commented Nov 16, 2021

@krizzu please review this PR. It seems that you're the maintainer.

@krizzu
Copy link
Member

krizzu commented Nov 17, 2021

Sure thing, looks fine

I just wonder if we can reduce the duplication and do something like this

// 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
   * system that is global to the app.  It should be used instead of LocalStorage.
   *
   * It is recommended that you use an abstraction on top of `AsyncStorage`
   * instead of `AsyncStorage` directly for anything more than light usage since
   * it operates globally.
   *
   * On iOS, `AsyncStorage` is backed by native code that stores small values in a
   * serialized dictionary and larger values in separate files. On Android,
   * `AsyncStorage` will use either [RocksDB](http://rocksdb.org/) or SQLite
   * based on what is available.
   *
   * @see https://react-native-async-storage.github.io/async-storage/docs/api
   */
  export function useAsyncStorage(key: string): AsyncStorageHook
  const AsyncStorageLib: AsyncStorage;
  export default AsyncStorageLib;
}

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

@lafest
Copy link
Contributor Author

lafest commented Nov 18, 2021

@krizzu Thanks for review!
I modified PR with based on your review.

It seems like some internal error in Android testing, but I have no permission for rerunning CI tests. Can you rerun that CI test?

@krizzu
Copy link
Member

krizzu commented Nov 18, 2021

@lafest Hey, please rebase your code, we merged a fix for this yesterday

* improve releases

* remove java automatic heap detection
@lafest
Copy link
Contributor Author

lafest commented Nov 22, 2021

@krizzu I rebased this repository's master branch to mine. I'm not sure I did right thing, because this is my first rebase. Did I do right?

@krizzu
Copy link
Member

krizzu commented Nov 22, 2021

It's more like a merge, than a rebase, but it's fine.

@tido64 mind having a look at the types? Need another pair of eyes on this 🙏

Copy link
Member

@tido64 tido64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for the merge/rebase weirdness, it looks fine to me.

types/index.d.ts Show resolved Hide resolved
@krizzu krizzu merged commit 5f6d6a0 into react-native-async-storage:master Nov 24, 2021
@krizzu
Copy link
Member

krizzu commented Nov 24, 2021

🎉 This PR is included in version 1.15.12 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants