Skip to content

Commit 435d3d9

Browse files
test: add error and loading state tests for TargetEnvironment
- Test loading spinner displays while fetching architectures - Test error alert displays when fetching architectures fails - Add setupErrorHandler to mock fetch rejections
1 parent 6a62b06 commit 435d3d9

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/Components/CreateImageWizard/steps/ImageOutput/tests/TargetEnvironment.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
createCustomArchitecturesHandler,
1414
createDefaultFetchHandler,
1515
mockArchitecturesWithNetworkInstaller,
16+
setupErrorHandler,
1617
} from './mocks';
1718

1819
fetchMock.enableMocks();
@@ -211,4 +212,26 @@ describe('TargetEnvironment', () => {
211212
expect(networkInstallerCheckbox).toBeEnabled();
212213
});
213214
});
215+
216+
describe('Loading and error states', () => {
217+
test('displays loading spinner while fetching architectures', async () => {
218+
fetchMock.mockResponse(() => new Promise(() => {}));
219+
220+
renderTargetEnvironment();
221+
222+
expect(
223+
await screen.findByText(/loading target environments/i),
224+
).toBeInTheDocument();
225+
});
226+
227+
test('displays error alert when fetching architectures fails', async () => {
228+
setupErrorHandler();
229+
230+
renderTargetEnvironment();
231+
232+
expect(
233+
await screen.findByText(/couldn't be loaded/i),
234+
).toBeInTheDocument();
235+
});
236+
});
214237
});

src/Components/CreateImageWizard/steps/ImageOutput/tests/mocks/handlers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ export const createCustomArchitecturesHandler = (
2727
): FetchHandler => {
2828
return composeHandlers(createArchitecturesHandler({ architectures }));
2929
};
30+
31+
export const setupErrorHandler = (
32+
message: string = 'Internal Server Error',
33+
): void => {
34+
fetchMock.resetMocks();
35+
fetchMock.enableMocks();
36+
fetchMock.mockReject(new Error(message));
37+
};

src/test/Components/CreateImageWizard/steps/ImageOutput/ImageOutput.test.tsx

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import type { Router as RemixRouter } from '@remix-run/router';
2-
import { screen } from '@testing-library/react';
3-
import { http, HttpResponse } from 'msw';
4-
5-
import {
6-
EDIT_BLUEPRINT,
7-
IMAGE_BUILDER_API,
8-
RHEL_10,
9-
} from '../../../../../constants';
1+
// NOTE: Ready for migration
2+
// These edit mode tests verify the round-trip: API Response → mapRequestToState() →
3+
// Redux State → mapRequestFromState() → API Request. They could be replaced by unit
4+
// tests for the request mapper functions (mapRequestToState/mapRequestFromState) which
5+
// would be faster and more focused than full integration tests.
6+
import { EDIT_BLUEPRINT } from '../../../../../constants';
107
import { mockBlueprintIds } from '../../../../fixtures/blueprints';
118
import {
129
aarch64CreateBlueprintRequest,
@@ -15,35 +12,11 @@ import {
1512
rhel9CreateBlueprintRequest,
1613
x86_64CreateBlueprintRequest,
1714
} from '../../../../fixtures/editMode';
18-
import { server } from '../../../../mocks/server';
1915
import {
2016
interceptEditBlueprintRequest,
21-
renderCreateMode,
2217
renderEditMode,
2318
} from '../../wizardTestUtils';
2419

25-
// this is a weird false positive by eslint, the var is being used
26-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
27-
let router: RemixRouter | undefined = undefined;
28-
29-
describe('Step Image output', () => {
30-
beforeEach(() => {
31-
vi.clearAllMocks();
32-
router = undefined;
33-
});
34-
35-
test('alert gets rendered when fetching target environments fails', async () => {
36-
server.use(
37-
http.get(`${IMAGE_BUILDER_API}/architectures/${RHEL_10}`, () => {
38-
return new HttpResponse(null, { status: 404 });
39-
}),
40-
);
41-
42-
await renderCreateMode();
43-
await screen.findByText(/Couldn't fetch target environments/);
44-
});
45-
});
46-
4720
describe('Image output edit mode', () => {
4821
beforeEach(() => {
4922
vi.clearAllMocks();

0 commit comments

Comments
 (0)