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

[TS migration] Migrate react-native-onyx mock #35674

Merged
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
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

/* eslint-disable rulesdir/prefer-onyx-connect-in-libs */
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get rid of this, or add a comment explaining why we need it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't get rid of this but I've added a comment explaining the reason we are disabling the rule.
Please have a look.

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};
Loading