forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerDetails.spec.ts
183 lines (144 loc) · 5.53 KB
/
ContainerDetails.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import '@testing-library/jest-dom/vitest';
import { test, expect, vi, beforeAll, beforeEach } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/svelte';
import ContainerDetails from './ContainerDetails.svelte';
import { get } from 'svelte/store';
import { containersInfos } from '/@/stores/containers';
import type { ContainerInfo } from '../../../../main/src/plugin/api/container-info';
import { router } from 'tinro';
import { lastPage } from '/@/stores/breadcrumb';
const listContainersMock = vi.fn();
const getContainerInspectMock = vi.fn();
const myContainer: ContainerInfo = {
Id: 'myContainer',
Labels: {},
Status: 'running',
engineId: 'engine0',
engineName: 'podman',
engineType: 'podman',
StartedAt: '',
Names: ['name0'],
Image: '',
ImageID: '',
Command: '',
Created: 0,
Ports: [],
State: '',
};
const deleteContainerMock = vi.fn();
vi.mock('xterm', () => {
return {
Terminal: vi.fn().mockReturnValue({ loadAddon: vi.fn(), open: vi.fn(), write: vi.fn(), clear: vi.fn() }),
};
});
beforeAll(() => {
(window as any).listContainers = listContainersMock;
(window as any).deleteContainer = deleteContainerMock;
(window as any).getContainerInspect = getContainerInspectMock;
(window as any).getConfigurationValue = vi.fn().mockReturnValue(12);
(window as any).logsContainer = vi.fn();
(window as any).matchMedia = vi.fn().mockReturnValue({
addListener: vi.fn(),
});
(window as any).ResizeObserver = vi.fn().mockReturnValue({ observe: vi.fn(), unobserve: vi.fn() });
});
beforeEach(() => {});
test('Expect logs when tty is not enabled', async () => {
router.goto('/');
containersInfos.set([myContainer]);
// spy router.goto
const routerGotoSpy = vi.spyOn(router, 'goto');
getContainerInspectMock.mockResolvedValue({
Config: {
Tty: false,
},
});
// render the component
render(ContainerDetails, { containerID: 'myContainer' });
// wait router.goto is called
while (routerGotoSpy.mock.calls.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
}
// grab current route and check we have been redirected to tty
const currentRoute = window.location;
expect(currentRoute.href).toBe('http://localhost:3000/logs');
expect(routerGotoSpy).toBeCalledWith('/logs');
});
test('Expect show tty if container has tty enabled', async () => {
router.goto('/');
containersInfos.set([myContainer]);
// spy router.goto
const routerGotoSpy = vi.spyOn(router, 'goto');
getContainerInspectMock.mockResolvedValue({
Config: {
Tty: true,
OpenStdin: true,
},
});
// render the component
render(ContainerDetails, { containerID: 'myContainer' });
// wait router.goto is called
while (routerGotoSpy.mock.calls.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
}
// grab current route and check we have been redirected to tty
const currentRoute = window.location;
expect(currentRoute.href).toBe('http://localhost:3000/tty');
expect(routerGotoSpy).toBeCalledWith('/tty');
});
test('Expect redirect to previous page if container is deleted', async () => {
router.goto('/');
getContainerInspectMock.mockResolvedValue({
Config: {},
});
const routerGotoSpy = vi.spyOn(router, 'goto');
listContainersMock.mockResolvedValue([myContainer]);
window.dispatchEvent(new CustomEvent('extensions-already-started'));
while (get(containersInfos).length !== 1) {
await new Promise(resolve => setTimeout(resolve, 500));
}
// remove myContainer from the store when we call 'deleteContainer'
// it will then refresh the store and update ContainerDetails page
deleteContainerMock.mockImplementation(() => {
containersInfos.update(containers => containers.filter(container => container.Id !== myContainer.Id));
});
// defines a fake lastPage so we can check where we will be redirected
lastPage.set({ name: 'Fake Previous', path: '/last' });
// render the component
render(ContainerDetails, { containerID: 'myContainer' });
// wait router.goto is called
while (routerGotoSpy.mock.calls.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
}
// grab current route
const currentRoute = window.location;
expect(currentRoute.href).toBe('http://localhost:3000/logs');
// click on delete container button
const deleteButton = screen.getByRole('button', { name: 'Delete Container' });
await fireEvent.click(deleteButton);
// check that delete method has been called
expect(deleteContainerMock).toHaveBeenCalled();
// expect that we have called the router when page has been removed
// to jump to the previous page
expect(routerGotoSpy).toBeCalledWith('/last');
// grab updated route
const afterRoute = window.location;
expect(afterRoute.href).toBe('http://localhost:3000/last');
});