|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 6 | +import { AppleIapClient } from './apple-iap.client'; |
| 7 | +import { AppleIapMissingCredentialsError } from './apple-iap.error'; |
| 8 | +import { Environment, StatusResponse } from 'app-store-server-api'; |
| 9 | +import { |
| 10 | + AppleIapClientConfig, |
| 11 | + MockAppleIapClientConfigProvider, |
| 12 | +} from './apple-iap.client.config'; |
| 13 | +import assert from 'assert'; |
| 14 | +import { faker } from '@faker-js/faker'; |
| 15 | + |
| 16 | +jest.mock('app-store-server-api', () => { |
| 17 | + const actual = jest.requireActual('app-store-server-api'); |
| 18 | + return { |
| 19 | + Environment: actual.Environment, |
| 20 | + AppStoreServerAPI: jest.fn().mockImplementation(() => ({ |
| 21 | + getSubscriptionStatuses: jest.fn(), |
| 22 | + })), |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +describe('AppleIapClient', () => { |
| 27 | + let appleIapClient: AppleIapClient; |
| 28 | + let appleIapClientConfig: AppleIapClientConfig; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + const module: TestingModule = await Test.createTestingModule({ |
| 32 | + providers: [MockAppleIapClientConfigProvider, AppleIapClient], |
| 33 | + }).compile(); |
| 34 | + |
| 35 | + appleIapClient = module.get(AppleIapClient); |
| 36 | + appleIapClientConfig = module.get(AppleIapClientConfig); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('getSubscriptionStatuses', () => { |
| 40 | + it('should return subscription status', async () => { |
| 41 | + const mockTransactionId = faker.string.uuid(); |
| 42 | + const mockStatusResponse = { |
| 43 | + environment: Environment.Sandbox, |
| 44 | + } as StatusResponse; |
| 45 | + const mockApiInstance = appleIapClient.appStoreServerApiClients |
| 46 | + .values() |
| 47 | + .next().value; |
| 48 | + assert(mockApiInstance); |
| 49 | + |
| 50 | + jest |
| 51 | + .spyOn(mockApiInstance, 'getSubscriptionStatuses') |
| 52 | + .mockResolvedValue(mockStatusResponse); |
| 53 | + |
| 54 | + const result = await appleIapClient.getSubscriptionStatuses( |
| 55 | + appleIapClientConfig.credentials[0].bundleId, |
| 56 | + mockTransactionId |
| 57 | + ); |
| 58 | + |
| 59 | + expect(result).toEqual(mockStatusResponse); |
| 60 | + expect(mockApiInstance.getSubscriptionStatuses).toHaveBeenCalledWith( |
| 61 | + mockTransactionId |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw AppleIapMissingCredentialsError if no credentials exist for bundleId', () => { |
| 66 | + const mockTransactionId = faker.string.uuid(); |
| 67 | + const mockBundleId = faker.string.uuid(); |
| 68 | + |
| 69 | + expect(async () => |
| 70 | + appleIapClient.getSubscriptionStatuses(mockBundleId, mockTransactionId) |
| 71 | + ).rejects.toThrowError(AppleIapMissingCredentialsError); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments