-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charlie Drage <[email protected]>
- Loading branch information
Showing
4 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/frontend/src/lib/disk-image/DiskImageConnectionStatus.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
2 changes: 1 addition & 1 deletion
2
...es/frontend/src/VMConnectionStatus.svelte → ...sk-image/DiskImageConnectionStatus.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/frontend/src/lib/disk-image/DiskImageDetailsVirtualMachine.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters