|
| 1 | +/* |
| 2 | + * Copyright Contributors to the OpenCue Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + isValidShowName, |
| 19 | + findShow, |
| 20 | + createShow, |
| 21 | + getShows, |
| 22 | + type Show, |
| 23 | +} from '@/app/utils/show_utils'; |
| 24 | +import { accessGetApi } from '@/app/utils/api_utils'; |
| 25 | + |
| 26 | +jest.mock('@/app/utils/api_utils', () => ({ |
| 27 | + accessGetApi: jest.fn(), |
| 28 | +})); |
| 29 | + |
| 30 | +const mockShow: Show = { |
| 31 | + id: 's1', name: 'myshow', active: true, bookingEnabled: true, dispatchEnabled: true, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('show_utils', () => { |
| 35 | + beforeEach(() => jest.clearAllMocks()); |
| 36 | + |
| 37 | + describe('isValidShowName', () => { |
| 38 | + it('accepts alphanumeric names', () => { |
| 39 | + expect(isValidShowName('myShow123')).toBe(true); |
| 40 | + }); |
| 41 | + it('rejects empty, spaces, and punctuation', () => { |
| 42 | + expect(isValidShowName('')).toBe(false); |
| 43 | + expect(isValidShowName('my show')).toBe(false); |
| 44 | + expect(isValidShowName('my-show')).toBe(false); |
| 45 | + expect(isValidShowName('show_1')).toBe(false); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('findShow', () => { |
| 50 | + it('posts the name to /api/show/findshow and returns the show', async () => { |
| 51 | + (accessGetApi as jest.Mock).mockResolvedValue({ show: mockShow }); |
| 52 | + |
| 53 | + await expect(findShow('myshow')).resolves.toEqual(mockShow); |
| 54 | + expect(accessGetApi).toHaveBeenCalledWith( |
| 55 | + '/api/show/findshow', |
| 56 | + JSON.stringify({ name: 'myshow' }), |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns null when the gateway reports the show was not found', async () => { |
| 61 | + (accessGetApi as jest.Mock).mockResolvedValue({ notFound: true }); |
| 62 | + await expect(findShow('nope')).resolves.toBeNull(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns null when nothing comes back', async () => { |
| 66 | + (accessGetApi as jest.Mock).mockResolvedValue(null); |
| 67 | + await expect(findShow('nope')).resolves.toBeNull(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('createShow', () => { |
| 72 | + it('posts the name to /api/show/createshow and returns the created show', async () => { |
| 73 | + (accessGetApi as jest.Mock).mockResolvedValue({ show: mockShow }); |
| 74 | + |
| 75 | + await expect(createShow('myshow')).resolves.toEqual(mockShow); |
| 76 | + expect(accessGetApi).toHaveBeenCalledWith( |
| 77 | + '/api/show/createshow', |
| 78 | + JSON.stringify({ name: 'myshow' }), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('throws when creation fails so the form can surface the error', async () => { |
| 83 | + (accessGetApi as jest.Mock).mockResolvedValue(null); |
| 84 | + await expect(createShow('myshow')).rejects.toThrow('Failed to create show'); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('getShows', () => { |
| 89 | + it('returns the array of shows', async () => { |
| 90 | + (accessGetApi as jest.Mock).mockResolvedValue([mockShow]); |
| 91 | + await expect(getShows()).resolves.toEqual([mockShow]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns [] when the gateway returns a non-array', async () => { |
| 95 | + (accessGetApi as jest.Mock).mockResolvedValue(null); |
| 96 | + await expect(getShows()).resolves.toEqual([]); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments