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.

39 changes: 39 additions & 0 deletions __mocks__/react-native-onyx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* 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};
/* eslint-enable 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.

Should we enable the rule at the end of a file?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to enable it again @ruben-rebelo 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right! This is a left-over, removing it! Thank you!

Loading