|
| 1 | +import { CombinedError } from '@urql/core'; |
| 2 | + |
| 3 | +import { getMockOclifConfig } from '../../../../__tests__/commands/utils'; |
| 4 | +import { ExpoGraphqlClient } from '../../../../commandUtils/context/contextUtils/createGraphqlClient'; |
| 5 | +import { AppPlatform } from '../../../../graphql/generated'; |
| 6 | +import { |
| 7 | + EmbeddedUpdateFragment, |
| 8 | + EmbeddedUpdateQuery, |
| 9 | + isEmbeddedUpdateNotFoundError, |
| 10 | +} from '../../../../graphql/queries/EmbeddedUpdateQuery'; |
| 11 | +import Log from '../../../../log'; |
| 12 | +import * as json from '../../../../utils/json'; |
| 13 | +import UpdateEmbeddedView from '../view'; |
| 14 | + |
| 15 | +jest.mock('../../../../graphql/queries/EmbeddedUpdateQuery', () => ({ |
| 16 | + EmbeddedUpdateQuery: { viewByIdAsync: jest.fn() }, |
| 17 | + isEmbeddedUpdateNotFoundError: jest.fn(), |
| 18 | +})); |
| 19 | +jest.mock('../../../../log'); |
| 20 | +jest.mock('../../../../utils/json'); |
| 21 | + |
| 22 | +const mockView = jest.mocked(EmbeddedUpdateQuery.viewByIdAsync); |
| 23 | +const mockIsNotFound = jest.mocked(isEmbeddedUpdateNotFoundError); |
| 24 | +const mockLogLog = jest.mocked(Log.log); |
| 25 | +const mockEnableJsonOutput = jest.mocked(json.enableJsonOutput); |
| 26 | +const mockPrintJson = jest.mocked(json.printJsonOnlyOutput); |
| 27 | + |
| 28 | +const VALID_UUID = 'a1b2c3d4-1234-4000-8000-000000000000'; |
| 29 | + |
| 30 | +const MOCK_CONTEXT = { |
| 31 | + projectId: 'project-123', |
| 32 | + loggedIn: { graphqlClient: {} as ExpoGraphqlClient }, |
| 33 | +}; |
| 34 | + |
| 35 | +const MOCK_EMBEDDED_UPDATE: EmbeddedUpdateFragment = { |
| 36 | + id: VALID_UUID, |
| 37 | + platform: AppPlatform.Ios, |
| 38 | + runtimeVersion: '1.0.0', |
| 39 | + channel: 'production', |
| 40 | + createdAt: '2026-05-29T00:00:00Z', |
| 41 | +}; |
| 42 | + |
| 43 | +describe(UpdateEmbeddedView, () => { |
| 44 | + const mockConfig = getMockOclifConfig(); |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks(); |
| 48 | + mockView.mockResolvedValue(MOCK_EMBEDDED_UPDATE); |
| 49 | + mockIsNotFound.mockReturnValue(false); |
| 50 | + }); |
| 51 | + |
| 52 | + function createCommand(argv: string[]): UpdateEmbeddedView { |
| 53 | + const command = new UpdateEmbeddedView(argv, mockConfig); |
| 54 | + // @ts-expect-error getContextAsync is protected |
| 55 | + jest.spyOn(command, 'getContextAsync').mockResolvedValue(MOCK_CONTEXT); |
| 56 | + return command; |
| 57 | + } |
| 58 | + |
| 59 | + it('prints formatted details on success', async () => { |
| 60 | + await createCommand([VALID_UUID]).run(); |
| 61 | + |
| 62 | + expect(mockView).toHaveBeenCalledWith(MOCK_CONTEXT.loggedIn.graphqlClient, { |
| 63 | + embeddedUpdateId: VALID_UUID, |
| 64 | + appId: MOCK_CONTEXT.projectId, |
| 65 | + }); |
| 66 | + expect(mockLogLog).toHaveBeenCalledTimes(1); |
| 67 | + expect(mockLogLog.mock.calls[0][0]).toContain(VALID_UUID); |
| 68 | + }); |
| 69 | + |
| 70 | + it('--json prints the raw embedded update and skips formatted output', async () => { |
| 71 | + await createCommand([VALID_UUID, '--json']).run(); |
| 72 | + |
| 73 | + expect(mockEnableJsonOutput).toHaveBeenCalled(); |
| 74 | + expect(mockPrintJson).toHaveBeenCalledWith(MOCK_EMBEDDED_UPDATE); |
| 75 | + expect(mockLogLog).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('exits with a friendly message when the server returns NOT_FOUND', async () => { |
| 79 | + const notFound = new CombinedError({ graphQLErrors: [] }); |
| 80 | + mockView.mockRejectedValue(notFound); |
| 81 | + mockIsNotFound.mockReturnValue(true); |
| 82 | + |
| 83 | + await expect(createCommand([VALID_UUID]).run()).rejects.toThrow(); |
| 84 | + expect(mockIsNotFound).toHaveBeenCalledWith(notFound); |
| 85 | + }); |
| 86 | + |
| 87 | + it('rethrows unexpected errors', async () => { |
| 88 | + const boom = new Error('boom'); |
| 89 | + mockView.mockRejectedValue(boom); |
| 90 | + mockIsNotFound.mockReturnValue(false); |
| 91 | + |
| 92 | + await expect(createCommand([VALID_UUID]).run()).rejects.toThrow('boom'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments