|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2025 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 { render, fireEvent } from '@testing-library/svelte'; |
| 22 | +import { beforeEach, test, vi, expect } from 'vitest'; |
| 23 | +import KubeYamlEditor from '/@/lib/monaco-editor/KubeYamlEditor.svelte'; |
| 24 | +import type { QuadletInfo } from '/@shared/src/models/quadlet-info'; |
| 25 | +import { QuadletType } from '/@shared/src/utils/quadlet-type'; |
| 26 | +import type { ProviderContainerConnectionIdentifierInfo } from '/@shared/src/models/provider-container-connection-identifier-info'; |
| 27 | +import { quadletAPI } from '/@/api/client'; |
| 28 | +import MonacoEditor from '/@/lib/monaco-editor/MonacoEditor.svelte'; |
| 29 | + |
| 30 | +// mock monaco editor |
| 31 | +vi.mock('/@/lib/monaco-editor/MonacoEditor.svelte'); |
| 32 | +// mock clients |
| 33 | +vi.mock('/@/api/client', () => ({ |
| 34 | + quadletAPI: { |
| 35 | + getKubeYAML: vi.fn(), |
| 36 | + }, |
| 37 | +})); |
| 38 | + |
| 39 | +const MOCK_YAML = ` |
| 40 | +foo=bar |
| 41 | +`; |
| 42 | + |
| 43 | +beforeEach(() => { |
| 44 | + vi.resetAllMocks(); |
| 45 | + |
| 46 | + vi.mocked(quadletAPI.getKubeYAML).mockResolvedValue(MOCK_YAML); |
| 47 | +}); |
| 48 | + |
| 49 | +const PODMAN_MACHINE_DEFAULT: ProviderContainerConnectionIdentifierInfo = { |
| 50 | + name: 'podman-machine-default', |
| 51 | + providerId: 'podman', |
| 52 | +}; |
| 53 | + |
| 54 | +const KUBE_QUADLET: QuadletInfo & { type: QuadletType.KUBE } = { |
| 55 | + type: QuadletType.KUBE, |
| 56 | + id: `foo.bar`, |
| 57 | + path: `/mnt/foo/bar.kube`, |
| 58 | + content: 'dummy-content', |
| 59 | + state: 'active', |
| 60 | + connection: PODMAN_MACHINE_DEFAULT, |
| 61 | +}; |
| 62 | + |
| 63 | +test('ensure reload button is visible', async () => { |
| 64 | + const { getByTitle } = render(KubeYamlEditor, { |
| 65 | + quadlet: KUBE_QUADLET, |
| 66 | + loading: false, |
| 67 | + }); |
| 68 | + |
| 69 | + const reloadBtn = getByTitle('Reload file'); |
| 70 | + expect(reloadBtn).toBeInTheDocument(); |
| 71 | + // onmount pull the yaml so need to wait for completion |
| 72 | + await vi.waitFor(() => { |
| 73 | + expect(reloadBtn).toBeEnabled(); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test('ensure reload button is disabled when loading true', async () => { |
| 78 | + const { getByTitle } = render(KubeYamlEditor, { |
| 79 | + quadlet: KUBE_QUADLET, |
| 80 | + loading: true, |
| 81 | + }); |
| 82 | + |
| 83 | + const reloadBtn = getByTitle('Reload file'); |
| 84 | + expect(reloadBtn).toBeInTheDocument(); |
| 85 | + expect(reloadBtn).toBeDisabled(); |
| 86 | +}); |
| 87 | + |
| 88 | +test('expect reload button to call quadletAPI#getKubeYAML', async () => { |
| 89 | + const { getByTitle } = render(KubeYamlEditor, { |
| 90 | + quadlet: KUBE_QUADLET, |
| 91 | + loading: false, |
| 92 | + }); |
| 93 | + |
| 94 | + const reloadBtn = getByTitle('Reload file'); |
| 95 | + vi.mocked(quadletAPI.getKubeYAML).mockReset(); |
| 96 | + await fireEvent.click(reloadBtn); |
| 97 | + |
| 98 | + await vi.waitFor(() => { |
| 99 | + expect(quadletAPI.getKubeYAML).toHaveBeenCalledOnce(); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +test('expect result from quadletAPI#getKubeYAML to be displayed in monaco editor', async () => { |
| 104 | + render(KubeYamlEditor, { |
| 105 | + quadlet: KUBE_QUADLET, |
| 106 | + loading: false, |
| 107 | + }); |
| 108 | + |
| 109 | + await vi.waitFor(() => { |
| 110 | + expect(quadletAPI.getKubeYAML).toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + await vi.waitFor(() => { |
| 114 | + expect(MonacoEditor).toHaveBeenCalledWith( |
| 115 | + expect.anything(), |
| 116 | + expect.objectContaining({ |
| 117 | + content: MOCK_YAML, |
| 118 | + }), |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +test('expect error from quadletAPI#getKubeYAML to be displayed', async () => { |
| 124 | + vi.mocked(quadletAPI.getKubeYAML).mockRejectedValue(new Error('Dummy foo error')); |
| 125 | + |
| 126 | + const { getByRole } = render(KubeYamlEditor, { |
| 127 | + quadlet: KUBE_QUADLET, |
| 128 | + loading: false, |
| 129 | + }); |
| 130 | + |
| 131 | + await vi.waitFor(() => { |
| 132 | + expect(quadletAPI.getKubeYAML).toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + await vi.waitFor(() => { |
| 136 | + const alert = getByRole('alert'); |
| 137 | + expect(alert).toHaveTextContent('Something went wrong: Error: Dummy foo error'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments