|
| 1 | +import {act} from '@testing-library/react'; |
| 2 | +import {beforeEach, describe, expect, it, vi} from 'vitest'; |
| 3 | +import {ValidationError} from '../../../src/utils/errors'; |
| 4 | +import {renderHookWithProviders} from '../../../src/test/test-utils'; |
| 5 | +import {useCreateLabel, useEditLabel, useFindLabelByName} from '../../../src/api/labels'; |
| 6 | +import {withMockFetch} from '../../utils/mock-fetch'; |
| 7 | + |
| 8 | +const {mockShowToast, mockSonnerError} = vi.hoisted(() => ({ |
| 9 | + mockShowToast: vi.fn(), |
| 10 | + mockSonnerError: vi.fn() |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('@tryghost/admin-x-design-system', async (importOriginal) => { |
| 14 | + const original = await importOriginal<typeof import('@tryghost/admin-x-design-system')>(); |
| 15 | + return {...original, showToast: mockShowToast}; |
| 16 | +}); |
| 17 | + |
| 18 | +vi.mock('sonner', () => ({ |
| 19 | + toast: { |
| 20 | + error: mockSonnerError, |
| 21 | + dismiss: vi.fn() |
| 22 | + } |
| 23 | +})); |
| 24 | + |
| 25 | +const duplicateLabelResponse = { |
| 26 | + errors: [{ |
| 27 | + code: 'VALIDATION', |
| 28 | + context: 'Label already exists', |
| 29 | + details: null, |
| 30 | + ghostErrorCode: null, |
| 31 | + help: null, |
| 32 | + id: 'label-error-id', |
| 33 | + message: 'Validation error, cannot save label.', |
| 34 | + property: null, |
| 35 | + type: 'ValidationError' |
| 36 | + }] |
| 37 | +}; |
| 38 | + |
| 39 | +const existingLabel = { |
| 40 | + id: 'label-1', |
| 41 | + name: 'Existing label', |
| 42 | + slug: 'existing-label', |
| 43 | + created_at: '', |
| 44 | + updated_at: '' |
| 45 | +}; |
| 46 | + |
| 47 | +const mockErrorFetch = { |
| 48 | + json: duplicateLabelResponse, |
| 49 | + headers: {'content-type': 'application/json'}, |
| 50 | + ok: false, |
| 51 | + status: 422 |
| 52 | +}; |
| 53 | + |
| 54 | +describe('labels api', () => { |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('rejects duplicate creates without reporting - the picker owns recovery', async () => { |
| 60 | + await withMockFetch(mockErrorFetch, async () => { |
| 61 | + const {result} = renderHookWithProviders(() => useCreateLabel()); |
| 62 | + |
| 63 | + await act(async () => { |
| 64 | + await expect(result.current.mutateAsync({name: 'Existing label'})).rejects.toBeInstanceOf(ValidationError); |
| 65 | + }); |
| 66 | + |
| 67 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 68 | + expect(mockSonnerError).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('finds a label by name', async () => { |
| 73 | + await withMockFetch({ |
| 74 | + json: {labels: [existingLabel]}, |
| 75 | + headers: {'content-type': 'application/json'}, |
| 76 | + ok: true, |
| 77 | + status: 200 |
| 78 | + }, async (mock) => { |
| 79 | + const {result} = renderHookWithProviders(() => useFindLabelByName()); |
| 80 | + |
| 81 | + await act(async () => { |
| 82 | + await expect(result.current(`O'Existing label`)).resolves.toEqual(existingLabel); |
| 83 | + }); |
| 84 | + |
| 85 | + const url = new URL(mock.calls[0][0] as string); |
| 86 | + expect(url.searchParams.get('filter')).toBe(String.raw`name:'O\'Existing label'`); |
| 87 | + expect(url.searchParams.get('limit')).toBe('1'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not escape backslashes in the lookup filter', async () => { |
| 92 | + await withMockFetch({ |
| 93 | + json: {labels: []}, |
| 94 | + headers: {'content-type': 'application/json'}, |
| 95 | + ok: true, |
| 96 | + status: 200 |
| 97 | + }, async (mock) => { |
| 98 | + const {result} = renderHookWithProviders(() => useFindLabelByName()); |
| 99 | + |
| 100 | + await act(async () => { |
| 101 | + await result.current('trailing\\'); |
| 102 | + }); |
| 103 | + |
| 104 | + // NQL keeps lone backslashes literal and only unescapes \' and \", |
| 105 | + // so a doubled backslash would query a two-backslash name |
| 106 | + const url = new URL(mock.calls[0][0] as string); |
| 107 | + expect(url.searchParams.get('filter')).toBe(String.raw`name:'trailing\'`); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('resolves undefined when no label matches the name', async () => { |
| 112 | + await withMockFetch({ |
| 113 | + json: {labels: []}, |
| 114 | + headers: {'content-type': 'application/json'}, |
| 115 | + ok: true, |
| 116 | + status: 200 |
| 117 | + }, async () => { |
| 118 | + const {result} = renderHookWithProviders(() => useFindLabelByName()); |
| 119 | + |
| 120 | + await act(async () => { |
| 121 | + await expect(result.current('Missing label')).resolves.toBeUndefined(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('rejects duplicate edits without reporting - the edit row surfaces them', async () => { |
| 127 | + await withMockFetch(mockErrorFetch, async () => { |
| 128 | + const {result} = renderHookWithProviders(() => useEditLabel()); |
| 129 | + |
| 130 | + await act(async () => { |
| 131 | + await expect(result.current.mutateAsync({id: 'label-1', name: 'Existing label'})).rejects.toBeInstanceOf(ValidationError); |
| 132 | + }); |
| 133 | + |
| 134 | + expect(mockShowToast).not.toHaveBeenCalled(); |
| 135 | + expect(mockSonnerError).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments