|
17 | 17 | ***********************************************************************/ |
18 | 18 |
|
19 | 19 | import type { WebviewPanel, ExtensionContext } from '@podman-desktop/api'; |
20 | | -import { Uri, window } from '@podman-desktop/api'; |
21 | | -import { beforeEach, test, vi } from 'vitest'; |
| 20 | +import { kubernetes, Uri, window } from '@podman-desktop/api'; |
| 21 | +import { assert, beforeEach, describe, expect, test, vi } from 'vitest'; |
22 | 22 | import { DashboardExtension } from './dashboard-extension'; |
| 23 | +import { vol } from 'memfs'; |
23 | 24 |
|
24 | | -import { readFile } from 'node:fs/promises'; |
| 25 | +import type { ContextsManager } from './manager/contexts-manager'; |
| 26 | +import type { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher'; |
25 | 27 |
|
26 | 28 | let extensionContextMock: ExtensionContext; |
27 | 29 | let dashboardExtension: DashboardExtension; |
| 30 | +let contextsManagerMock: ContextsManager; |
| 31 | +let contextsStatesDispatcher: ContextsStatesDispatcher; |
28 | 32 |
|
29 | 33 | vi.mock(import('node:fs')); |
30 | 34 | vi.mock(import('node:fs/promises')); |
| 35 | +vi.mock(import('@kubernetes/client-node')); |
31 | 36 |
|
32 | 37 | beforeEach(() => { |
33 | 38 | vi.restoreAllMocks(); |
34 | 39 | vi.resetAllMocks(); |
| 40 | + vol.reset(); |
35 | 41 |
|
36 | 42 | vi.mocked(window.createWebviewPanel).mockReturnValue({ |
37 | 43 | webview: { |
38 | 44 | html: '', |
39 | 45 | onDidReceiveMessage: vi.fn(), |
40 | 46 | }, |
41 | 47 | } as unknown as WebviewPanel); |
42 | | - vi.mocked(Uri.joinPath).mockReturnValue({ fsPath: '' } as unknown as Uri); |
43 | | - vi.mocked(readFile).mockResolvedValue('<html></html>'); |
| 48 | + vi.mocked(Uri.joinPath).mockReturnValue({ fsPath: '/path/to/extension/index.html' } as unknown as Uri); |
44 | 49 | // Create a mock for the ExtensionContext |
45 | 50 | extensionContextMock = { |
46 | 51 | subscriptions: [], |
47 | 52 | } as unknown as ExtensionContext; |
48 | | - dashboardExtension = new DashboardExtension(extensionContextMock); |
| 53 | + // Create a mock for the contextsManager |
| 54 | + contextsManagerMock = { |
| 55 | + update: vi.fn(), |
| 56 | + } as unknown as ContextsManager; |
| 57 | + contextsStatesDispatcher = { |
| 58 | + init: vi.fn(), |
| 59 | + } as unknown as ContextsStatesDispatcher; |
| 60 | + dashboardExtension = new DashboardExtension(extensionContextMock, contextsManagerMock, contextsStatesDispatcher); |
| 61 | + vi.mocked(kubernetes.getKubeconfig).mockReturnValue({ |
| 62 | + path: '/path/to/kube/config', |
| 63 | + } as Uri); |
49 | 64 | }); |
50 | 65 |
|
51 | | -test('should activate correctly', async () => { |
52 | | - await dashboardExtension.activate(); |
| 66 | +describe('a kubeconfig file is not present', () => { |
| 67 | + beforeEach(() => { |
| 68 | + vol.fromJSON({ |
| 69 | + '/path/to/extension/index.html': '<html></html>', |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + test('should activate correctly and calls contextsManager every time the kubeconfig file changes', async () => { |
| 74 | + await dashboardExtension.activate(); |
| 75 | + expect(contextsManagerMock.update).not.toHaveBeenCalled(); |
| 76 | + |
| 77 | + const callback = vi.mocked(kubernetes.onDidUpdateKubeconfig).mock.lastCall?.[0]; |
| 78 | + assert(callback); |
| 79 | + vi.mocked(contextsManagerMock.update).mockClear(); |
| 80 | + callback({ type: 'UPDATE', location: { path: '/path/to/kube/config' } as Uri }); |
| 81 | + expect(contextsManagerMock.update).toHaveBeenCalledOnce(); |
| 82 | + |
| 83 | + expect(contextsStatesDispatcher.init).toHaveBeenCalledOnce(); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should deactivate correctly', async () => { |
| 87 | + await dashboardExtension.activate(); |
| 88 | + await dashboardExtension.deactivate(); |
| 89 | + }); |
53 | 90 | }); |
54 | 91 |
|
55 | | -test('should deactivate correctly', async () => { |
56 | | - await dashboardExtension.activate(); |
57 | | - await dashboardExtension.deactivate(); |
| 92 | +describe('a kubeconfig file is present', () => { |
| 93 | + beforeEach(() => { |
| 94 | + vol.fromJSON({ |
| 95 | + '/path/to/extension/index.html': '<html></html>', |
| 96 | + '/path/to/kube/config': '{}', |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + test('should activate correctly and calls contextsManager every time the kubeconfig file changes', async () => { |
| 101 | + await dashboardExtension.activate(); |
| 102 | + expect(contextsManagerMock.update).toHaveBeenCalledOnce(); |
| 103 | + |
| 104 | + const callback = vi.mocked(kubernetes.onDidUpdateKubeconfig).mock.lastCall?.[0]; |
| 105 | + assert(callback); |
| 106 | + vi.mocked(contextsManagerMock.update).mockClear(); |
| 107 | + callback({ type: 'UPDATE', location: { path: '/path/to/kube/config' } as Uri }); |
| 108 | + expect(contextsManagerMock.update).toHaveBeenCalledOnce(); |
| 109 | + |
| 110 | + expect(contextsStatesDispatcher.init).toHaveBeenCalledOnce(); |
| 111 | + }); |
| 112 | + |
| 113 | + test('should deactivate correctly', async () => { |
| 114 | + await dashboardExtension.activate(); |
| 115 | + await dashboardExtension.deactivate(); |
| 116 | + }); |
58 | 117 | }); |
0 commit comments