|
| 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 type { ProviderStatus } from '@podman-desktop/api'; |
| 22 | +import { render, screen } from '@testing-library/svelte'; |
| 23 | +import userEvent from '@testing-library/user-event'; |
| 24 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 25 | + |
| 26 | +import { providerInfos } from '/@/stores/providers'; |
| 27 | +import type { ProviderContainerConnectionInfo, ProviderInfo } from '/@api/provider-info'; |
| 28 | + |
| 29 | +import ImageEmptyScreen from './ImageEmptyScreen.svelte'; |
| 30 | + |
| 31 | +const pullImageMock = vi.fn(); |
| 32 | +const showMessageBoxMock = vi.fn(); |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + vi.resetAllMocks(); |
| 36 | + (window as any).pullImage = pullImageMock; |
| 37 | + (window as any).showMessageBox = showMessageBoxMock; |
| 38 | +}); |
| 39 | + |
| 40 | +// set up a valid provider connection for pull image |
| 41 | +function setup() { |
| 42 | + const pStatus: ProviderStatus = 'started'; |
| 43 | + const pInfo: ProviderContainerConnectionInfo = { |
| 44 | + name: 'test', |
| 45 | + displayName: 'test', |
| 46 | + status: 'started', |
| 47 | + endpoint: { |
| 48 | + socketPath: '', |
| 49 | + }, |
| 50 | + type: 'podman', |
| 51 | + }; |
| 52 | + const providerInfo = { |
| 53 | + id: 'test', |
| 54 | + internalId: 'id', |
| 55 | + name: '', |
| 56 | + containerConnections: [pInfo], |
| 57 | + kubernetesConnections: undefined, |
| 58 | + status: pStatus, |
| 59 | + containerProviderConnectionCreation: false, |
| 60 | + containerProviderConnectionInitialization: false, |
| 61 | + kubernetesProviderConnectionCreation: false, |
| 62 | + kubernetesProviderConnectionInitialization: false, |
| 63 | + links: undefined, |
| 64 | + detectionChecks: undefined, |
| 65 | + warnings: undefined, |
| 66 | + images: undefined, |
| 67 | + installationSupport: undefined, |
| 68 | + } as unknown as ProviderInfo; |
| 69 | + providerInfos.set([providerInfo]); |
| 70 | +} |
| 71 | + |
| 72 | +test('expect page to have pull image button', async () => { |
| 73 | + render(ImageEmptyScreen); |
| 74 | + |
| 75 | + const pullButton = screen.getByRole('button', { name: 'Pull your first image' }); |
| 76 | + |
| 77 | + expect(pullButton).toBeInTheDocument(); |
| 78 | + |
| 79 | + await userEvent.click(pullButton); |
| 80 | +}); |
| 81 | + |
| 82 | +test('expect error to show up in message box when pull has an error', async () => { |
| 83 | + setup(); |
| 84 | + render(ImageEmptyScreen); |
| 85 | + |
| 86 | + pullImageMock.mockRejectedValueOnce(new Error('Cannot pull image')); |
| 87 | + |
| 88 | + const pullButton = screen.getByRole('button', { name: 'Pull your first image' }); |
| 89 | + expect(pullButton).toBeInTheDocument(); |
| 90 | + |
| 91 | + await userEvent.click(pullButton); |
| 92 | + |
| 93 | + expect(showMessageBoxMock).toBeCalledWith({ |
| 94 | + title: `Error while pulling image`, |
| 95 | + message: `Error while pulling image from test: Cannot pull image`, |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +test('expect error to show up in message box with no providers', async () => { |
| 100 | + providerInfos.set([]); |
| 101 | + render(ImageEmptyScreen); |
| 102 | + |
| 103 | + const pullButton = screen.getByRole('button', { name: 'Pull your first image' }); |
| 104 | + |
| 105 | + await userEvent.click(pullButton); |
| 106 | + |
| 107 | + expect(showMessageBoxMock).toBeCalledWith({ |
| 108 | + title: `Error while pulling image`, |
| 109 | + message: `No provider connections found`, |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +test('expect image to be pulled successfully', async () => { |
| 114 | + setup(); |
| 115 | + render(ImageEmptyScreen); |
| 116 | + |
| 117 | + const pullButton = screen.getByRole('button', { name: 'Pull your first image' }); |
| 118 | + |
| 119 | + await userEvent.click(pullButton); |
| 120 | + |
| 121 | + expect(pullImageMock).toBeCalled(); |
| 122 | + expect(showMessageBoxMock).not.toBeCalled(); |
| 123 | +}); |
0 commit comments