Skip to content

Commit

Permalink
chore: added tests
Browse files Browse the repository at this point in the history
Added tests for container and compose actions.

Signed-off-by: Tim deBoer <[email protected]>
  • Loading branch information
deboer-tim committed Dec 20, 2023
1 parent 008cc0f commit 183f243
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 2 deletions.
118 changes: 118 additions & 0 deletions packages/renderer/src/lib/compose/ComposeActions.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
4 changes: 2 additions & 2 deletions packages/renderer/src/lib/compose/ComposeActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -42,7 +42,7 @@ function inProgress(inProgress: boolean, state?: string): void {
if (state) {
container.state = state;
}
});
}
dispatch('update', compose);
}
Expand Down
95 changes: 95 additions & 0 deletions packages/renderer/src/lib/container/ContainerActions.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 183f243

Please sign in to comment.