Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-suse committed Aug 29, 2024
1 parent c2a26fe commit ebeec53
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen, act } from '@testing-library/react';
import { render, screen, act, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { faker } from '@faker-js/faker';
import {
Expand Down Expand Up @@ -91,10 +91,10 @@ describe('ApiKeySettingsModal', () => {

await user.type(screen.getByRole('spinbutton'), '20');

await user.click(screen.getByRole('button', { name: 'months' }));
await user.click(screen.getByRole('button', { name: 'months' }));

await user.click(screen.getByRole('button', { name: 'Generate' }));

await user.click(await waitFor(() => screen.findByText('Generate')));
await user.click(screen.getByRole('button', { name: 'Generate' }));

const [{ apiKeyExpiration: generatedApiKeyExpiration }] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { render } from '@testing-library/react';
import TimeSeriesLineChart from './TimeSeriesLineChart';
import '@testing-library/jest-dom';

jest.mock('react-chartjs-2', () => ({
Line: () => <canvas />,
}));

describe('TimeSeriesLineChart component', () => {
it('should raise an error if the datasets are more then 5', () => {
const datasets = [
Expand Down Expand Up @@ -92,7 +96,6 @@ describe('TimeSeriesLineChart component', () => {
</div>
);

const canvas = container.querySelector('canvas');
expect(canvas).toBeDefined();
expect(container.querySelector('canvas')).toBeDefined();
});
});
6 changes: 4 additions & 2 deletions assets/js/pages/ChecksCatalog/CheckItem.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';

Expand Down Expand Up @@ -67,6 +67,8 @@ describe('ChecksCatalog CheckItem component', () => {
await user.click(checkDiv);
expect(screen.getByText(check.remediation)).toBeVisible();
await user.click(checkDiv);
expect(screen.queryByText(check.remediation)).not.toBeInTheDocument();
await waitFor(() =>
expect(screen.queryByText(check.remediation)).not.toBeInTheDocument()
);
});
});
4 changes: 2 additions & 2 deletions assets/js/pages/ChecksCatalog/ChecksCatalog.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ describe('ChecksCatalog ChecksCatalog component', () => {
await user.click(screen.getByText(filter));

expect(
screen.getByText(expectDisabled, { exact: false }).closest('li')
screen.getByText(expectDisabled, { exact: false }).closest('div')
).toHaveAttribute('aria-disabled', 'true');

const expectItemEnabled = (itemExpectedEnabled) =>
expect(
screen.getByText(itemExpectedEnabled).closest('li')
screen.getByText(itemExpectedEnabled).closest('div')
).not.toHaveAttribute('aria-disabled');

if (expectEnabled) {
Expand Down
3 changes: 2 additions & 1 deletion assets/js/pages/Users/UserForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,11 @@ describe('UserForm', () => {

await user.click(screen.getByText('Disabled'));

expect(screen.getAllByText('Enabled')[1].closest('li')).toHaveAttribute(
expect(screen.getAllByText('Enabled')[1].closest('div')).toHaveAttribute(
'aria-disabled',
'true'
);

expect(mockOnSave).not.toHaveBeenCalled();
});

Expand Down
9 changes: 5 additions & 4 deletions assets/js/pages/Users/Users.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import '@testing-library/jest-dom';
import { screen, render } from '@testing-library/react';
import { screen, render, waitFor } from '@testing-library/react';
import { adminUser, userFactory } from '@lib/test-utils/factories/users';
import { renderWithRouter } from '@lib/test-utils';
import { userEvent } from '@testing-library/user-event';
Expand Down Expand Up @@ -44,6 +44,7 @@ describe('Users', () => {
enabled: true,
created_at: creationTime[0],
});

const user = userFactory.build({
enabled: false,
created_at: creationTime[1],
Expand Down Expand Up @@ -90,10 +91,10 @@ describe('Users', () => {
expect(screen.getAllByText(user.username)[1]).toBeVisible();

const cancelButton = screen.getByRole('button', { name: /Cancel/i });
const modalTitel = screen.getByText(modalHeader);
expect(modalTitel).toBeInTheDocument();
const modalTitle = screen.getByText(modalHeader);
expect(modalTitle).toBeInTheDocument();
await userEvent.click(cancelButton);
expect(modalTitel).not.toBeInTheDocument();
await waitFor(() => expect(modalTitle).not.toBeInTheDocument());
});

describe('Single sign on', () => {
Expand Down
10 changes: 4 additions & 6 deletions test/e2e/cypress/e2e/host_details.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,10 @@ context('Host Details', () => {

cy.get('#headlessui-portal-root').as('modal');

cy.get('@modal')
.find('.w-full')
.should(
'contain.text',
`Clean up data discovered by agent on host ${selectedHost.hostName}`
);
cy.get('@modal').should(
'contain.text',
`Clean up data discovered by agent on host ${selectedHost.hostName}`
);
cy.get('@modal').contains('button', 'Clean up').click();

cy.get('@modal').should('not.exist');
Expand Down
10 changes: 4 additions & 6 deletions test/e2e/cypress/e2e/hosts_overview.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,10 @@ context('Hosts Overview', () => {

cy.get('#headlessui-portal-root').as('modal');

cy.get('@modal')
.find('.w-full')
.should(
'contain.text',
`Clean up data discovered by agent on host ${hostToDeregister.name}`
);
cy.get('@modal').should(
'contain.text',
`Clean up data discovered by agent on host ${hostToDeregister.name}`
);

cy.get('@modal').contains('button', 'Clean up').click();

Expand Down
6 changes: 4 additions & 2 deletions test/e2e/cypress/e2e/users.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ describe('Users', () => {

cy.get('button.totp-selection-dropdown').click();

cy.get('li:contains("Disabled")').click();
cy.contains('div', 'Disabled').click();
cy.contains('button', 'Save').click();

cy.get('div').contains('User edited successfully');
Expand All @@ -397,7 +397,9 @@ describe('Users', () => {

cy.get('button.totp-selection-dropdown').click();

cy.get('ul > li:nth-child(1)')
cy.get(
'div[role="listbox"][data-headlessui-state="open"] > div:nth-child(1)'
)
.invoke('attr', 'aria-disabled')
.should('eq', 'true');
});
Expand Down

0 comments on commit ebeec53

Please sign in to comment.