Skip to content

Commit

Permalink
fix: update current context reactively (#5055)
Browse files Browse the repository at this point in the history
* fix: update current context reactively
Signed-off-by: Philippe Martin <phmartin@redhat.com>

* fix: use find istead of filter
Signed-off-by: Philippe Martin <phmartin@redhat.com>
  • Loading branch information
feloy committed Nov 30, 2023
1 parent 98f23af commit 9f25c64
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Expand Up @@ -18,7 +18,7 @@

import '@testing-library/jest-dom/vitest';
import { beforeEach, expect, test, vi } from 'vitest';
import { render, screen } from '@testing-library/svelte';
import { fireEvent, render, screen, within } from '@testing-library/svelte';
import PreferencesKubernetesContextsRendering from './PreferencesKubernetesContextsRendering.svelte';
import { kubernetesContexts } from '/@/stores/kubernetes-contexts';
import type { KubeContext } from '../../../../main/src/plugin/kubernetes-context';
Expand Down Expand Up @@ -79,3 +79,39 @@ test('Test that context-name2 is the current context', async () => {
expect(spanContextName).toBeInTheDocument();
expect(spanContextName.parentElement).toEqual(currentContext.parentElement);
});

test('when deleting the current context, a popup should ask confirmation', async () => {
const showMessageBoxMock = vi.fn();
(window as any).showMessageBox = showMessageBoxMock;
showMessageBoxMock.mockResolvedValue({ result: 1 });

render(PreferencesKubernetesContextsRendering, {});
const currentContext = screen.getAllByRole('row')[1];
expect(currentContext).toBeInTheDocument();

const label = within(currentContext).queryByLabelText('current-context');
expect(label).toBeInTheDocument();

const deleteBtn = within(currentContext).getByRole('button', { name: 'Delete Context' });
expect(deleteBtn).toBeInTheDocument();
await fireEvent.click(deleteBtn);
expect(showMessageBoxMock).toHaveBeenCalledOnce();
});

test('when deleting the non current context, no popup should ask confirmation', async () => {
const showMessageBoxMock = vi.fn();
(window as any).showMessageBox = showMessageBoxMock;
showMessageBoxMock.mockResolvedValue({ result: 1 });

render(PreferencesKubernetesContextsRendering, {});
const currentContext = screen.getAllByRole('row')[0];
expect(currentContext).toBeInTheDocument();

const label = within(currentContext).queryByLabelText('current-context');
expect(label).not.toBeInTheDocument();

const deleteBtn = within(currentContext).getByRole('button', { name: 'Delete Context' });
expect(deleteBtn).toBeInTheDocument();
await fireEvent.click(deleteBtn);
expect(showMessageBoxMock).not.toHaveBeenCalled();
});
Expand Up @@ -2,19 +2,14 @@
import SettingsPage from './SettingsPage.svelte';
import EngineIcon from '../ui/EngineIcon.svelte';
import EmptyScreen from '../ui/EmptyScreen.svelte';
import { onMount } from 'svelte';
import Link from '../ui/Link.svelte';
import { faTrash, faRightToBracket } from '@fortawesome/free-solid-svg-icons';
import ListItemButtonIcon from '../ui/ListItemButtonIcon.svelte';
import ErrorMessage from '../ui/ErrorMessage.svelte';
import { kubernetesContexts } from '../../stores/kubernetes-contexts';
import { clearKubeUIContextErrors, setKubeUIContextError } from '../kube/KubeContextUI';
let currentContextName: string | undefined;
onMount(async () => {
currentContextName = await window.kubernetesGetCurrentContextName();
});
$: currentContextName = $kubernetesContexts.find(c => c.currentContext)?.name;
async function handleSetContext(contextName: string) {
$kubernetesContexts = clearKubeUIContextErrors($kubernetesContexts);
Expand Down

0 comments on commit 9f25c64

Please sign in to comment.