|
| 1 | +import Onyx from 'react-native-onyx'; |
| 2 | +import {read} from '@libs/API'; |
| 3 | +import {READ_COMMANDS} from '@libs/API/types'; |
| 4 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 5 | +import type {BillingGraceEndPeriod} from '@src/types/onyx'; |
| 6 | +import {openSubscriptionPage} from '../../src/libs/actions/Subscription'; |
| 7 | +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
| 8 | + |
| 9 | +jest.mock('@libs/API'); |
| 10 | +const mockRead = jest.mocked(read); |
| 11 | + |
| 12 | +describe('actions/Subscription', () => { |
| 13 | + beforeAll(() => { |
| 14 | + Onyx.init({ |
| 15 | + keys: ONYXKEYS, |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + |
| 22 | + return Onyx.clear().then(waitForBatchedUpdates); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + jest.restoreAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('openSubscriptionPage', () => { |
| 30 | + it('should call API.read with loading optimistic/success/failure data when no grace periods are provided', () => { |
| 31 | + openSubscriptionPage(); |
| 32 | + |
| 33 | + expect(mockRead).toHaveBeenCalledWith( |
| 34 | + READ_COMMANDS.OPEN_SUBSCRIPTION_PAGE, |
| 35 | + null, |
| 36 | + expect.objectContaining({ |
| 37 | + optimisticData: [ |
| 38 | + { |
| 39 | + onyxMethod: Onyx.METHOD.SET, |
| 40 | + key: ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA, |
| 41 | + value: true, |
| 42 | + }, |
| 43 | + ], |
| 44 | + successData: [ |
| 45 | + { |
| 46 | + onyxMethod: Onyx.METHOD.SET, |
| 47 | + key: ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA, |
| 48 | + value: false, |
| 49 | + }, |
| 50 | + ], |
| 51 | + failureData: [ |
| 52 | + { |
| 53 | + onyxMethod: Onyx.METHOD.SET, |
| 54 | + key: ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA, |
| 55 | + value: false, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should clear all grace period keys optimistically and restore on failure', () => { |
| 63 | + const gracePeriod1: BillingGraceEndPeriod = {value: 1700000000}; |
| 64 | + const gracePeriod2: BillingGraceEndPeriod = {value: 1700099999}; |
| 65 | + const key1 = `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END}11111`; |
| 66 | + const key2 = `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END}22222`; |
| 67 | + |
| 68 | + openSubscriptionPage({ |
| 69 | + [key1]: gracePeriod1, |
| 70 | + [key2]: gracePeriod2, |
| 71 | + }); |
| 72 | + |
| 73 | + expect(mockRead).toHaveBeenCalledWith( |
| 74 | + READ_COMMANDS.OPEN_SUBSCRIPTION_PAGE, |
| 75 | + null, |
| 76 | + expect.objectContaining({ |
| 77 | + optimisticData: expect.arrayContaining([ |
| 78 | + { |
| 79 | + onyxMethod: Onyx.METHOD.SET, |
| 80 | + key: ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA, |
| 81 | + value: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + onyxMethod: Onyx.METHOD.SET, |
| 85 | + key: key1, |
| 86 | + value: null, |
| 87 | + }, |
| 88 | + { |
| 89 | + onyxMethod: Onyx.METHOD.SET, |
| 90 | + key: key2, |
| 91 | + value: null, |
| 92 | + }, |
| 93 | + ]), |
| 94 | + failureData: expect.arrayContaining([ |
| 95 | + { |
| 96 | + onyxMethod: Onyx.METHOD.SET, |
| 97 | + key: ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA, |
| 98 | + value: false, |
| 99 | + }, |
| 100 | + { |
| 101 | + onyxMethod: Onyx.METHOD.SET, |
| 102 | + key: key1, |
| 103 | + value: gracePeriod1, |
| 104 | + }, |
| 105 | + { |
| 106 | + onyxMethod: Onyx.METHOD.SET, |
| 107 | + key: key2, |
| 108 | + value: gracePeriod2, |
| 109 | + }, |
| 110 | + ]), |
| 111 | + }), |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle undefined values in the grace period collection by rolling back to null', () => { |
| 116 | + const key1 = `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END}33333`; |
| 117 | + |
| 118 | + openSubscriptionPage({ |
| 119 | + [key1]: undefined, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(mockRead).toHaveBeenCalledWith( |
| 123 | + READ_COMMANDS.OPEN_SUBSCRIPTION_PAGE, |
| 124 | + null, |
| 125 | + expect.objectContaining({ |
| 126 | + failureData: expect.arrayContaining([ |
| 127 | + { |
| 128 | + onyxMethod: Onyx.METHOD.SET, |
| 129 | + key: key1, |
| 130 | + value: null, |
| 131 | + }, |
| 132 | + ]), |
| 133 | + }), |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should not include grace period data when collection is undefined', () => { |
| 138 | + openSubscriptionPage(undefined); |
| 139 | + |
| 140 | + const call = mockRead.mock.calls.at(0); |
| 141 | + const onyxData = call?.at(2) as Parameters<typeof mockRead>[2]; |
| 142 | + |
| 143 | + // optimisticData should only have the loading key |
| 144 | + expect(onyxData?.optimisticData).toHaveLength(1); |
| 145 | + expect(onyxData?.optimisticData?.[0]?.key).toBe(ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA); |
| 146 | + |
| 147 | + // failureData should only have the loading key |
| 148 | + expect(onyxData?.failureData).toHaveLength(1); |
| 149 | + expect(onyxData?.failureData?.[0]?.key).toBe(ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should not include grace period data when collection is empty', () => { |
| 153 | + openSubscriptionPage({}); |
| 154 | + |
| 155 | + const call = mockRead.mock.calls.at(0); |
| 156 | + const onyxData = call?.at(2) as Parameters<typeof mockRead>[2]; |
| 157 | + |
| 158 | + // optimisticData should only have the loading key |
| 159 | + expect(onyxData?.optimisticData).toHaveLength(1); |
| 160 | + expect(onyxData?.optimisticData?.[0]?.key).toBe(ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA); |
| 161 | + |
| 162 | + // failureData should only have the loading key |
| 163 | + expect(onyxData?.failureData).toHaveLength(1); |
| 164 | + expect(onyxData?.failureData?.[0]?.key).toBe(ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments