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

[No QA][TS migration] Migrate 'react-native-airship.js' mock to TypeScript #35676

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
42 changes: 0 additions & 42 deletions __mocks__/@ua/react-native-airship.js

This file was deleted.

53 changes: 53 additions & 0 deletions __mocks__/@ua/react-native-airship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type {AirshipContact, AirshipPush, AirshipPushAndroid, AirshipPushIOS, AirshipRoot} from '@ua/react-native-airship';
import {EventType as AirshipEventType, iOS as AirshipIOS} from '@ua/react-native-airship';

const EventType: Partial<typeof AirshipEventType> = {
NotificationResponse: AirshipEventType.NotificationResponse,
PushReceived: AirshipEventType.PushReceived,
};

const iOS: Partial<typeof AirshipIOS> = {
ForegroundPresentationOption: {
Sound: AirshipIOS.ForegroundPresentationOption.Sound,
Badge: AirshipIOS.ForegroundPresentationOption.Badge,
Banner: AirshipIOS.ForegroundPresentationOption.Banner,
List: AirshipIOS.ForegroundPresentationOption.List,
},
};

const pushIOS: AirshipPushIOS = jest.fn().mockImplementation(() => ({
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
setBadgeNumber: jest.fn(),
setForegroundPresentationOptions: jest.fn(),
setForegroundPresentationOptionsCallback: jest.fn(),
}))();

const pushAndroid: AirshipPushAndroid = jest.fn().mockImplementation(() => ({
setForegroundDisplayPredicate: jest.fn(),
}))();

const push: AirshipPush = jest.fn().mockImplementation(() => ({
iOS: pushIOS,
android: pushAndroid,
enableUserNotifications: () => Promise.resolve(false),
clearNotifications: jest.fn(),
getNotificationStatus: () => Promise.resolve({airshipOptIn: false, systemEnabled: false, airshipEnabled: false}),
getActiveNotifications: () => Promise.resolve([]),
}))();

const contact: AirshipContact = jest.fn().mockImplementation(() => ({
identify: jest.fn(),
getNamedUserId: () => Promise.resolve(undefined),
reset: jest.fn(),
module: jest.fn(),
}))();

const Airship: Partial<AirshipRoot> = {
addListener: jest.fn(),
removeAllListeners: jest.fn(),
push,
contact,
};

export default Airship;

export {EventType, iOS};
8 changes: 3 additions & 5 deletions jest/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import mockClipboard from '@react-native-clipboard/clipboard/jest/clipboard-mock';
import '@shopify/flash-list/jestSetup';
import 'react-native-gesture-handler/jestSetup';
import mockStorage from 'react-native-onyx/dist/storage/__mocks__';
Expand All @@ -13,15 +12,14 @@ reanimatedJestUtils.setUpTests();
// https://reactnavigation.org/docs/testing/#mocking-native-modules
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');

// Clipboard requires mocking as NativeEmitter will be undefined with jest-runner.
// https://github.com/react-native-clipboard/clipboard#mocking-clipboard
jest.mock('@react-native-clipboard/clipboard', () => mockClipboard);

// Mock react-native-onyx storage layer because the SQLite storage layer doesn't work in jest.
// Mocking this file in __mocks__ does not work because jest doesn't support mocking files that are not directly used in the testing project,
// and we only want to mock the storage layer, not the whole Onyx module.
jest.mock('react-native-onyx/dist/storage', () => mockStorage);

// Mock NativeEventEmitter as its needed to provide mocks of libraries which include it
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved

// Turn off the console logs for timing events. They are not relevant for unit tests and create a lot of noise
jest.spyOn(console, 'debug').mockImplementation((...params) => {
if (params[0].indexOf('Timing:') === 0) {
Expand Down
Loading