test: move packages test to Packages.test.tsx file

this commit move relevant packages test to packages.test.tsx
This commit is contained in:
Michal Gold 2024-08-13 15:45:23 +03:00 committed by Klara Simickova
parent 34ef6b2b95
commit 2168bc31d8
2 changed files with 355 additions and 355 deletions

View file

@ -1,11 +1,9 @@
import React from 'react';
import type { Router as RemixRouter } from '@remix-run/router';
import { screen, waitFor, within } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { selectCustomRepo } from './wizardTestUtils';
import { clickBack, clickNext, verifyCancelButton } from './wizardTestUtils';
import { clickBack, clickNext } from './wizardTestUtils';
import CreateImageWizard from '../../../Components/CreateImageWizard/CreateImageWizard';
import { renderCustomRoutesWithReduxRouter } from '../../testUtils';
@ -21,351 +19,6 @@ const routes = [
},
];
// The router is just initiliazed here, it's assigned a value in the tests
let router: RemixRouter | undefined = undefined;
const typeIntoSearchBox = async (searchTerm: string) => {
const user = userEvent.setup();
const searchbox = await screen.findByRole('textbox', {
name: /search packages/i,
});
await waitFor(() => user.click(searchbox));
await waitFor(() => user.type(searchbox, searchTerm));
};
const clearSearchBox = async () => {
const user = userEvent.setup();
const searchbox = await screen.findByRole('textbox', {
name: /search packages/i,
});
await waitFor(() => user.click(searchbox));
await waitFor(() => user.clear(searchbox));
};
const getAllCheckboxes = async () => {
const pkgTable = await screen.findByTestId('packages-table');
await screen.findAllByTestId('package-row');
const checkboxes = await within(pkgTable).findAllByRole('checkbox', {
name: /select row/i,
});
return checkboxes;
};
const toggleSelected = async () => {
const user = userEvent.setup();
const selected = await screen.findByRole('button', { name: /selected/i });
await waitFor(async () => user.click(selected));
};
const checkRecommendationsEmptyState = async () => {
await screen.findByRole('button', {
name: /Recommended Red Hat packages/,
});
await screen.findByText('Select packages to generate recommendations.');
};
describe('Step Packages', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
'imagewizard',
{},
routes
));
// select aws as upload destination
const uploadAws = await screen.findByTestId('upload-aws');
user.click(uploadAws);
await clickNext();
// aws step
const manualOption = await screen.findByRole('radio', {
name: /manually enter an account id\./i,
});
await waitFor(async () => user.click(manualOption));
await waitFor(async () =>
user.type(
await screen.findByRole('textbox', {
name: 'aws account id',
}),
'012345678901'
)
);
await clickNext();
// skip registration
await screen.findByRole('textbox', {
name: 'Select activation key',
});
const registrationCheckbox = await screen.findByTestId(
'automatically-register-checkbox'
);
user.click(registrationCheckbox);
await clickNext();
// skip OpenSCAP
await clickNext();
// skip snapshots
await clickNext();
// skip Repositories
await clickNext();
// skip fsc
await clickNext();
};
test('clicking Next loads Image name', async () => {
await setUp();
await clickNext();
await clickNext();
await screen.findByRole('heading', {
name: 'Details',
});
});
test('clicking Back loads repositories', async () => {
await setUp();
await clickBack();
await screen.findByRole('heading', {
name: /Custom repositories/i,
});
});
test('clicking Cancel loads landing page', async () => {
await setUp();
await verifyCancelButton(router);
});
test('should display search bar and toggle buttons', async () => {
await setUp();
await typeIntoSearchBox('test');
await screen.findByRole('button', {
name: /available/i,
});
await screen.findByRole('button', {
name: /selected/i,
});
});
test('should display default state', async () => {
await setUp();
await screen.findByText(
'Search above to add additionalpackages to your image.'
);
});
test('should display an exact match if found regardless of too many results', async () => {
await setUp();
await typeIntoSearchBox('testPkg-123');
await screen.findByTestId('exact-match-row');
await screen.findByRole('heading', {
name: /too many results to display/i,
});
});
test('search results should be sorted with most relevant results first', async () => {
await setUp();
await selectCustomRepo();
await typeIntoSearchBox('test');
const packagesTable = await screen.findByTestId('packages-table');
const getRows = async () =>
await within(packagesTable).findAllByTestId('package-row');
const availablePackages = await getRows();
await waitFor(() => expect(availablePackages).toHaveLength(6));
expect(availablePackages[0]).toHaveTextContent('test');
expect(availablePackages[1]).toHaveTextContent('test-sources');
expect(availablePackages[2]).toHaveTextContent('testPkg');
expect(availablePackages[3]).toHaveTextContent('testPkg-sources');
expect(availablePackages[4]).toHaveTextContent('lib-test');
expect(availablePackages[5]).toHaveTextContent('lib-test-sources');
});
test('selected packages are sorted the same way as available packages', async () => {
await setUp();
await selectCustomRepo();
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
for (const checkbox in checkboxes) {
user.click(checkboxes[checkbox]);
}
await toggleSelected();
const packagesTable = await screen.findByTestId('packages-table');
const getRows = async () =>
await within(packagesTable).findAllByTestId('package-row');
const availablePackages = await getRows();
await waitFor(() => expect(availablePackages).toHaveLength(6));
expect(availablePackages[0]).toHaveTextContent('test');
expect(availablePackages[1]).toHaveTextContent('test-sources');
expect(availablePackages[2]).toHaveTextContent('testPkg');
expect(availablePackages[3]).toHaveTextContent('testPkg-sources');
expect(availablePackages[4]).toHaveTextContent('lib-test');
expect(availablePackages[5]).toHaveTextContent('lib-test-sources');
});
test('selected packages persist throughout steps', async () => {
await setUp();
await typeIntoSearchBox('test');
const pkgTable = await screen.findByTestId('packages-table');
await screen.findAllByTestId('package-row');
const getFirstPkgCheckbox = async () =>
await within(pkgTable).findByRole('checkbox', {
name: /select row 0/i,
});
let firstPkgCheckbox = (await getFirstPkgCheckbox()) as HTMLInputElement;
expect(firstPkgCheckbox.checked).toEqual(false);
user.click(firstPkgCheckbox);
await waitFor(() => expect(firstPkgCheckbox.checked).toEqual(true));
await clickNext();
await clickBack();
firstPkgCheckbox = (await getFirstPkgCheckbox()) as HTMLInputElement;
expect(firstPkgCheckbox.checked).toEqual(true);
});
test('should display empty available state on failed search', async () => {
await setUp();
await typeIntoSearchBox('asdf');
await screen.findByText('No results found');
});
test('should display too many results state for more than 100 results', async () => {
await setUp();
await typeIntoSearchBox('te');
await screen.findByText('Too many results to display');
});
test('should display too short', async () => {
await setUp();
await typeIntoSearchBox('t');
await screen.findByText('The search value is too short');
});
test('should display relevant results in selected first', async () => {
await setUp();
await selectCustomRepo();
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
user.click(checkboxes[0]);
user.click(checkboxes[1]);
await clearSearchBox();
await typeIntoSearchBox('mock');
// wait for debounce
await waitFor(
() => {
expect(screen.getByText(/mockPkg/)).toBeInTheDocument();
},
{
timeout: 1500,
}
);
user.click(checkboxes[0]);
user.click(checkboxes[1]);
await toggleSelected();
await clearSearchBox();
await typeIntoSearchBox('test');
const packagesTable = await screen.findByTestId('packages-table');
const getRows = async () =>
await within(packagesTable).findAllByTestId('package-row');
const availablePackages = await getRows();
expect(availablePackages[0]).toHaveTextContent('test');
expect(availablePackages[1]).toHaveTextContent('test-sources');
});
test('should display recommendations', async () => {
await setUp();
await checkRecommendationsEmptyState();
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
user.click(checkboxes[0]);
await screen.findByText('recommendedPackage1');
await screen.findByText('recommendedPackage2');
await screen.findByText('recommendedPackage3');
});
test('allow to add recommendations to selected', async () => {
await setUp();
await checkRecommendationsEmptyState();
const pkgTable = await screen.findByTestId('packages-table');
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
user.click(checkboxes[0]);
const addRecButtons = await screen.findAllByTestId(
'add-recommendation-button'
);
user.click(addRecButtons[0]);
const selected = await screen.findByRole('button', { name: /Selected/ });
user.click(selected);
await within(pkgTable).findByText('recommendedPackage1');
});
});
describe('Step Custom repositories', () => {
beforeEach(() => {
vi.clearAllMocks();
@ -373,11 +26,7 @@ describe('Step Custom repositories', () => {
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
'imagewizard',
{},
routes
));
await renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);
// select aws as upload destination
const uploadAws = await screen.findByTestId('upload-aws');

View file

@ -1,6 +1,10 @@
import React from 'react';
import { Router as RemixRouter } from '@remix-run/router/dist/router';
import { screen, waitFor, within } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import CreateImageWizard from '../../../../../Components/CreateImageWizard/CreateImageWizard';
import { CREATE_BLUEPRINT, EDIT_BLUEPRINT } from '../../../../../constants';
import { CreateBlueprintRequest } from '../../../../../store/imageBuilderApi';
import { mockBlueprintIds } from '../../../../fixtures/blueprints';
@ -11,7 +15,12 @@ import {
expectedSinglePackageRecommendation,
packagesCreateBlueprintRequest,
} from '../../../../fixtures/editMode';
import { clickNext } from '../../wizardTestUtils';
import { renderCustomRoutesWithReduxRouter } from '../../../../testUtils';
import {
clickBack,
clickNext,
verifyCancelButton,
} from '../../wizardTestUtils';
import { selectCustomRepo } from '../../wizardTestUtils';
import {
blueprintRequest,
@ -62,6 +71,21 @@ const searchForGroup = async () => {
await waitFor(() => user.type(searchBox, '@grouper'));
};
const getRowsIn = async () => {
const packagesTable = await screen.findByTestId('packages-table');
const getRows = async () =>
await within(packagesTable).findAllByTestId('package-row');
const availablePackages = await getRows();
await waitFor(() => expect(availablePackages).toHaveLength(6));
expect(availablePackages[0]).toHaveTextContent('test');
expect(availablePackages[1]).toHaveTextContent('test-sources');
expect(availablePackages[2]).toHaveTextContent('testPkg');
expect(availablePackages[3]).toHaveTextContent('testPkg-sources');
expect(availablePackages[4]).toHaveTextContent('lib-test');
expect(availablePackages[5]).toHaveTextContent('lib-test-sources');
};
const clearSearchInput = async () => {
const user = userEvent.setup();
const clearSearchBtn = await screen.findByRole('button', {
@ -349,3 +373,330 @@ describe('package groups on packages step', () => {
await waitFor(() => expect(secondRowCells[0]).toHaveTextContent('fish2'));
});
});
const getAllCheckboxes = async () => {
const pkgTable = await screen.findByTestId('packages-table');
await screen.findAllByTestId('package-row');
const checkboxes = await within(pkgTable).findAllByRole('checkbox', {
name: /select row/i,
});
return checkboxes;
};
let router: RemixRouter | undefined = undefined;
const routes = [
{
path: 'insights/image-builder/*',
element: <div />,
},
{
path: 'insights/image-builder/imagewizard/:composeId?',
element: <CreateImageWizard />,
},
];
const typeIntoSearchBox = async (searchTerm: string) => {
const user = userEvent.setup();
const searchbox = await screen.findByRole('textbox', {
name: /search packages/i,
});
await waitFor(() => user.click(searchbox));
await waitFor(() => user.type(searchbox, searchTerm));
};
const checkRecommendationsEmptyState = async () => {
await screen.findByRole('button', {
name: /Recommended Red Hat packages/,
});
await screen.findByText('Select packages to generate recommendations.');
};
const clearSearchBox = async () => {
const user = userEvent.setup();
const searchbox = await screen.findByRole('textbox', {
name: /search packages/i,
});
await waitFor(() => user.click(searchbox));
await waitFor(() => user.clear(searchbox));
};
const toggleSelected = async () => {
const user = userEvent.setup();
const selected = await screen.findByRole('button', { name: /selected/i });
await waitFor(async () => user.click(selected));
};
describe('Step Packages', () => {
beforeEach(() => {
vi.clearAllMocks();
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = await renderCustomRoutesWithReduxRouter(
'imagewizard',
{},
routes
));
// select aws as upload destination
const uploadAws = await screen.findByTestId('upload-aws');
user.click(uploadAws);
await clickNext();
// aws step
const manualOption = await screen.findByRole('radio', {
name: /manually enter an account id\./i,
});
await waitFor(async () => user.click(manualOption));
await waitFor(async () =>
user.type(
await screen.findByRole('textbox', {
name: 'aws account id',
}),
'012345678901'
)
);
await clickNext();
// skip registration
await screen.findByRole('textbox', {
name: 'Select activation key',
});
const registrationCheckbox = await screen.findByTestId(
'automatically-register-checkbox'
);
user.click(registrationCheckbox);
await clickNext();
// skip OpenSCAP
await clickNext();
// skip snapshots
await clickNext();
// skip Repositories
await clickNext();
// skip fsc
await clickNext();
};
test('clicking Next loads Image name', async () => {
await setUp();
await clickNext();
await clickNext();
await screen.findByRole('heading', {
name: 'Details',
});
});
test('clicking Back loads repositories', async () => {
await setUp();
await clickBack();
await screen.findByRole('heading', {
name: /Custom repositories/i,
});
});
test('clicking Cancel loads landing page', async () => {
await setUp();
await verifyCancelButton(router);
});
test('should display search bar and toggle buttons', async () => {
await setUp();
await typeIntoSearchBox('test');
await screen.findByRole('button', {
name: /available/i,
});
await screen.findByRole('button', {
name: /selected/i,
});
});
test('should display default state', async () => {
await setUp();
await screen.findByText(
'Search above to add additionalpackages to your image.'
);
});
test('should display an exact match if found regardless of too many results', async () => {
await setUp();
await typeIntoSearchBox('testPkg-123');
await screen.findByTestId('exact-match-row');
await screen.findByRole('heading', {
name: /too many results to display/i,
});
});
test('search results should be sorted with most relevant results first', async () => {
await setUp();
await selectCustomRepo();
await typeIntoSearchBox('test');
await getRowsIn();
});
test('selected packages are sorted the same way as available packages', async () => {
await setUp();
await selectCustomRepo();
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
for (const checkbox in checkboxes) {
user.click(checkboxes[checkbox]);
}
await toggleSelected();
await getRowsIn();
});
test('selected packages persist throughout steps', async () => {
await setUp();
await typeIntoSearchBox('test');
const pkgTable = await screen.findByTestId('packages-table');
await screen.findAllByTestId('package-row');
const getFirstPkgCheckbox = async () =>
await within(pkgTable).findByRole('checkbox', {
name: /select row 0/i,
});
let firstPkgCheckbox = (await getFirstPkgCheckbox()) as HTMLInputElement;
expect(firstPkgCheckbox.checked).toEqual(false);
user.click(firstPkgCheckbox);
await waitFor(() => expect(firstPkgCheckbox.checked).toEqual(true));
await clickNext();
await clickBack();
firstPkgCheckbox = (await getFirstPkgCheckbox()) as HTMLInputElement;
expect(firstPkgCheckbox.checked).toEqual(true);
});
test('should display empty available state on failed search', async () => {
await setUp();
await typeIntoSearchBox('asdf');
await screen.findByText('No results found');
});
test('should display too many results state for more than 100 results', async () => {
await setUp();
await typeIntoSearchBox('te');
await screen.findByText('Too many results to display');
});
test('should display too short', async () => {
await setUp();
await typeIntoSearchBox('t');
await screen.findByText('The search value is too short');
});
test('should display relevant results in selected first', async () => {
await setUp();
await selectCustomRepo();
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
user.click(checkboxes[0]);
user.click(checkboxes[1]);
await clearSearchBox();
await typeIntoSearchBox('mock');
// wait for debounce
await waitFor(
() => {
expect(screen.getByText(/mockPkg/)).toBeInTheDocument();
},
{
timeout: 1500,
}
);
user.click(checkboxes[0]);
user.click(checkboxes[1]);
await toggleSelected();
await clearSearchBox();
await typeIntoSearchBox('test');
const packagesTable = await screen.findByTestId('packages-table');
const getRows = async () =>
await within(packagesTable).findAllByTestId('package-row');
const availablePackages = await getRows();
expect(availablePackages[0]).toHaveTextContent('test');
expect(availablePackages[1]).toHaveTextContent('test-sources');
});
test('should display recommendations', async () => {
await setUp();
await checkRecommendationsEmptyState();
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
user.click(checkboxes[0]);
await screen.findByText('recommendedPackage1');
await screen.findByText('recommendedPackage2');
await screen.findByText('recommendedPackage3');
});
test('allow to add recommendations to selected', async () => {
await setUp();
await checkRecommendationsEmptyState();
const pkgTable = await screen.findByTestId('packages-table');
await typeIntoSearchBox('test');
const checkboxes = await getAllCheckboxes();
user.click(checkboxes[0]);
const addRecButtons = await screen.findAllByTestId(
'add-recommendation-button'
);
user.click(addRecButtons[0]);
const selected = await screen.findByRole('button', { name: /Selected/ });
user.click(selected);
await within(pkgTable).findByText('recommendedPackage1');
});
});