From 183f243615f31b7d911aaf8c14efa07a82ff1e0c Mon Sep 17 00:00:00 2001 From: Tim deBoer Date: Sun, 10 Dec 2023 22:58:22 -0500 Subject: [PATCH] chore: added tests Added tests for container and compose actions. Signed-off-by: Tim deBoer --- .../src/lib/compose/ComposeActions.spec.ts | 118 ++++++++++++++++++ .../src/lib/compose/ComposeActions.svelte | 4 +- .../lib/container/ContainerActions.spec.ts | 95 ++++++++++++++ 3 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 packages/renderer/src/lib/compose/ComposeActions.spec.ts create mode 100644 packages/renderer/src/lib/container/ContainerActions.spec.ts diff --git a/packages/renderer/src/lib/compose/ComposeActions.spec.ts b/packages/renderer/src/lib/compose/ComposeActions.spec.ts new file mode 100644 index 0000000000000..d078eb875ff45 --- /dev/null +++ b/packages/renderer/src/lib/compose/ComposeActions.spec.ts @@ -0,0 +1,118 @@ +/********************************************************************** + * 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, beforeEach, afterEach } from 'vitest'; +import { fireEvent, render, screen } from '@testing-library/svelte'; +import ComposeActions from './ComposeActions.svelte'; +import type { ComposeInfoUI } from './ComposeInfoUI'; +import type { ContainerInfoUI } from '../container/ContainerInfoUI'; + +const compose: ComposeInfoUI = { + engineId: 'podman', + engineType: 'podman', + name: 'my-compose-group', + status: 'STOPPED', + actionInProgress: false, + actionError: undefined, + containers: [ + { + actionInProgress: false, + actionError: undefined, + state: 'STOPPED', + } as ContainerInfoUI, + ], +} as ComposeInfoUI; + +const getContributedMenusMock = vi.fn(); +const updateMock = vi.fn(); + +beforeEach(() => { + (window as any).startContainersByLabel = vi.fn(); + (window as any).stopContainersByLabel = vi.fn(); + (window as any).restartContainersByLabel = vi.fn(); + (window as any).deleteContainersByLabel = vi.fn(); + + (window as any).getContributedMenus = getContributedMenusMock; + getContributedMenusMock.mockImplementation(() => Promise.resolve([])); +}); + +afterEach(() => { + vi.resetAllMocks(); + vi.clearAllMocks(); +}); + +test('Expect no error and status starting compose', async () => { + const { component } = render(ComposeActions, { compose }); + component.$on('update', updateMock); + + // click on start button + const startButton = screen.getByRole('button', { name: 'Start Compose' }); + await fireEvent.click(startButton); + + expect(compose.status).toEqual('STARTING'); + expect(compose.actionError).toEqual(''); + expect(compose.containers[0].state).toEqual('STARTING'); + expect(compose.containers[0].actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); + +test('Expect no error and status stopping compose', async () => { + const { component } = render(ComposeActions, { compose }); + component.$on('update', updateMock); + + // click on stop button + const stopButton = screen.getByRole('button', { name: 'Stop Compose' }); + await fireEvent.click(stopButton); + + expect(compose.status).toEqual('STOPPING'); + expect(compose.actionError).toEqual(''); + expect(compose.containers[0].state).toEqual('STOPPING'); + expect(compose.containers[0].actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); + +test('Expect no error and status restarting compose', async () => { + const { component } = render(ComposeActions, { compose }); + component.$on('update', updateMock); + + // click on restart button + const restartButton = screen.getByRole('button', { name: 'Restart Compose' }); + await fireEvent.click(restartButton); + + expect(compose.status).toEqual('RESTARTING'); + expect(compose.actionError).toEqual(''); + expect(compose.containers[0].state).toEqual('RESTARTING'); + expect(compose.containers[0].actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); + +test('Expect no error and status deleting compose', async () => { + const { component } = render(ComposeActions, { compose }); + component.$on('update', updateMock); + + // click on delete button + const deleteButton = screen.getByRole('button', { name: 'Delete Compose' }); + await fireEvent.click(deleteButton); + + expect(compose.status).toEqual('DELETING'); + expect(compose.actionError).toEqual(''); + expect(compose.containers[0].state).toEqual('DELETING'); + expect(compose.containers[0].actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); diff --git a/packages/renderer/src/lib/compose/ComposeActions.svelte b/packages/renderer/src/lib/compose/ComposeActions.svelte index 6d6fc9ddc9829..cfc9d6103bff8 100644 --- a/packages/renderer/src/lib/compose/ComposeActions.svelte +++ b/packages/renderer/src/lib/compose/ComposeActions.svelte @@ -33,7 +33,7 @@ function inProgress(inProgress: boolean, state?: string): void { compose.status = state; } - compose.containers.forEach(container => { + for (const container of compose.containers) { container.actionInProgress = inProgress; // reset error when starting task if (inProgress) { @@ -42,7 +42,7 @@ function inProgress(inProgress: boolean, state?: string): void { if (state) { container.state = state; } - }); + } dispatch('update', compose); } diff --git a/packages/renderer/src/lib/container/ContainerActions.spec.ts b/packages/renderer/src/lib/container/ContainerActions.spec.ts new file mode 100644 index 0000000000000..816d6856622e0 --- /dev/null +++ b/packages/renderer/src/lib/container/ContainerActions.spec.ts @@ -0,0 +1,95 @@ +/********************************************************************** + * 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, beforeEach, afterEach } from 'vitest'; +import { fireEvent, render, screen } from '@testing-library/svelte'; +import ContainerActions from './ContainerActions.svelte'; +import type { ContainerInfoUI } from './ContainerInfoUI'; + +const container: ContainerInfoUI = {} as ContainerInfoUI; + +const getContributedMenusMock = vi.fn(); +const updateMock = vi.fn(); + +beforeEach(() => { + (window as any).startContainer = vi.fn(); + (window as any).stopContainer = vi.fn(); + (window as any).restartContainer = vi.fn(); + (window as any).deleteContainer = vi.fn(); + + (window as any).getContributedMenus = getContributedMenusMock; + getContributedMenusMock.mockImplementation(() => Promise.resolve([])); +}); + +afterEach(() => { + vi.resetAllMocks(); + vi.clearAllMocks(); +}); + +test('Expect no error and status starting container', async () => { + const { component } = render(ContainerActions, { container }); + component.$on('update', updateMock); + + // click on start button + const startButton = screen.getByRole('button', { name: 'Start Container' }); + await fireEvent.click(startButton); + + expect(container.state).toEqual('STARTING'); + expect(container.actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); + +test('Expect no error and status stopping container', async () => { + const { component } = render(ContainerActions, { container }); + component.$on('update', updateMock); + + // click on stop button + const stopButton = screen.getByRole('button', { name: 'Stop Container' }); + await fireEvent.click(stopButton); + + expect(container.state).toEqual('STOPPING'); + expect(container.actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); + +test('Expect no error and status restarting container', async () => { + const { component } = render(ContainerActions, { container }); + component.$on('update', updateMock); + + // click on restart button + const restartButton = screen.getByRole('button', { name: 'Restart Container' }); + await fireEvent.click(restartButton); + + expect(container.state).toEqual('RESTARTING'); + expect(container.actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +}); + +test('Expect no error and status deleting container', async () => { + const { component } = render(ContainerActions, { container }); + component.$on('update', updateMock); + + // click on delete button + const deleteButton = screen.getByRole('button', { name: 'Delete Container' }); + await fireEvent.click(deleteButton); + + expect(container.state).toEqual('DELETING'); + expect(container.actionError).toEqual(''); + expect(updateMock).toHaveBeenCalled(); +});