test: Move beforeEach blocks inside describe

This moved the `beforeEach` blocks inside the `describe` blocks as per documentation.

Previously the code inside `beforeEach` didn't trigger which lead to problems with asynchrocinity as mocks didn't get cleared.
This commit is contained in:
regexowl 2024-07-08 11:15:31 +02:00 committed by Ondřej Ezr
parent e036f9b892
commit 98c17e55df
21 changed files with 242 additions and 55 deletions

View file

@ -41,6 +41,10 @@ const selectBlueprintById = async (bpId: string) => {
};
describe('Blueprints', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
const blueprintNameWithComposes = 'Dark Chocolate';
const blueprintIdWithComposes = '677b010b-e95e-4694-9813-d11d847f1bfc';
@ -186,6 +190,10 @@ describe('Blueprints', () => {
});
describe('edit blueprint', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const editedBlueprintName = 'Dark Chocolate';
const routes = [
{
@ -229,6 +237,10 @@ describe('Blueprints', () => {
});
describe('filtering', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('filter blueprints', async () => {
renderWithReduxRouter('', {});
@ -253,6 +265,10 @@ describe('Blueprints', () => {
});
describe('pagination', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('paging of blueprints', async () => {
renderWithReduxRouter('', {});
@ -286,6 +302,10 @@ describe('Blueprints', () => {
});
describe('composes filtering', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('filter composes by blueprint version', async () => {
renderWithReduxRouter('', {});

View file

@ -62,17 +62,6 @@ vi.mock('@unleash/proxy-client-react', () => ({
// The router is just initiliazed here, it's assigned a value in the tests
let router: RemixRouter | undefined = undefined;
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
vi.clearAllMocks();
router = undefined;
server.resetHandlers();
});
const getSourceDropdown = async () => {
const sourceDropdown = await screen.findByRole('textbox', {
name: /select source/i,
@ -83,6 +72,11 @@ const getSourceDropdown = async () => {
};
describe('Step Upload to Azure', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(

View file

@ -53,15 +53,6 @@ vi.mock('@unleash/proxy-client-react', () => ({
),
}));
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
vi.clearAllMocks();
});
const selectRhel8 = async () => {
const user = userEvent.setup();
await waitFor(async () =>
@ -87,6 +78,10 @@ const clickFromImageOutputToOpenScap = async () => {
};
describe('Step Compliance', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
const setup = async () => {
renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);

View file

@ -66,17 +66,6 @@ vi.mock('@unleash/proxy-client-react', () => ({
}),
}));
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
mockContentSourcesEnabled = true;
});
afterEach(() => {
vi.clearAllMocks();
mockContentSourcesEnabled = true;
});
const typeIntoSearchBox = async (searchTerm: string) => {
const user = userEvent.setup();
const searchbox = await screen.findByRole('textbox', {
@ -133,6 +122,12 @@ export const selectCustomRepo = async () => {
};
describe('Step Packages', () => {
beforeEach(() => {
vi.clearAllMocks();
mockContentSourcesEnabled = true;
});
const user = userEvent.setup();
const setUp = async () => {
mockContentSourcesEnabled = false;
@ -425,6 +420,11 @@ describe('Step Packages', () => {
});
describe('Step Custom repositories', () => {
beforeEach(() => {
vi.clearAllMocks();
mockContentSourcesEnabled = true;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(

View file

@ -100,18 +100,12 @@ const clickFromImageOutputToFsc = async () => {
await clickNext(); // skip OSCAP
};
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
vi.clearAllMocks();
router = undefined;
server.resetHandlers();
});
describe('Create Image Wizard', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
test('renders component', async () => {
renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);
// check heading
@ -129,6 +123,11 @@ describe('Create Image Wizard', () => {
});
describe('Step Image output', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
@ -291,6 +290,11 @@ describe('Step Image output', () => {
});
describe('Step Upload to AWS', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
@ -447,6 +451,11 @@ describe('Step Upload to AWS', () => {
});
describe('Step Upload to Google', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
@ -528,6 +537,11 @@ describe('Step Upload to Google', () => {
});
describe('Step Registration', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
@ -801,6 +815,11 @@ describe('Step Registration', () => {
});
describe('Step File system configuration', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
@ -992,6 +1011,11 @@ describe('Step Details', () => {
});
describe('Step Review', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
@ -1167,6 +1191,11 @@ describe('Step Review', () => {
});
describe('Keyboard accessibility', () => {
beforeEach(() => {
vi.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(

View file

@ -71,12 +71,15 @@ const goToReviewStep = async () => {
};
describe('validates name', () => {
beforeEach(async () => {
vi.clearAllMocks();
});
test('with invalid name', async () => {
await renderCreateMode();
await goToRegistrationStep();
await clickRegisterLater();
await goToDetailsStep();
const nextButton = await getNextButton();
expect(nextButton).toBeDisabled();
await enterBlueprintName(' ');
@ -99,13 +102,16 @@ describe('validates name', () => {
await clickRegisterLater();
await goToDetailsStep();
await enterBlueprintName('Lemon Pie');
const nextButton = await getNextButton();
await waitFor(() => expect(nextButton).toBeDisabled());
});
});
describe('registration request generated correctly', () => {
beforeEach(async () => {
vi.clearAllMocks();
});
test('without description', async () => {
await renderCreateMode();
await goToRegistrationStep();
@ -145,6 +151,10 @@ describe('registration request generated correctly', () => {
});
describe('Details edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['details'];
await renderEditMode(id);

View file

@ -130,6 +130,10 @@ const goToReviewStep = async () => {
};
describe('file system configuration request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('10 GiB / correct', async () => {
await renderCreateMode();
await goToFileSystemConfigurationStep();
@ -283,6 +287,10 @@ describe('file system configuration request generated correctly', () => {
});
describe('FSC edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['fsc'];
await renderEditMode(id);

View file

@ -98,6 +98,10 @@ const goToReviewStep = async (): Promise<void> => {
};
describe('First Boot step', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('should render First Boot step', async () => {
await renderCreateMode();
await goToFirstBootStep();
@ -131,6 +135,10 @@ describe('First Boot step', () => {
});
describe('First Boot edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['firstBoot'];
await renderEditMode(id);

View file

@ -28,7 +28,6 @@ import {
rhel9CreateBlueprintRequest,
x86_64CreateBlueprintRequest,
} from '../../../../fixtures/editMode';
import { server } from '../../../../mocks/server';
import {
clickNext,
renderCustomRoutesWithReduxRouter,
@ -79,16 +78,6 @@ vi.mock('@unleash/proxy-client-react', () => ({
useFlag: vi.fn(() => false),
}));
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
vi.clearAllMocks();
server.resetHandlers();
});
const openReleaseMenu = async () => {
const user = userEvent.setup();
const releaseMenu = screen.getAllByRole('button', {
@ -174,6 +163,10 @@ const goToReviewStep = async () => {
};
describe('Check that the target filtering is in accordance to mock content', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('rhel9 x86_64', async () => {
const user = userEvent.setup();
await renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);
@ -367,6 +360,10 @@ describe('Check that the target filtering is in accordance to mock content', ()
});
describe('Check step consistency', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('going back and forth with selected options only keeps the one compatible', async () => {
const user = userEvent.setup();
await renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);
@ -406,6 +403,10 @@ describe('Check step consistency', () => {
});
describe('set release using query parameter', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('rhel 9 by default (no query parameter)', async () => {
await renderCreateMode();
await screen.findByText('Red Hat Enterprise Linux (RHEL) 9');
@ -423,6 +424,10 @@ describe('set release using query parameter', () => {
});
describe('set architecture using query parameter', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('x86_64 by default (no query parameter)', async () => {
await renderCreateMode();
await screen.findByText('x86_64');
@ -440,6 +445,11 @@ describe('set architecture using query parameter', () => {
});
describe('set target using query parameter', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
test('no target by default (no query parameter)', async () => {
await renderCreateMode();
const nextButton = await screen.findByRole('button', { name: /Next/ });
@ -476,6 +486,10 @@ describe('set target using query parameter', () => {
});
describe('distribution request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('rhel-8', async () => {
await renderCreateMode();
await selectRhel8();
@ -526,6 +540,10 @@ describe('distribution request generated correctly', () => {
});
describe('architecture request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('x86_64', async () => {
await renderCreateMode();
await selectX86_64();
@ -566,6 +584,10 @@ describe('architecture request generated correctly', () => {
});
describe('Image Output edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works - rhel9', async () => {
const id = mockBlueprintIds['rhel9'];
await renderEditMode(id);

View file

@ -126,6 +126,10 @@ const expectedFilesystemCisL2 = [
];
describe('oscap', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('add a profile', async () => {
await renderCreateMode();
await goToOscapStep();
@ -190,6 +194,10 @@ describe('oscap', () => {
});
describe('OpenSCAP edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['oscap'];
await renderEditMode(id);

View file

@ -153,6 +153,10 @@ const openIncludedPackagesPopover = async () => {
};
describe('packages request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('with custom packages', async () => {
await renderCreateMode();
await goToPackagesStep();
@ -222,6 +226,10 @@ describe('packages request generated correctly', () => {
});
describe('package recommendations', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('selecting single recommendation adds it to the request', async () => {
await renderCreateMode();
await goToPackagesStep();
@ -285,6 +293,10 @@ describe('package recommendations', () => {
});
describe('Packages edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['packages'];
await renderEditMode(id);
@ -299,6 +311,10 @@ describe('Packages edit mode', () => {
});
describe('pagination on packages step', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('itemcount correct after search', async () => {
await renderCreateMode();
await goToPackagesStep();
@ -342,6 +358,10 @@ describe('pagination on packages step', () => {
});
describe('package groups on packages step', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('included packages popover', async () => {
await renderCreateMode();
await goToPackagesStep();

View file

@ -98,6 +98,10 @@ const goToReviewStep = async () => {
};
describe('registration request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const imageRequest: ImageRequest = {
architecture: 'x86_64',
image_type: 'guest-image',
@ -211,6 +215,10 @@ describe('registration request generated correctly', () => {
});
describe('Registration edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['registration'];
await renderEditMode(id);

View file

@ -93,6 +93,10 @@ const deselectFirstRepository = async () => {
};
describe('repositories request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('with custom repositories', async () => {
await renderCreateMode();
await goToRepositoriesStep();
@ -181,6 +185,11 @@ describe('repositories request generated correctly', () => {
});
describe('Repositories edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
test('edit mode works', async () => {
const id = mockBlueprintIds['repositories'];
await renderEditMode(id);

View file

@ -105,6 +105,10 @@ const getSnapshotMethodElement = async () =>
await screen.findByRole('button', { name: /Snapshot method/i });
describe('repository snapshot tab - ', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('select use a snapshot with 1 repo selected', async () => {
await renderCreateMode();
await goToSnapshotStep();
@ -160,6 +164,10 @@ describe('repository snapshot tab - ', () => {
});
describe('Snapshot edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['snapshot'];
await renderEditMode(id);

View file

@ -111,6 +111,10 @@ const enterAccountId = async () => {
};
describe('aws image type request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('using a source', async () => {
await selectAwsTarget();
await goToAwsStep();
@ -182,6 +186,10 @@ describe('aws image type request generated correctly', () => {
});
describe('AWS edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['aws'];
await renderEditMode(id);

View file

@ -147,6 +147,10 @@ const enterResourceGroup = async () => {
};
describe('azure image type request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('using a source', async () => {
await selectAzureTarget();
await goToAzureStep();
@ -225,6 +229,10 @@ describe('azure image type request generated correctly', () => {
});
describe('Azure edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['azure'];
await renderEditMode(id);

View file

@ -108,6 +108,10 @@ const selectGoogleAccount = async (optionId: string) => {
};
describe('gcp image type request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('share image with google account', async () => {
await clickGCPTarget();
await selectGoogleAccount('google-account');
@ -201,6 +205,10 @@ describe('gcp image type request generated correctly', () => {
});
describe('GCP edit mode', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('edit mode works', async () => {
const id = mockBlueprintIds['gcp'];
await renderEditMode(id);

View file

@ -33,6 +33,10 @@ vi.mock('@unleash/proxy-client-react', () => ({
}));
describe('Images Table', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
test('render ImagesTable', async () => {
await renderWithReduxRouter('', {});
@ -210,6 +214,10 @@ describe('Images Table', () => {
});
describe('Images Table Toolbar', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('render toolbar', async () => {
await renderWithReduxRouter('', {});
await screen.findByTestId('images-table');
@ -221,6 +229,10 @@ describe('Images Table Toolbar', () => {
});
describe('Clones table', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
test('renders clones table', async () => {
await renderWithReduxRouter('', {});

View file

@ -31,6 +31,10 @@ vi.mock('@unleash/proxy-client-react', () => ({
}));
describe('Landing Page', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('renders page heading', async () => {
renderWithReduxRouter('', {});

View file

@ -36,6 +36,10 @@ const routes = [
];
describe('Create Share To Regions Modal', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
test('validation', async () => {
await renderCustomRoutesWithReduxRouter(`share/${composeId}`, {}, routes);