Skip to content

Commit 417e695

Browse files
committed
test: unit tests for NoContextPage component
Signed-off-by: Philippe Martin <[email protected]>
1 parent 15b79c6 commit 417e695

File tree

1 file changed

+119
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)