Skip to content

Commit

Permalink
Merge pull request #35676 from kubabutkiewicz/ts-migration/react-nati…
Browse files Browse the repository at this point in the history
…ve-airship

[No QA][TS migration] Migrate 'react-native-airship.js' mock to TypeScript
  • Loading branch information
roryabraham authored Feb 15, 2024
2 parents a45e8e0 + 4ca8d0f commit c4cff43
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
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(() => ({
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 it is needed to provide mocks of libraries which include it
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');

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

0 comments on commit c4cff43

Please sign in to comment.