|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2024 Red Hat, Inc. |
| 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + ***********************************************************************/ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | + |
| 21 | +import { fireEvent, render, screen } from '@testing-library/svelte'; |
| 22 | +import { afterEach, beforeAll, expect, test, vi } from 'vitest'; |
| 23 | + |
| 24 | +import { providerInfos } from '/@/stores/providers'; |
| 25 | +import type { ImageInfo } from '/@api/image-info'; |
| 26 | +import type { ProviderContainerConnectionInfo, ProviderInfo } from '/@api/provider-info'; |
| 27 | + |
| 28 | +import PodEmptyScreen from './PodEmptyScreen.svelte'; |
| 29 | + |
| 30 | +beforeAll(() => { |
| 31 | + (window as any).createPod = vi.fn(); |
| 32 | + (window as any).showMessageBox = vi.fn(); |
| 33 | + (window as any).clipboardWriteText = vi.fn(); |
| 34 | + (window as any).pullImage = vi.fn(); |
| 35 | + (window as any).listImages = vi.fn(); |
| 36 | + (window as any).createAndStartContainer = vi.fn(); |
| 37 | + providerInfos.set([ |
| 38 | + { |
| 39 | + containerConnections: [ |
| 40 | + { |
| 41 | + status: 'started', |
| 42 | + } as unknown as ProviderContainerConnectionInfo, |
| 43 | + ], |
| 44 | + } as unknown as ProviderInfo, |
| 45 | + ]); |
| 46 | +}); |
| 47 | + |
| 48 | +afterEach(() => { |
| 49 | + vi.resetAllMocks(); |
| 50 | +}); |
| 51 | + |
| 52 | +const helloImage = 'quay.io/podman/hello:latest'; |
| 53 | +const error = new Error('Error message'); |
| 54 | +const errorMessage = { |
| 55 | + title: 'Error when running a pod', |
| 56 | + message: String(error), |
| 57 | +}; |
| 58 | +const imageErrorMessage = { |
| 59 | + title: `Error when running a pod`, |
| 60 | + message: `Could not find '${helloImage}' in images`, |
| 61 | +}; |
| 62 | +const providerErrorMessage = { |
| 63 | + title: `Error when running a pod`, |
| 64 | + message: `No provider connections found`, |
| 65 | +}; |
| 66 | +const buttonName = 'Start your first pod'; |
| 67 | +const getButton = screen.getByRole.bind(screen, 'button', { name: buttonName }); |
| 68 | +const copyToClipboard = 'Copy To Clipboard'; |
| 69 | +const imageInfo = { |
| 70 | + engineId: 'engineId', |
| 71 | + RepoTags: [helloImage], |
| 72 | +} as ImageInfo; |
| 73 | +const podInfo = { engineId: imageInfo.engineId, Id: 'Id' }; |
| 74 | +const podCreateCommand = `podman run -dt --pod new:myFirstPod ${helloImage}`; |
| 75 | + |
| 76 | +function testComponent(name: string, fn: () => Promise<unknown>) { |
| 77 | + test(name, () => { |
| 78 | + vi.mocked(window.listImages).mockResolvedValue([imageInfo]); |
| 79 | + render(PodEmptyScreen); |
| 80 | + return fn(); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +testComponent('renders button to run first pod', async () => { |
| 85 | + expect(getButton()).toBeVisible(); |
| 86 | +}); |
| 87 | + |
| 88 | +testComponent('button click creates and starts a pod', async () => { |
| 89 | + vi.spyOn(window, 'createPod').mockResolvedValue(podInfo); |
| 90 | + await fireEvent.click(getButton()); |
| 91 | + expect(window.createPod).toBeCalledWith({ name: 'myFirstPod' }); |
| 92 | + expect(window.createAndStartContainer).toBeCalledWith(podInfo.engineId, { |
| 93 | + Image: helloImage, |
| 94 | + pod: 'myFirstPod', |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +testComponent('button click shows error message if creating pod fails', async () => { |
| 99 | + vi.spyOn(window, 'createPod').mockRejectedValue(error); |
| 100 | + await fireEvent.click(getButton()); |
| 101 | + expect(window.showMessageBox).toBeCalledWith(errorMessage); |
| 102 | +}); |
| 103 | + |
| 104 | +testComponent('button click shows error message if starting pod fails', async () => { |
| 105 | + vi.spyOn(window, 'createPod').mockResolvedValue(podInfo); |
| 106 | + vi.spyOn(window, 'createAndStartContainer').mockRejectedValue(error); |
| 107 | + await fireEvent.click(getButton()); |
| 108 | + vi.waitFor(() => expect(window.showMessageBox).toBeCalledWith(errorMessage)); |
| 109 | +}); |
| 110 | + |
| 111 | +test('button click shows error if image could not be pulled', async () => { |
| 112 | + vi.mocked(window.listImages).mockResolvedValue([]); |
| 113 | + render(PodEmptyScreen); |
| 114 | + await fireEvent.click(getButton()); |
| 115 | + vi.waitFor(() => expect(window.showMessageBox).toBeCalledWith(imageErrorMessage)); |
| 116 | +}); |
| 117 | + |
| 118 | +testComponent('button click shows error message if there is no active provider connection', async () => { |
| 119 | + providerInfos.set([]); |
| 120 | + await fireEvent.click(getButton()); |
| 121 | + vi.waitFor(() => expect(window.showMessageBox).toBeCalledWith(providerErrorMessage)); |
| 122 | +}); |
| 123 | + |
| 124 | +testComponent(`${copyToClipboard} button click puts starting pod command to clipboard`, async () => { |
| 125 | + await fireEvent.click(screen.getByTitle(copyToClipboard)); |
| 126 | + expect(window.clipboardWriteText).toBeCalledWith(podCreateCommand); |
| 127 | +}); |
0 commit comments