forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreferencesContainerConnectionRendering.spec.ts
213 lines (188 loc) · 6.64 KB
/
PreferencesContainerConnectionRendering.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**********************************************************************
* 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
***********************************************************************/
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
import '@testing-library/jest-dom';
import { test, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/svelte';
import PreferencesResourcesRendering from './PreferencesResourcesRendering.svelte';
import { providerInfos } from '../../stores/providers';
import type { ProviderContainerConnectionInfo, ProviderInfo } from '../../../../main/src/plugin/api/provider-info';
import userEvent from '@testing-library/user-event';
import { router } from 'tinro';
import PreferencesContainerConnectionRendering from './PreferencesContainerConnectionRendering.svelte';
import { lastPage } from '/@/stores/breadcrumb';
test('Expect that the right machine is displayed', async () => {
const socketPath = '/my/common-socket-path';
const podmanMachineName1 = 'podman machine 1';
const podmanMachineName2 = 'podman machine 2';
const podmanMachineName3 = 'podman machine 3';
const providerInfo: ProviderInfo = {
id: 'podman',
name: 'podman',
images: {
icon: 'img',
},
status: 'started',
warnings: undefined,
containerProviderConnectionCreation: true,
detectionChecks: undefined,
containerConnections: [
{
name: podmanMachineName1,
status: 'started',
endpoint: {
socketPath,
},
type: 'podman',
},
{
name: podmanMachineName2,
status: 'started',
endpoint: {
socketPath,
},
type: 'podman',
},
{
name: podmanMachineName3,
status: 'started',
endpoint: {
socketPath,
},
type: 'podman',
},
],
installationSupport: undefined,
internalId: '0',
kubernetesConnections: [],
kubernetesProviderConnectionCreation: true,
links: undefined,
containerProviderConnectionInitialization: false,
containerProviderConnectionCreationDisplayName: 'Podman machine',
kubernetesProviderConnectionInitialization: false,
};
// 3 connections with the same socket path
providerInfos.set([providerInfo]);
// encode name with base64 of the second machine
const name = Buffer.from(podmanMachineName2).toString('base64');
const connection = Buffer.from(socketPath).toString('base64');
render(PreferencesContainerConnectionRendering, {
name,
connection,
providerInternalId: '0',
});
// expect to have the second machine being displayed (and not the first one that also match socket path)
const title = screen.getByRole('heading', { name: 'podman machine 2', level: 1 });
expect(title).toBeInTheDocument();
});
test('Expect that removing the connection is going back to the previous page', async () => {
const socketPath = '/my/common-socket-path';
const podmanMachineName1 = 'podman machine 1';
const podmanMachineName2 = 'podman machine 2';
const podmanMachineName3 = 'podman machine 3';
const routerGotoSpy = vi.spyOn(router, 'goto');
const deleteMock = vi.fn();
(window as any).deleteProviderConnectionLifecycle = deleteMock;
const providerInfo: ProviderInfo = {
id: 'podman',
name: 'podman',
images: {
icon: 'img',
},
status: 'started',
warnings: undefined,
containerProviderConnectionCreation: true,
detectionChecks: undefined,
containerConnections: [
{
name: podmanMachineName1,
status: 'started',
endpoint: {
socketPath,
},
type: 'podman',
},
{
name: podmanMachineName2,
status: 'stopped',
endpoint: {
socketPath,
},
type: 'podman',
lifecycleMethods: ['delete'],
},
{
name: podmanMachineName3,
status: 'started',
endpoint: {
socketPath,
},
type: 'podman',
},
],
installationSupport: undefined,
internalId: '0',
kubernetesConnections: [],
kubernetesProviderConnectionCreation: true,
links: undefined,
containerProviderConnectionInitialization: false,
containerProviderConnectionCreationDisplayName: 'Podman machine',
kubernetesProviderConnectionInitialization: false,
};
// 3 connections with the same socket path
providerInfos.set([providerInfo]);
// encode name with base64 of the second machine
const name = Buffer.from(podmanMachineName2).toString('base64');
const connection = Buffer.from(socketPath).toString('base64');
// defines a fake lastPage so we can check where we will be redirected
lastPage.set({ name: 'Fake Previous', path: '/last' });
// delete current machine 2 from the provider info
deleteMock.mockImplementation(() => {
providerInfos.update(providerInfos =>
providerInfos.map(provider => {
provider.containerConnections = provider.containerConnections.filter(
connection => connection.name !== podmanMachineName2,
);
return provider;
}),
);
});
render(PreferencesContainerConnectionRendering, {
name,
connection,
providerInternalId: '0',
});
// expect to have the second machine being displayed (and not the first one that also match socket path)
const title = screen.getByRole('heading', { name: 'podman machine 2', level: 1 });
expect(title).toBeInTheDocument();
// ok now we delete the connection
const deleteButton = screen.getByRole('button', { name: 'Delete' });
// grab current route
const currentRoute = window.location;
expect(currentRoute.href).toBe('http://localhost:3000/');
// click on it
await userEvent.click(deleteButton);
// 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');
});