|
| 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, screen } from '@testing-library/svelte'; |
| 22 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 23 | +import NoContextPage from './NoContextPage.svelte'; |
| 24 | +import { StatesMocks } from '/@/tests/state-mocks'; |
| 25 | +import { FakeStateObject } from '/@/state/util/fake-state-object.svelte'; |
| 26 | +import type { KubernetesProvidersInfo } from '@kubernetes-dashboard/channels'; |
| 27 | +import KubeIcon from '/@/component/icons/KubeIcon.svelte'; |
| 28 | +import KubernetesProviderCard from '/@/component/dashboard/KubernetesProviderCard.svelte'; |
| 29 | +import type { Unsubscriber } from 'svelte/store'; |
| 30 | + |
| 31 | +vi.mock(import('./KubernetesProviderCard.svelte')); |
| 32 | +vi.mock(import('/@/component/icons/KubeIcon.svelte')); |
| 33 | + |
| 34 | +const statesMocks = new StatesMocks(); |
| 35 | +let kubernetesProvidersMock: FakeStateObject<KubernetesProvidersInfo, void>; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + vi.resetAllMocks(); |
| 39 | + statesMocks.reset(); |
| 40 | + |
| 41 | + kubernetesProvidersMock = new FakeStateObject<KubernetesProvidersInfo, void>(); |
| 42 | + statesMocks.mock<KubernetesProvidersInfo, void>('stateKubernetesProvidersInfoUI', kubernetesProvidersMock); |
| 43 | +}); |
| 44 | + |
| 45 | +test('should render the Kubernetes icon', () => { |
| 46 | + render(NoContextPage); |
| 47 | + expect(KubeIcon).toHaveBeenCalledWith(expect.anything(), { size: '80' }); |
| 48 | +}); |
| 49 | + |
| 50 | +test('should render the main heading', () => { |
| 51 | + render(NoContextPage); |
| 52 | + |
| 53 | + const heading = screen.getByRole('heading', { level: 1 }); |
| 54 | + expect(heading).toHaveTextContent('No Kubernetes cluster'); |
| 55 | +}); |
| 56 | + |
| 57 | +test('should render the description text', () => { |
| 58 | + render(NoContextPage); |
| 59 | + |
| 60 | + const description = screen.getByText(/A Kubernetes cluster is a group of nodes/); |
| 61 | + expect(description).toBeInTheDocument(); |
| 62 | + expect(description).toHaveClass('text-[var(--pd-details-empty-sub-header)]', 'text-balance'); |
| 63 | +}); |
| 64 | + |
| 65 | +test('should render providers when data is available', () => { |
| 66 | + const mockProviders: KubernetesProvidersInfo = { |
| 67 | + providers: [ |
| 68 | + { |
| 69 | + id: 'provider-1', |
| 70 | + creationDisplayName: 'Provider 1', |
| 71 | + creationButtonTitle: 'Create Provider 1', |
| 72 | + emptyConnectionMarkdownDescription: 'Description 1', |
| 73 | + }, |
| 74 | + { |
| 75 | + id: 'provider-2', |
| 76 | + creationDisplayName: 'Provider 2', |
| 77 | + creationButtonTitle: 'Create Provider 2', |
| 78 | + emptyConnectionMarkdownDescription: 'Description 2', |
| 79 | + }, |
| 80 | + ], |
| 81 | + }; |
| 82 | + |
| 83 | + kubernetesProvidersMock.setData(mockProviders); |
| 84 | + render(NoContextPage); |
| 85 | + |
| 86 | + expect(KubernetesProviderCard).toHaveBeenCalledWith(expect.anything(), { provider: mockProviders.providers[0] }); |
| 87 | + expect(KubernetesProviderCard).toHaveBeenCalledWith(expect.anything(), { provider: mockProviders.providers[1] }); |
| 88 | +}); |
| 89 | + |
| 90 | +test('should handle providers with minimal data', () => { |
| 91 | + const mockProviders = { |
| 92 | + providers: [ |
| 93 | + { |
| 94 | + id: 'minimal-provider', |
| 95 | + }, |
| 96 | + ], |
| 97 | + }; |
| 98 | + |
| 99 | + kubernetesProvidersMock.setData(mockProviders); |
| 100 | + render(NoContextPage); |
| 101 | + |
| 102 | + expect(KubernetesProviderCard).toHaveBeenCalledWith(expect.anything(), { provider: mockProviders.providers[0] }); |
| 103 | +}); |
| 104 | + |
| 105 | +test('should call subscribe on mount', () => { |
| 106 | + render(NoContextPage); |
| 107 | + |
| 108 | + expect(kubernetesProvidersMock.subscribe).toHaveBeenCalledTimes(1); |
| 109 | +}); |
| 110 | + |
| 111 | +test('should call unsubscribe on unmount', () => { |
| 112 | + const unsubscribeMock: Unsubscriber = vi.fn(); |
| 113 | + vi.mocked(kubernetesProvidersMock.subscribe).mockReturnValue(unsubscribeMock); |
| 114 | + const component = render(NoContextPage); |
| 115 | + |
| 116 | + component.unmount(); |
| 117 | + expect(unsubscribeMock).toHaveBeenCalledTimes(1); |
| 118 | +}); |
0 commit comments