|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { server } from '../../../tests/mocks/node'; |
| 5 | + |
| 6 | +import { updateChannelSiteUrl } from './channels'; |
| 7 | + |
| 8 | +const storeHash = 'test-store'; |
| 9 | +const accessToken = 'test-token'; |
| 10 | +const apiHost = 'api.bigcommerce.com'; |
| 11 | +const channelId = 1; |
| 12 | + |
| 13 | +beforeAll(() => { |
| 14 | + vi.mock('./telemetry', () => { |
| 15 | + const instance = { |
| 16 | + identify: vi.fn(), |
| 17 | + isEnabled: vi.fn(() => true), |
| 18 | + track: vi.fn(), |
| 19 | + correlationId: 'test-session-uuid', |
| 20 | + commandName: 'unknown', |
| 21 | + durationMs: vi.fn().mockReturnValue(0), |
| 22 | + analytics: { |
| 23 | + closeAndFlush: vi.fn().mockResolvedValue(undefined), |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + return { |
| 28 | + Telemetry: vi.fn().mockImplementation(() => instance), |
| 29 | + getTelemetry: vi.fn(() => instance), |
| 30 | + resetTelemetry: vi.fn(), |
| 31 | + }; |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +afterAll(() => { |
| 36 | + vi.restoreAllMocks(); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('updateChannelSiteUrl', () => { |
| 40 | + test('PUTs the URL and returns parsed channel site', async () => { |
| 41 | + let receivedBody: unknown; |
| 42 | + |
| 43 | + server.use( |
| 44 | + http.put( |
| 45 | + 'https://:apiHost/stores/:storeHash/v3/channels/:channelId/site', |
| 46 | + async ({ request }) => { |
| 47 | + receivedBody = await request.json(); |
| 48 | + |
| 49 | + return HttpResponse.json({ |
| 50 | + data: { |
| 51 | + id: 42, |
| 52 | + url: 'https://new.example.com', |
| 53 | + channel_id: channelId, |
| 54 | + }, |
| 55 | + }); |
| 56 | + }, |
| 57 | + ), |
| 58 | + ); |
| 59 | + |
| 60 | + const result = await updateChannelSiteUrl( |
| 61 | + channelId, |
| 62 | + 'https://new.example.com', |
| 63 | + storeHash, |
| 64 | + accessToken, |
| 65 | + apiHost, |
| 66 | + ); |
| 67 | + |
| 68 | + expect(receivedBody).toEqual({ url: 'https://new.example.com' }); |
| 69 | + expect(result).toEqual({ id: 42, url: 'https://new.example.com', channelId }); |
| 70 | + }); |
| 71 | + |
| 72 | + test('throws with re-auth hint on 401', async () => { |
| 73 | + server.use( |
| 74 | + http.put('https://:apiHost/stores/:storeHash/v3/channels/:channelId/site', () => |
| 75 | + HttpResponse.json({}, { status: 401 }), |
| 76 | + ), |
| 77 | + ); |
| 78 | + |
| 79 | + await expect( |
| 80 | + updateChannelSiteUrl(channelId, 'https://x.example', storeHash, accessToken, apiHost), |
| 81 | + ).rejects.toThrow('Re-run `catalyst auth login`'); |
| 82 | + }); |
| 83 | + |
| 84 | + test('throws with re-auth hint on 403', async () => { |
| 85 | + server.use( |
| 86 | + http.put('https://:apiHost/stores/:storeHash/v3/channels/:channelId/site', () => |
| 87 | + HttpResponse.json({}, { status: 403 }), |
| 88 | + ), |
| 89 | + ); |
| 90 | + |
| 91 | + await expect( |
| 92 | + updateChannelSiteUrl(channelId, 'https://x.example', storeHash, accessToken, apiHost), |
| 93 | + ).rejects.toThrow('Re-run `catalyst auth login`'); |
| 94 | + }); |
| 95 | + |
| 96 | + test('throws with status on other errors', async () => { |
| 97 | + server.use( |
| 98 | + http.put('https://:apiHost/stores/:storeHash/v3/channels/:channelId/site', () => |
| 99 | + HttpResponse.json({}, { status: 500 }), |
| 100 | + ), |
| 101 | + ); |
| 102 | + |
| 103 | + await expect( |
| 104 | + updateChannelSiteUrl(channelId, 'https://x.example', storeHash, accessToken, apiHost), |
| 105 | + ).rejects.toThrow('Failed to update channel site: 500'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments