|
| 1 | +import * as LINKING from "react-native"; |
| 2 | +import configureMockStore from "redux-mock-store"; |
| 3 | +import { applicationChangeState } from "../../store/actions/application"; |
| 4 | +import { appReducer } from "../../store/reducers"; |
| 5 | +import { GlobalState } from "../../store/reducers/types"; |
| 6 | +import { linkingSubscription } from "../linkingSubscription"; |
| 7 | +import * as ARCHIVING_SELECTORS from "../../features/messages/store/reducers/archiving"; |
| 8 | +import { resetMessageArchivingAction } from "../../features/messages/store/actions/archiving"; |
| 9 | +import * as UTIL_GUARDS from "../../features/authentication/common/store/utils/guards"; |
| 10 | +import * as DEEP_LINKING from "../../features/pn/aar/utils/deepLinking"; |
| 11 | +import { storeLinkingUrl } from "../../store/actions/linking"; |
| 12 | +import * as NAVIGATION_SRV from "../NavigationService"; |
| 13 | +import { MESSAGES_ROUTES } from "../../features/messages/navigation/routes"; |
| 14 | +import PN_ROUTES from "../../features/pn/navigation/routes"; |
| 15 | +import * as UTM_LINK from "../../features/utmLink"; |
| 16 | + |
| 17 | +describe("linkingSubscription", () => { |
| 18 | + beforeEach(() => { |
| 19 | + jest.clearAllMocks(); |
| 20 | + jest.resetAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should add and remove an event listener", () => { |
| 24 | + const { addEventListenerSpy, mockCurrySubscription } = initializeTests(); |
| 25 | + |
| 26 | + const removeMock = jest.fn(); |
| 27 | + addEventListenerSpy.mockImplementation( |
| 28 | + () => ({ remove: removeMock } as any) |
| 29 | + ); |
| 30 | + |
| 31 | + const mockUnsubscribe = mockCurrySubscription(jest.fn()); |
| 32 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 33 | + "url", |
| 34 | + expect.any(Function) |
| 35 | + ); |
| 36 | + mockUnsubscribe(); |
| 37 | + expect(removeMock).toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + it("should call the listener and 'processUtmLink'", () => { |
| 40 | + const { mockCurrySubscription, addEventListenerSpy } = initializeTests(); |
| 41 | + const mockListener = jest.fn(); |
| 42 | + const mockProcessUtmLink = jest.fn(); |
| 43 | + |
| 44 | + jest |
| 45 | + .spyOn(UTM_LINK, "processUtmLink") |
| 46 | + .mockImplementation(mockProcessUtmLink); |
| 47 | + |
| 48 | + mockCurrySubscription(mockListener); |
| 49 | + |
| 50 | + const testUrl = "https://example.com"; |
| 51 | + runEventListenerCallback(addEventListenerSpy, { url: testUrl }); |
| 52 | + |
| 53 | + expect(mockListener).toHaveBeenCalledTimes(1); |
| 54 | + expect(mockListener).toHaveBeenCalledWith(testUrl); |
| 55 | + expect(mockProcessUtmLink).toHaveBeenCalledTimes(1); |
| 56 | + expect(mockProcessUtmLink).toHaveBeenCalledWith( |
| 57 | + testUrl, |
| 58 | + expect.any(Function) |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it.each([false, true])( |
| 63 | + `should dispatch 'resetMessageArchivingAction' if archiving is not disabled; archiving : %d`, |
| 64 | + isDisabled => { |
| 65 | + const { mockDispatch, mockCurrySubscription, addEventListenerSpy } = |
| 66 | + initializeTests(); |
| 67 | + |
| 68 | + jest |
| 69 | + .spyOn(ARCHIVING_SELECTORS, "isArchivingDisabledSelector") |
| 70 | + .mockImplementation(() => isDisabled); |
| 71 | + |
| 72 | + mockCurrySubscription(jest.fn()); |
| 73 | + |
| 74 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(1); |
| 75 | + |
| 76 | + const testUrl = "https://example.com"; |
| 77 | + runEventListenerCallback(addEventListenerSpy, { url: testUrl }); |
| 78 | + if (isDisabled) { |
| 79 | + expect(mockDispatch).not.toHaveBeenCalledWith( |
| 80 | + resetMessageArchivingAction(undefined) |
| 81 | + ); |
| 82 | + } else { |
| 83 | + expect(mockDispatch).toHaveBeenCalledWith( |
| 84 | + resetMessageArchivingAction(undefined) |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + ); |
| 89 | + |
| 90 | + [true, false].forEach(isLoggedIn => { |
| 91 | + [true, false].forEach(isAARLink => { |
| 92 | + it(`should handle a URL event when${ |
| 93 | + isLoggedIn ? "" : " not" |
| 94 | + } logged in, and the link passed ${ |
| 95 | + isAARLink ? "is" : "isn't" |
| 96 | + } a valid AAR link`, () => { |
| 97 | + const { mockDispatch, mockCurrySubscription, addEventListenerSpy } = |
| 98 | + initializeTests(); |
| 99 | + const mockNav = jest.fn(); |
| 100 | + const testUrl = `https://example.com/${isAARLink}/${isLoggedIn}`; |
| 101 | + |
| 102 | + jest |
| 103 | + .spyOn(UTIL_GUARDS, "isLoggedIn") |
| 104 | + .mockImplementation(() => isLoggedIn); |
| 105 | + jest |
| 106 | + .spyOn(DEEP_LINKING, "isSendAARLink") |
| 107 | + .mockImplementation(() => isAARLink); |
| 108 | + jest |
| 109 | + .spyOn(NAVIGATION_SRV.default, "navigate") |
| 110 | + .mockImplementation(mockNav); |
| 111 | + |
| 112 | + mockCurrySubscription(jest.fn()); |
| 113 | + |
| 114 | + runEventListenerCallback(addEventListenerSpy, { url: testUrl }); |
| 115 | + |
| 116 | + if (isLoggedIn) { |
| 117 | + // When logged in, we do not store the URL for later processing |
| 118 | + expect(mockDispatch).not.toHaveBeenCalledWith( |
| 119 | + expect.objectContaining({ type: "STORE_LINKING_URL" }) |
| 120 | + ); |
| 121 | + |
| 122 | + if (isAARLink) { |
| 123 | + expect(mockNav).toHaveBeenCalledWith( |
| 124 | + MESSAGES_ROUTES.MESSAGES_NAVIGATOR, |
| 125 | + { |
| 126 | + screen: PN_ROUTES.MAIN, |
| 127 | + params: { |
| 128 | + screen: PN_ROUTES.QR_SCAN_FLOW, |
| 129 | + params: { aarUrl: testUrl } |
| 130 | + } |
| 131 | + } |
| 132 | + ); |
| 133 | + } else { |
| 134 | + expect(mockNav).not.toHaveBeenCalled(); |
| 135 | + } |
| 136 | + } else { |
| 137 | + expect(mockNav).not.toHaveBeenCalled(); |
| 138 | + expect(mockDispatch).toHaveBeenCalledWith(storeLinkingUrl(testUrl)); |
| 139 | + } |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +// --------------------- UTILS --------------------- |
| 146 | + |
| 147 | +const runEventListenerCallback = ( |
| 148 | + eventListenerSpy: jest.SpyInstance, |
| 149 | + event: { url: string } |
| 150 | +) => { |
| 151 | + const callback = eventListenerSpy.mock.calls[0][1] as (event: { |
| 152 | + url: string; |
| 153 | + }) => void; |
| 154 | + callback(event); |
| 155 | +}; |
| 156 | + |
| 157 | +const initializeTests = () => { |
| 158 | + const mockStore = configureMockStore<GlobalState>(); |
| 159 | + const defaultState = appReducer(undefined, applicationChangeState("active")); |
| 160 | + const store = mockStore(defaultState); |
| 161 | + const addEventListenerSpy = jest.spyOn(LINKING.Linking, "addEventListener"); |
| 162 | + const mockDispatch = jest.fn(); |
| 163 | + const mockCurrySubscription = linkingSubscription(mockDispatch, store); |
| 164 | + |
| 165 | + return { |
| 166 | + store, |
| 167 | + addEventListenerSpy, |
| 168 | + mockCurrySubscription, |
| 169 | + mockDispatch |
| 170 | + }; |
| 171 | +}; |
0 commit comments