Skip to content

Commit

Permalink
feat: add containerImageName in context for dashboard/container contr…
Browse files Browse the repository at this point in the history
…ibution (podman-desktop#10262)

* feat: add containerImageName in context for dashboard/container contribution
Signed-off-by: Philippe Martin <[email protected]>

* fix: review

Co-authored-by: Florent BENOIT <[email protected]>
Signed-off-by: Philippe Martin <[email protected]>

---------

Signed-off-by: Philippe Martin <[email protected]>
Co-authored-by: Florent BENOIT <[email protected]>
  • Loading branch information
feloy and benoitf authored Dec 13, 2024
1 parent 1efdae0 commit 2ffea78
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/renderer/src/lib/container/ContainerActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { router } from 'tinro';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';

import ContributionActions from '/@/lib/actions/ContributionActions.svelte';
import { context } from '/@/stores/context';

import { ContextUI } from '../context/context';
import ContainerActions from './ContainerActions.svelte';
import type { ContainerInfoUI } from './ContainerInfoUI';

Expand All @@ -48,6 +52,8 @@ const getContributedMenusMock = vi.fn();
const updateMock = vi.fn();
const showMessageBoxMock = vi.fn();

vi.mock('/@/lib/actions/ContributionActions.svelte');

beforeEach(() => {
(window as any).showMessageBox = showMessageBoxMock;
(window as any).startContainer = vi.fn();
Expand Down Expand Up @@ -146,3 +152,29 @@ test('Expect Generate Kube to redirect to expected page', async () => {

expect(goToMock).toBeCalledWith(`/containers/container-id/kube`);
});

test('Expect ContributionsAction component is created with a contextUI containing containerImageName', async () => {
const contributionActionsMock = vi.mocked(ContributionActions);
const containerWithImageName = new ContainerInfoUIImpl(
'container-id',
'container-engine-id',
) as unknown as ContainerInfoUI;
containerWithImageName.image = 'quay.io/user/my-image';
const ctx = new ContextUI();
ctx.setValue('key1', 'value1');
context.set(ctx);
render(ContainerActions, { container: containerWithImageName });
await vi.waitFor(() => {
expect(contributionActionsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
contextUI: {
_value: {
containerImageName: 'quay.io/user/my-image',
key1: 'value1',
},
},
}),
);
});
});
25 changes: 23 additions & 2 deletions packages/renderer/src/lib/container/ContainerActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import {
faTrash,
} from '@fortawesome/free-solid-svg-icons';
import { DropdownMenu } from '@podman-desktop/ui-svelte';
import { createEventDispatcher, onMount } from 'svelte';
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
import type { Unsubscriber } from 'svelte/store';
import ContributionActions from '/@/lib/actions/ContributionActions.svelte';
import { withConfirmation } from '/@/lib/dialogs/messagebox-utils';
import { handleNavigation } from '/@/navigation';
import { context } from '/@/stores/context';
import { NavigationPage } from '/@api/navigation-page';
import type { Menu } from '../../../../main/src/plugin/menu-registry';
import { MenuContext } from '../../../../main/src/plugin/menu-registry';
import { ContextUI } from '../context/context';
import FlatMenu from '../ui/FlatMenu.svelte';
import ListItemButtonIcon from '../ui/ListItemButtonIcon.svelte';
import { ContainerGroupInfoTypeUI, type ContainerInfoUI } from './ContainerInfoUI';
Expand All @@ -29,13 +32,30 @@ export let container: ContainerInfoUI;
export let dropdownMenu = false;
export let detailed = false;
let globalContext: ContextUI;
let contextsUnsubscribe: Unsubscriber;
const dispatch = createEventDispatcher<{ update: ContainerInfoUI }>();
export let onUpdate: (update: ContainerInfoUI) => void = update => {
dispatch('update', update);
};
let contributions: Menu[] = [];
onMount(async () => {
contributions = await window.getContributedMenus(MenuContext.DASHBOARD_CONTAINER);
contextsUnsubscribe = context.subscribe(value => {
// Copy context, do not use reference
globalContext = new ContextUI();
const allValues = value.collectAllValues();
for (const k in allValues) {
globalContext.setValue(k, allValues[k]);
}
globalContext.setValue('containerImageName', container.image);
});
});
onDestroy(() => {
// unsubscribe from the store
contextsUnsubscribe?.();
});
function inProgress(inProgress: boolean, state?: string): void {
Expand Down Expand Up @@ -249,5 +269,6 @@ if (dropdownMenu) {
dropdownMenu={dropdownMenu}
contributions={contributions}
detailed={detailed}
onError={handleError} />
onError={handleError}
contextUI={globalContext} />
</svelte:component>

0 comments on commit 2ffea78

Please sign in to comment.