|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { scheduleEntryAction } from '../../src/lib/create-stack'; |
| 3 | + |
| 4 | +const { mockPost } = vi.hoisted(() => ({ mockPost: vi.fn() })); |
| 5 | + |
| 6 | +vi.mock('axios', () => ({ default: { post: mockPost } })); |
| 7 | + |
| 8 | +vi.mock('@contentstack/cli-utilities', () => ({ |
| 9 | + configHandler: { |
| 10 | + get: vi.fn().mockImplementation((key: string) => { |
| 11 | + if (key === 'authorisationType') return 'BASIC'; |
| 12 | + if (key === 'authtoken') return 'test-authtoken'; |
| 13 | + if (key === 'region') return 'NA'; |
| 14 | + return undefined; |
| 15 | + }), |
| 16 | + }, |
| 17 | + authHandler: { |
| 18 | + checkExpiryAndRefresh: vi.fn().mockResolvedValue(undefined), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +describe('scheduleEntryAction', () => { |
| 23 | + const API_KEY = 'blt-test-api-key'; |
| 24 | + const ENTRY_OPTS = { |
| 25 | + contentTypeUid: 'blog', |
| 26 | + entryUid: 'entry123', |
| 27 | + action: 'publish' as const, |
| 28 | + environment: 'development', |
| 29 | + locale: 'en-us', |
| 30 | + scheduledAt: '2026-08-01T10:00:00.000Z', |
| 31 | + }; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + mockPost.mockReset(); |
| 35 | + mockPost.mockResolvedValue({ data: {} }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('sends api_version: 3.2 header on entry publish', async () => { |
| 39 | + await scheduleEntryAction(API_KEY, ENTRY_OPTS); |
| 40 | + |
| 41 | + expect(mockPost).toHaveBeenCalledOnce(); |
| 42 | + const [url, , { headers }] = mockPost.mock.calls[0]; |
| 43 | + expect(url).toContain('/v3/content_types/blog/entries/entry123/publish'); |
| 44 | + expect(headers).toMatchObject({ api_version: '3.2' }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('sends api_version: 3.2 header on entry unpublish', async () => { |
| 48 | + await scheduleEntryAction(API_KEY, { ...ENTRY_OPTS, action: 'unpublish' }); |
| 49 | + |
| 50 | + const [url, , { headers }] = mockPost.mock.calls[0]; |
| 51 | + expect(url).toContain('/v3/content_types/blog/entries/entry123/unpublish'); |
| 52 | + expect(headers).toMatchObject({ api_version: '3.2' }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('sends api_version: 3.2 header on asset publish (sys_assets)', async () => { |
| 56 | + await scheduleEntryAction(API_KEY, { |
| 57 | + ...ENTRY_OPTS, |
| 58 | + contentTypeUid: 'sys_assets', |
| 59 | + entryUid: 'asset456', |
| 60 | + }); |
| 61 | + |
| 62 | + const [url, , { headers }] = mockPost.mock.calls[0]; |
| 63 | + expect(url).toContain('/v3/assets/asset456/publish'); |
| 64 | + expect(headers).toMatchObject({ api_version: '3.2' }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('sends api_version: 3.2 header on asset unpublish (sys_assets)', async () => { |
| 68 | + await scheduleEntryAction(API_KEY, { |
| 69 | + ...ENTRY_OPTS, |
| 70 | + contentTypeUid: 'sys_assets', |
| 71 | + entryUid: 'asset456', |
| 72 | + action: 'unpublish', |
| 73 | + }); |
| 74 | + |
| 75 | + const [url, , { headers }] = mockPost.mock.calls[0]; |
| 76 | + expect(url).toContain('/v3/assets/asset456/unpublish'); |
| 77 | + expect(headers).toMatchObject({ api_version: '3.2' }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('includes branch in headers alongside api_version when branch option is provided', async () => { |
| 81 | + await scheduleEntryAction(API_KEY, { ...ENTRY_OPTS, branch: 'feature-branch' }); |
| 82 | + |
| 83 | + const [, , { headers }] = mockPost.mock.calls[0]; |
| 84 | + expect(headers).toMatchObject({ api_version: '3.2', branch: 'feature-branch' }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('omits branch from headers when no branch option is given', async () => { |
| 88 | + await scheduleEntryAction(API_KEY, ENTRY_OPTS); |
| 89 | + |
| 90 | + const [, , { headers }] = mockPost.mock.calls[0]; |
| 91 | + expect(headers).not.toHaveProperty('branch'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments