Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Drage <[email protected]>
  • Loading branch information
cdrage committed Oct 17, 2024
1 parent ac16c3b commit 89f5a7a
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**********************************************************************
* Copyright (C) 2024 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 { render, screen } from '@testing-library/svelte';
import '@testing-library/jest-dom/vitest';
import { test, expect } from 'vitest';
import DiskImageConnectionStatus from './DiskImageConnectionStatus.svelte';

test('Expect to show connection status if status is connected', async () => {
render(DiskImageConnectionStatus, { status: 'connected' });

// Find span element with connected status
expect(screen.getByText('connected')).toBeDefined();

// Find status element, use the inner div of that element to check if it includes bg-[var(--pd-status-connected)]
const statusElement = screen.getByRole('status');
if (!statusElement) {
throw new Error('Status element not found');
}
const innerDiv = statusElement.querySelector('div');
if (!innerDiv) {
throw new Error('Inner div not found');
}
expect(innerDiv.classList.contains('bg-[var(--pd-status-connected)]')).toBeDefined();
});

test('Expect to show disconnected status if VM error passed in', async () => {
render(DiskImageConnectionStatus, { status: 'VM error' });

// Find span element with connected status
expect(screen.getByText('VM error')).toBeDefined();

// Find status element, use the inner div of that element to check if it includes bg-[var(--pd-status-disconnected)]
const statusElement = screen.getByRole('status');
if (!statusElement) {
throw new Error('Status element not found');
}
const innerDiv = statusElement.querySelector('div');
if (!innerDiv) {
throw new Error('Inner div not found');
}
expect(innerDiv.classList.contains('bg-[var(--pd-status-disconnected)]')).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import Label from './lib/upstream/Label.svelte';
import Label from '/@/lib/upstream/Label.svelte';
export let status: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**********************************************************************
* Copyright (C) 2024 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 { render, screen, waitFor } from '@testing-library/svelte';
import { vi, test, expect, beforeAll } from 'vitest';
import { bootcClient } from '/@/api/client';
import DiskImageDetailsVirtualMachine from './DiskImageDetailsVirtualMachine.svelte';
import type { BootcBuildInfo } from '/@shared/src/models/bootc';

vi.mock('/@/api/client', async () => {
return {
rpcBrowser: {
subscribe: () => {
return {
unsubscribe: () => {},
};
},
},
bootcClient: {
listHistoryInfo: vi.fn(),
getConfigurationValue: vi.fn(),
stopCurrentVM: vi.fn(),
checkVMLaunchPrereqs: vi.fn(),
launchVM: vi.fn(),
},
};
});

beforeAll(() => {
(window as any).ResizeObserver = ResizeObserver;
(window as any).getConfigurationValue = vi.fn().mockResolvedValue(undefined);
(window as any).matchMedia = vi.fn().mockReturnValue({
addListener: vi.fn(),
});

Object.defineProperty(window, 'matchMedia', {
value: () => {
return {
matches: false,
addListener: () => {},
removeListener: () => {},
};
},
});
});

class ResizeObserver {
observe = vi.fn();
disconnect = vi.fn();
unobserve = vi.fn();
}

test('Render virtual machine terminal window', async () => {
vi.mocked(bootcClient.getConfigurationValue).mockResolvedValue(14);

// Use BootcBuildInfo to render the component
const build = {
id: 'id1',
image: 'my-image',
imageId: 'image-id',
tag: 'latest',
engineId: 'podman',
type: ['ami'],
folder: '/bootc',
} as BootcBuildInfo;

render(DiskImageDetailsVirtualMachine, { build });

// Wait for 'launchVM' to have been called
await waitFor(() => {
expect(bootcClient.launchVM).toHaveBeenCalled();
});
});

test('Show prereqs message if prereq check fails (returns ANY string)', async () => {
vi.mocked(bootcClient.checkVMLaunchPrereqs).mockResolvedValue('Prereq check failed');

const build = {
id: 'id1',
image: 'my-image',
imageId: 'image-id',
tag: 'latest',
engineId: 'podman',
type: ['ami'],
folder: '/bootc',
} as BootcBuildInfo;

render(DiskImageDetailsVirtualMachine, { build });

// Expect prereq failure to be shown
await waitFor(() => {
expect(screen.queryByText('Prereq check failed')).toBeDefined();
});
});

test('Test failed launched VM showing in render', async () => {
vi.mocked(bootcClient.checkVMLaunchPrereqs).mockResolvedValue(undefined);
vi.mocked(bootcClient.launchVM).mockRejectedValue('Failed to launch VM');

const build = {
id: 'id1',
image: 'my-image',
imageId: 'image-id',
tag: 'latest',
engineId: 'podman',
type: ['ami'],
folder: '/bootc',
} as BootcBuildInfo;

render(DiskImageDetailsVirtualMachine, { build });

// Expect prereq failure to be shown
await waitFor(() => {
expect(screen.queryByText('Failed to launch VM')).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { onDestroy, onMount } from 'svelte';
import { router } from 'tinro';
import { bootcClient, rpcBrowser } from '../../api/client';
import { getTerminalTheme } from '../upstream/terminal-theme';
import VMConnectionStatus from '../../VMConnectionStatus.svelte';
import DiskImageConnectionStatus from './DiskImageConnectionStatus.svelte';
import Link from '../Link.svelte';
import { Messages } from '/@shared/src/messages/Messages';
import type { Subscriber } from '/@shared/src/messages/MessageProxy';
Expand Down Expand Up @@ -298,7 +298,7 @@ export function goToHomePage(): void {
{/if}

<div class="absolute top-[70px] right-[5px]">
<VMConnectionStatus status={connectionStatus} />
<DiskImageConnectionStatus status={connectionStatus} />
</div>
<div
class="min-w-full flex flex-col p-[5px] pb-[10px] pr-0 bg-[var(--pd-terminal-background)]"
Expand Down

0 comments on commit 89f5a7a

Please sign in to comment.