Skip to content

Commit

Permalink
Refs #37752 - Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 committed Jan 8, 2025
1 parent 858ab2f commit 08aae79
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { renderWithRedux, patientlyWaitFor, fireEvent, act } from 'react-testing
import mockAvailableHostCollections from './availableHostCollections.fixtures.json';
import mockRemovableHostCollections from './removableHostCollections.fixtures.json';
import { REMOVABLE_HOST_COLLECTIONS_KEY } from '../HostCollectionsConstants';
import { assertNockRequest, mockAutocomplete, nockInstance } from '../../../../../../test-utils/nockWrapper';
import nock, { assertNockRequest, mockAutocomplete, nockInstance } from '../../../../../../test-utils/nockWrapper';
import katelloApi, { foremanApi } from '../../../../../../services/api';
import { HostCollectionsAddModal, HostCollectionsRemoveModal } from '../HostCollectionsModal';

Expand Down Expand Up @@ -45,6 +45,11 @@ describe('HostCollectionsAddModal', () => {
[firstHostCollection] = results;
});

afterEach(() => {
nock.cleanAll(); // Removes all interceptors
nock.restore(); // Restores HTTP to normal behavior
});

test('Calls API with available_for=host on page load', async (done) => {
const autocompleteScope = mockAutocomplete(nockInstance, autocompleteUrl);

Expand All @@ -66,7 +71,7 @@ describe('HostCollectionsAddModal', () => {
expect(getAllByText(firstHostCollection.name)[0]).toBeInTheDocument());
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done(); // Pass jest callback to confirm test is done
act(done);
});

test('Calls alterHostCollections with combined list of existing and new host collections', async (done) => {
Expand Down Expand Up @@ -100,20 +105,16 @@ describe('HostCollectionsAddModal', () => {
await patientlyWaitFor(() =>
expect(getAllByText(firstHostCollection.name)[0]).toBeInTheDocument());
const checkbox = getByRole('checkbox', { name: 'Select row 0' });
await act(async () => {
fireEvent.click(checkbox);
});
fireEvent.click(checkbox);
const addButton = getByRole('button', { name: 'Add' });
expect(addButton).toHaveAttribute('aria-disabled', 'false');
await act(async () => {
fireEvent.click(addButton);
});
fireEvent.click(addButton);

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(alterScope);
assertNockRequest(hostDetailsScope);
done();
act(done);
});
test('Host collections whose host limit is exceeded are disabled', async (done) => {
const autocompleteScope = mockAutocomplete(nockInstance, autocompleteUrl);
Expand Down Expand Up @@ -143,7 +144,7 @@ describe('HostCollectionsAddModal', () => {

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done(); // Pass jest callback to confirm test is done
act(done); // Pass jest callback to confirm test is done
});
});

Expand All @@ -153,6 +154,11 @@ describe('HostCollectionsRemoveModal', () => {
[firstHostCollection] = results;
});

afterEach(() => {
nock.cleanAll(); // Removes all interceptors
nock.restore(); // Restores HTTP to normal behavior
});

test('Calls API without available_for=host on page load', async (done) => {
const autocompleteScope = mockAutocomplete(nockInstance, autocompleteUrl);

Expand All @@ -176,7 +182,7 @@ describe('HostCollectionsRemoveModal', () => {
// Assert request was made and completed, see helper function
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done(); // Pass jest callback to confirm test is done
act(done); // Pass jest callback to confirm test is done
});

test('Calls alterHostCollections with host collections being removed filtered out from the list', async (done) => {
Expand Down Expand Up @@ -225,7 +231,7 @@ describe('HostCollectionsRemoveModal', () => {
assertNockRequest(scope);
assertNockRequest(alterScope);
assertNockRequest(hostDetailsScope);
done();
act(done);
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test('Can call API for packages and show on screen on page load', async (done) =
// Assert request was made and completed, see helper function
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done(); // Pass jest callback to confirm test is done
act(done);
});

test('Can handle no packages being present', async (done) => {
Expand All @@ -107,7 +107,7 @@ test('Can handle no packages being present', async (done) => {
// Assert request was made and completed, see helper function
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done(); // Pass jest callback to confirm test is done
act(done);
});

test('Can filter by package status', async (done) => {
Expand Down Expand Up @@ -137,13 +137,9 @@ test('Can filter by package status', async (done) => {

const statusDropdown = queryByText('Status', { ignore: 'th' });
expect(statusDropdown).toBeInTheDocument();
await act(async () => {
fireEvent.click(statusDropdown);
});
fireEvent.click(statusDropdown);
const upgradable = getByRole('option', { name: 'select Upgradable' });
await act(async () => {
fireEvent.click(upgradable);
});
fireEvent.click(upgradable);
await patientlyWaitFor(() => {
expect(queryByText('coreutils')).toBeInTheDocument();
expect(queryByText('acl')).not.toBeInTheDocument();
Expand All @@ -152,7 +148,7 @@ test('Can filter by package status', async (done) => {
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(scope2);
done(); // Pass jest callback to confirm test is done
act(done);
});

test('Can upgrade a package via remote execution', async (done) => {
Expand Down Expand Up @@ -191,33 +187,27 @@ test('Can upgrade a package via remote execution', async (done) => {

const statusDropdown = getByText('Status', { ignore: 'th' });
expect(statusDropdown).toBeInTheDocument();
await act(async () => {
fireEvent.click(statusDropdown);
});
fireEvent.click(statusDropdown);
const upgradable = getByRole('option', { name: 'select Upgradable' });
await act(async () => {
fireEvent.click(upgradable);
});
fireEvent.click(upgradable);
await patientlyWaitFor(() => {
expect(getByText('coreutils')).toBeInTheDocument();
});

const kebabDropdown = getByLabelText('Kebab toggle');
await act(async () => {
kebabDropdown.click();
});
kebabDropdown.click();

await patientlyWaitFor(() => expect(getByText('Upgrade via remote execution')).toBeInTheDocument());
const rexAction = getByText('Upgrade via remote execution');
await patientlyWaitFor(() => expect(rexAction).toBeInTheDocument());
await act(async () => {
fireEvent.click(rexAction);
});

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(statusScope);
assertNockRequest(upgradeScope);
done();
assertNockRequest(upgradeScope, done);
act(done);
});

test('Can upgrade a package via customized remote execution', async (done) => {
Expand All @@ -244,21 +234,15 @@ test('Can upgrade a package via customized remote execution', async (done) => {

const statusDropdown = getByText('Status', { ignore: 'th' });
expect(statusDropdown).toBeInTheDocument();
await act(async () => {
fireEvent.click(statusDropdown);
});
fireEvent.click(statusDropdown);
const upgradable = getByRole('option', { name: 'select Upgradable' });
await act(async () => {
fireEvent.click(upgradable);
});
fireEvent.click(upgradable);
await patientlyWaitFor(() => {
expect(getByText('coreutils')).toBeInTheDocument();
});

const kebabDropdown = getByLabelText('Kebab toggle');
await act(async () => {
kebabDropdown.click();
});
kebabDropdown.click();

const rexAction = getByText('Upgrade via customized remote execution');
const feature = REX_FEATURES.KATELLO_PACKAGE_UPDATE;
Expand All @@ -269,14 +253,14 @@ test('Can upgrade a package via customized remote execution', async (done) => {
'href',
`/job_invocations/new?feature=${feature}&search=name%20%5E%20(${hostname})&inputs%5Bpackage%5D=${packageName}`,
);

await act(async () => {
fireEvent.click(rexAction);
});

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(statusScope);
done();
assertNockRequest(statusScope, done);
act(done);
});

test('Can remove a package via remote execution', async (done) => {
Expand Down Expand Up @@ -307,16 +291,17 @@ test('Can remove a package via remote execution', async (done) => {

await patientlyWaitFor(() => expect(getAllByText(firstPackage.name)[0]).toBeInTheDocument());

const kebabDropdown = getByLabelText('Actions');
kebabDropdown.click();

const kebabDropdown = getByLabelText('Kebab toggle');
fireEvent.click(kebabDropdown);
await patientlyWaitFor(() => expect(getByText('Remove')).toBeInTheDocument());
const rexAction = getByText('Remove');
await patientlyWaitFor(() => expect(rexAction).toBeInTheDocument());
fireEvent.click(rexAction);

await act(async () => {
fireEvent.click(rexAction);
});
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(removeScope, done);
act(done);
});

test('Can bulk remove a package via remote execution', async (done) => {
Expand Down Expand Up @@ -360,11 +345,14 @@ test('Can bulk remove a package via remote execution', async (done) => {

const rexAction = getByText('Remove');
await patientlyWaitFor(() => expect(rexAction).toBeInTheDocument());
fireEvent.click(rexAction);
await act(async () => {
fireEvent.click(rexAction);
});

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(removeScope, done);
act(done);
});

test('Can bulk upgrade via remote execution', async (done) => {
Expand Down Expand Up @@ -404,9 +392,7 @@ test('Can bulk upgrade via remote execution', async (done) => {
expect(getByLabelText('Select row 1').checked).toEqual(true);

const upgradeDropdown = getAllByRole('button', { name: 'Select' })[1];
await act(async () => {
fireEvent.click(upgradeDropdown);
});
fireEvent.click(upgradeDropdown);
const rexAction = getByLabelText('bulk_upgrade_rex');
expect(rexAction).toBeInTheDocument();
await act(async () => {
Expand All @@ -416,7 +402,7 @@ test('Can bulk upgrade via remote execution', async (done) => {
assertNockRequest(autocompleteScope);
assertNockRequest(scope);
assertNockRequest(upgradeScope);
done();
act(done);
});

test('Can bulk upgrade via customized remote execution', async (done) => {
Expand Down Expand Up @@ -447,9 +433,7 @@ test('Can bulk upgrade via customized remote execution', async (done) => {
expect(getByLabelText('Select row 1').checked).toEqual(true);

const upgradeDropdown = getAllByRole('button', { name: 'Select' })[1];
await act(async () => {
fireEvent.click(upgradeDropdown);
});
fireEvent.click(upgradeDropdown);
expect(upgradeDropdown).not.toHaveAttribute('disabled');

const rexAction = getByLabelText('bulk_upgrade_customized_rex');
Expand All @@ -458,7 +442,7 @@ test('Can bulk upgrade via customized remote execution', async (done) => {

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done();
act(done);
});

test('Upgrade is disabled when there are non-upgradable packages selected', async (done) => {
Expand Down Expand Up @@ -489,7 +473,7 @@ test('Upgrade is disabled when there are non-upgradable packages selected', asyn

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done();
act(done);
});

test('Remove is disabled when in select all mode', async (done) => {
Expand All @@ -516,7 +500,7 @@ test('Remove is disabled when in select all mode', async (done) => {

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done();
act(done);
});

test('Sets initial search query from url params', async (done) => {
Expand All @@ -538,6 +522,6 @@ test('Sets initial search query from url params', async (done) => {

assertNockRequest(autocompleteScope);
assertNockRequest(scope);
done(); // Pass jest callback to confirm test is done
act(done);
});

Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,9 @@ test('Remove rpm filter rule in a self-closing modal', async (done) => {
expect(queryByText(cvFilterName)).toBeNull();
await patientlyWaitFor(() => {
expect(getByText(cvFilterName)).toBeInTheDocument();
expect(getAllByLabelText('Kebab toggle')[1]).toBeInTheDocument();
});
await act(async () => {
getAllByLabelText('Kebab toggle')[1].click();
expect(getAllByLabelText('Kebab toggle')[0]).toBeInTheDocument();
});
getAllByLabelText('Kebab toggle')[0].click();
await patientlyWaitFor(() => {
expect(getByText('Remove')).toBeInTheDocument();
});
Expand Down Expand Up @@ -338,16 +336,14 @@ test('Edit rpm filter rule in a self-closing modal', async (done) => {
expect(queryByText(cvFilterName)).toBeNull();
await patientlyWaitFor(() => {
expect(getByText(cvFilterName)).toBeInTheDocument();
expect(getAllByLabelText('Kebab toggle')[1]).toBeInTheDocument();
});
await act(async () => {
getAllByLabelText('Kebab toggle')[1].click();
expect(getAllByLabelText('Kebab toggle')[0]).toBeInTheDocument();
});
fireEvent.click(getAllByLabelText('Kebab toggle')[0]);
await patientlyWaitFor(() => {
expect(getByText('Edit')).toBeInTheDocument();
});

getByText('Edit').click();
fireEvent.click(getByText('Edit'));

await patientlyWaitFor(() => {
expect(getByText('Edit RPM rule')).toBeInTheDocument();
Expand Down Expand Up @@ -409,6 +405,6 @@ test('Shows call-to-action when there are no RPM filters', async (done) => {
assertNockRequest(autocompleteScope);
assertNockRequest(cvFiltersScope);
assertNockRequest(cvFilterDetailScope);
assertNockRequest(cvPackageFilterRulesScope);
assertNockRequest(cvPackageFilterRulesScope, done);
act(done);
});
Loading

0 comments on commit 08aae79

Please sign in to comment.