|
18 | 18 |
|
19 | 19 | import '@testing-library/jest-dom/vitest';
|
20 | 20 |
|
| 21 | +import { within } from '@testing-library/dom'; |
21 | 22 | import { fireEvent, render, screen, waitFor } from '@testing-library/svelte';
|
22 |
| -import { afterEach, beforeAll, beforeEach, expect, test, vi } from 'vitest'; |
| 23 | +import { afterEach, beforeAll, beforeEach, expect, type Mock, test, vi } from 'vitest'; |
23 | 24 |
|
24 | 25 | import type { ContainerInfoUI } from '../container/ContainerInfoUI';
|
25 | 26 | import ComposeActions from './ComposeActions.svelte';
|
@@ -64,6 +65,21 @@ const getContributedMenusMock = vi.fn();
|
64 | 65 | const updateMock = vi.fn();
|
65 | 66 | const showMessageBoxMock = vi.fn();
|
66 | 67 |
|
| 68 | +type Deferred<T = void> = { |
| 69 | + promise: Promise<T>; |
| 70 | + resolve: (value: T | PromiseLike<T>) => void; |
| 71 | + reject: (reason?: unknown) => void; |
| 72 | +}; |
| 73 | +const createDeferred = <T = void>(): Deferred<T> => { |
| 74 | + let resolve!: (v: T | PromiseLike<T>) => void; |
| 75 | + let reject!: (e?: unknown) => void; |
| 76 | + const promise = new Promise<T>((res, rej) => { |
| 77 | + resolve = res; |
| 78 | + reject = rej; |
| 79 | + }); |
| 80 | + return { promise, resolve, reject }; |
| 81 | +}; |
| 82 | + |
67 | 83 | beforeAll(() => {
|
68 | 84 | Object.defineProperty(window, 'showMessageBox', { value: showMessageBoxMock });
|
69 | 85 | Object.defineProperty(window, 'startContainersByLabel', { value: vi.fn() });
|
@@ -143,3 +159,112 @@ test('Expect no error and status deleting compose', async () => {
|
143 | 159 | expect(compose.containers[0].actionError).toEqual('');
|
144 | 160 | expect(updateMock).toHaveBeenCalled();
|
145 | 161 | });
|
| 162 | + |
| 163 | +test('Stop keeps Start hidden during STOPPING (all containers are running)', async () => { |
| 164 | + const composeAllRunning: ComposeInfoUI = new ComposeInfoUIImpl( |
| 165 | + 'podman', |
| 166 | + 'podman', |
| 167 | + 'compose-all-running', |
| 168 | + 'RUNNING', |
| 169 | + false, |
| 170 | + undefined, |
| 171 | + [ |
| 172 | + { state: 'RUNNING', actionInProgress: false } as ContainerInfoUI, |
| 173 | + { state: 'RUNNING', actionInProgress: false } as ContainerInfoUI, |
| 174 | + ], |
| 175 | + ); |
| 176 | + |
| 177 | + const { container } = render(ComposeActions, { compose: composeAllRunning, onUpdate: updateMock }); |
| 178 | + const ui = within(container); |
| 179 | + |
| 180 | + expect(ui.getByRole('button', { name: 'Start Compose', hidden: true })).toHaveClass('hidden'); |
| 181 | + expect(ui.getByRole('button', { name: 'Stop Compose' })).not.toHaveClass('hidden'); |
| 182 | + |
| 183 | + const dStop = createDeferred<void>(); |
| 184 | + (window.stopContainersByLabel as unknown as Mock).mockReturnValueOnce(dStop.promise); |
| 185 | + |
| 186 | + await fireEvent.click(ui.getByRole('button', { name: 'Stop Compose' })); |
| 187 | + |
| 188 | + await waitFor(() => { |
| 189 | + expect(composeAllRunning.actionInProgress).toBe(true); |
| 190 | + expect(composeAllRunning.status).toBe('STOPPING'); |
| 191 | + }); |
| 192 | + |
| 193 | + dStop.resolve(); |
| 194 | + |
| 195 | + expect(ui.getByRole('button', { name: 'Start Compose', hidden: true })).toHaveClass('hidden'); |
| 196 | + expect(ui.getByRole('button', { name: 'Stop Compose' })).not.toHaveClass('hidden'); |
| 197 | +}); |
| 198 | + |
| 199 | +test('Start keeps Stop hidden during STARTING (all containers are stopped)', async () => { |
| 200 | + const composeAllStopped: ComposeInfoUI = new ComposeInfoUIImpl( |
| 201 | + 'podman', |
| 202 | + 'podman', |
| 203 | + 'compose-all-stopped', |
| 204 | + 'STOPPED', |
| 205 | + false, |
| 206 | + undefined, |
| 207 | + [ |
| 208 | + { state: 'STOPPED', actionInProgress: false } as ContainerInfoUI, |
| 209 | + { state: 'STOPPED', actionInProgress: false } as ContainerInfoUI, |
| 210 | + ], |
| 211 | + ); |
| 212 | + |
| 213 | + const { container } = render(ComposeActions, { compose: composeAllStopped, onUpdate: updateMock }); |
| 214 | + const ui = within(container); |
| 215 | + |
| 216 | + expect(ui.getByRole('button', { name: 'Start Compose' })).not.toHaveClass('hidden'); |
| 217 | + expect(ui.getByRole('button', { name: 'Stop Compose', hidden: true })).toHaveClass('hidden'); |
| 218 | + |
| 219 | + const dStart = createDeferred<void>(); |
| 220 | + (window.startContainersByLabel as unknown as Mock).mockReturnValueOnce(dStart.promise); |
| 221 | + |
| 222 | + await fireEvent.click(ui.getByRole('button', { name: 'Start Compose' })); |
| 223 | + |
| 224 | + await waitFor(() => { |
| 225 | + expect(composeAllStopped.actionInProgress).toBe(true); |
| 226 | + expect(composeAllStopped.status).toBe('STARTING'); |
| 227 | + }); |
| 228 | + |
| 229 | + dStart.resolve(); |
| 230 | + |
| 231 | + expect(ui.getByRole('button', { name: 'Stop Compose', hidden: true })).toHaveClass('hidden'); |
| 232 | + expect(ui.getByRole('button', { name: 'Start Compose' })).not.toHaveClass('hidden'); |
| 233 | +}); |
| 234 | + |
| 235 | +test('Stop keeps both visible during STOPPING (some containers are running)', async () => { |
| 236 | + const composePartial: ComposeInfoUI = new ComposeInfoUIImpl( |
| 237 | + 'podman', |
| 238 | + 'podman', |
| 239 | + 'compose-partial', |
| 240 | + 'RUNNING', |
| 241 | + false, |
| 242 | + undefined, |
| 243 | + [ |
| 244 | + { state: 'RUNNING', actionInProgress: false } as ContainerInfoUI, |
| 245 | + { state: 'STOPPED', actionInProgress: false } as ContainerInfoUI, |
| 246 | + { state: 'RUNNING', actionInProgress: false } as ContainerInfoUI, |
| 247 | + ], |
| 248 | + ); |
| 249 | + |
| 250 | + const { container } = render(ComposeActions, { compose: composePartial, onUpdate: updateMock }); |
| 251 | + const ui = within(container); |
| 252 | + |
| 253 | + expect(ui.getByRole('button', { name: 'Start Compose' })).not.toHaveClass('hidden'); |
| 254 | + expect(ui.getByRole('button', { name: 'Stop Compose' })).not.toHaveClass('hidden'); |
| 255 | + |
| 256 | + const dStop = createDeferred<void>(); |
| 257 | + (window.stopContainersByLabel as unknown as Mock).mockReturnValueOnce(dStop.promise); |
| 258 | + |
| 259 | + await fireEvent.click(ui.getByRole('button', { name: 'Stop Compose' })); |
| 260 | + |
| 261 | + await waitFor(() => { |
| 262 | + expect(composePartial.actionInProgress).toBe(true); |
| 263 | + expect(composePartial.status).toBe('STOPPING'); |
| 264 | + }); |
| 265 | + |
| 266 | + dStop.resolve(); |
| 267 | + |
| 268 | + expect(ui.getByRole('button', { name: 'Start Compose' })).not.toHaveClass('hidden'); |
| 269 | + expect(ui.getByRole('button', { name: 'Stop Compose' })).not.toHaveClass('hidden'); |
| 270 | +}); |
0 commit comments