Skip to content

Commit

Permalink
Merge pull request #35674 from ruben-rebelo/ts-migration/react-native…
Browse files Browse the repository at this point in the history
…-onyx-mock
  • Loading branch information
cead22 authored Feb 20, 2024
2 parents 299576f + 7fdbd4b commit e4051a8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
27 changes: 0 additions & 27 deletions __mocks__/react-native-onyx.js

This file was deleted.

43 changes: 43 additions & 0 deletions __mocks__/react-native-onyx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* We are disabling the lint rule that doesn't allow the usage of Onyx.connect outside libs
* because the intent of this file is to mock the usage of react-native-onyx so we will have to mock the connect function
*/

/* eslint-disable rulesdir/prefer-onyx-connect-in-libs */
import type {ConnectOptions, OnyxKey} from 'react-native-onyx';
import Onyx, {withOnyx} from 'react-native-onyx';

let connectCallbackDelay = 0;
function addDelayToConnectCallback(delay: number) {
connectCallbackDelay = delay;
}

type ReactNativeOnyxMock = {
addDelayToConnectCallback: (delay: number) => void;
} & typeof Onyx;

type ConnectionCallback<TKey extends OnyxKey> = NonNullable<ConnectOptions<TKey>['callback']>;
type ConnectionCallbackParams<TKey extends OnyxKey> = Parameters<ConnectionCallback<TKey>>;

const reactNativeOnyxMock: ReactNativeOnyxMock = {
...Onyx,
connect: <TKey extends OnyxKey>(mapping: ConnectOptions<TKey>) => {
const callback = (...params: ConnectionCallbackParams<TKey>) => {
if (connectCallbackDelay > 0) {
setTimeout(() => {
(mapping.callback as (...args: ConnectionCallbackParams<TKey>) => void)?.(...params);
}, connectCallbackDelay);
} else {
(mapping.callback as (...args: ConnectionCallbackParams<TKey>) => void)?.(...params);
}
};
return Onyx.connect({
...mapping,
callback,
});
},
addDelayToConnectCallback,
};

export default reactNativeOnyxMock;
export {withOnyx};

0 comments on commit e4051a8

Please sign in to comment.