Skip to content

Commit 76c91ca

Browse files
authored
chore: update ImageEmptyPage to include pullImage button (podman-desktop#8890)
* chore: update ImageEmptyPage to include pull image button Signed-off-by: Sonia Sandler <[email protected]>
1 parent 8579700 commit 76c91ca

File tree

2 files changed

+170
-3
lines changed

2 files changed

+170
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**********************************************************************
2+
* Copyright (C) 2024 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 type { ProviderStatus } from '@podman-desktop/api';
22+
import { render, screen } from '@testing-library/svelte';
23+
import userEvent from '@testing-library/user-event';
24+
import { beforeEach, expect, test, vi } from 'vitest';
25+
26+
import { providerInfos } from '/@/stores/providers';
27+
import type { ProviderContainerConnectionInfo, ProviderInfo } from '/@api/provider-info';
28+
29+
import ImageEmptyScreen from './ImageEmptyScreen.svelte';
30+
31+
const pullImageMock = vi.fn();
32+
const showMessageBoxMock = vi.fn();
33+
34+
beforeEach(() => {
35+
vi.resetAllMocks();
36+
(window as any).pullImage = pullImageMock;
37+
(window as any).showMessageBox = showMessageBoxMock;
38+
});
39+
40+
// set up a valid provider connection for pull image
41+
function setup() {
42+
const pStatus: ProviderStatus = 'started';
43+
const pInfo: ProviderContainerConnectionInfo = {
44+
name: 'test',
45+
displayName: 'test',
46+
status: 'started',
47+
endpoint: {
48+
socketPath: '',
49+
},
50+
type: 'podman',
51+
};
52+
const providerInfo = {
53+
id: 'test',
54+
internalId: 'id',
55+
name: '',
56+
containerConnections: [pInfo],
57+
kubernetesConnections: undefined,
58+
status: pStatus,
59+
containerProviderConnectionCreation: false,
60+
containerProviderConnectionInitialization: false,
61+
kubernetesProviderConnectionCreation: false,
62+
kubernetesProviderConnectionInitialization: false,
63+
links: undefined,
64+
detectionChecks: undefined,
65+
warnings: undefined,
66+
images: undefined,
67+
installationSupport: undefined,
68+
} as unknown as ProviderInfo;
69+
providerInfos.set([providerInfo]);
70+
}
71+
72+
test('expect page to have pull image button', async () => {
73+
render(ImageEmptyScreen);
74+
75+
const pullButton = screen.getByRole('button', { name: 'Pull your first image' });
76+
77+
expect(pullButton).toBeInTheDocument();
78+
79+
await userEvent.click(pullButton);
80+
});
81+
82+
test('expect error to show up in message box when pull has an error', async () => {
83+
setup();
84+
render(ImageEmptyScreen);
85+
86+
pullImageMock.mockRejectedValueOnce(new Error('Cannot pull image'));
87+
88+
const pullButton = screen.getByRole('button', { name: 'Pull your first image' });
89+
expect(pullButton).toBeInTheDocument();
90+
91+
await userEvent.click(pullButton);
92+
93+
expect(showMessageBoxMock).toBeCalledWith({
94+
title: `Error while pulling image`,
95+
message: `Error while pulling image from test: Cannot pull image`,
96+
});
97+
});
98+
99+
test('expect error to show up in message box with no providers', async () => {
100+
providerInfos.set([]);
101+
render(ImageEmptyScreen);
102+
103+
const pullButton = screen.getByRole('button', { name: 'Pull your first image' });
104+
105+
await userEvent.click(pullButton);
106+
107+
expect(showMessageBoxMock).toBeCalledWith({
108+
title: `Error while pulling image`,
109+
message: `No provider connections found`,
110+
});
111+
});
112+
113+
test('expect image to be pulled successfully', async () => {
114+
setup();
115+
render(ImageEmptyScreen);
116+
117+
const pullButton = screen.getByRole('button', { name: 'Pull your first image' });
118+
119+
await userEvent.click(pullButton);
120+
121+
expect(pullImageMock).toBeCalled();
122+
expect(showMessageBoxMock).not.toBeCalled();
123+
});
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,58 @@
11
<script lang="ts">
2-
import { EmptyScreen } from '@podman-desktop/ui-svelte';
2+
import { Button, EmptyScreen } from '@podman-desktop/ui-svelte';
3+
4+
import { providerInfos } from '/@/stores/providers';
35
46
import ImageIcon from '../images/ImageIcon.svelte';
57
6-
const commandLine = 'podman pull quay.io/podman/hello';
8+
$: selectedProviderConnection = $providerInfos
9+
.map(provider => provider.containerConnections)
10+
.flat()
11+
.find(providerContainerConnection => providerContainerConnection.status === 'started');
12+
13+
const firstImageName = 'quay.io/podman/hello';
14+
const commandLine = `podman pull ${firstImageName}`;
15+
let pullInProgress = false;
16+
17+
async function pullFirstImage() {
18+
if (!selectedProviderConnection) {
19+
await window.showMessageBox({
20+
title: `Error while pulling image`,
21+
message: `No provider connections found`,
22+
});
23+
return;
24+
}
25+
26+
pullInProgress = true;
27+
try {
28+
await window.pullImage(selectedProviderConnection, firstImageName, () => {});
29+
} catch (error: any) {
30+
const errorMessage = error.message ? error.message : error;
31+
await window.showMessageBox({
32+
title: `Error while pulling image`,
33+
message: `Error while pulling image from ${selectedProviderConnection.name}: ${errorMessage}`,
34+
});
35+
}
36+
pullInProgress = false;
37+
}
738
</script>
839

940
<EmptyScreen
1041
icon={ImageIcon}
1142
title="No images"
1243
message="Pull a first image using the following command line:"
1344
commandline={commandLine}
14-
on:click={() => window.clipboardWriteText(commandLine)} />
45+
on:click={() => window.clipboardWriteText(commandLine)}>
46+
<div slot="upperContent">
47+
<span class="text-[var(--pd-details-empty-sub-header)] max-w-[800px] text-pretty mx-2"
48+
>Pull a first image by clicking on this button:</span>
49+
<div class="flex gap-2 justify-center p-3">
50+
<Button
51+
title="Pull {firstImageName} image"
52+
type="primary"
53+
inProgress={pullInProgress}
54+
on:click={() => pullFirstImage()}>Pull your first image</Button>
55+
</div>
56+
<h1 class="text-xl text-[var(--pd-details-empty-header)]">OR</h1>
57+
</div>
58+
</EmptyScreen>

0 commit comments

Comments
 (0)