diff --git a/src/web/pages/targets/__tests__/details.jsx b/src/web/pages/targets/__tests__/details.jsx index 821b6295a6..08fd096339 100644 --- a/src/web/pages/targets/__tests__/details.jsx +++ b/src/web/pages/targets/__tests__/details.jsx @@ -136,7 +136,7 @@ describe('Target Details tests', () => { ); const headings = element.querySelectorAll('h2'); - const detailslinks = getAllByTestId('details-link'); + const detailsLinks = getAllByTestId('details-link'); expect(headings[0]).toHaveTextContent('Hosts'); @@ -154,13 +154,13 @@ describe('Target Details tests', () => { expect(element).toHaveTextContent('Alive TestScan Config Default'); expect(element).toHaveTextContent('Port List'); - expect(detailslinks[0]).toHaveAttribute('href', '/portlist/pl_id1'); + expect(detailsLinks[0]).toHaveAttribute('href', '/portlist/pl_id1'); expect(headings[1]).toHaveTextContent('Credentials'); expect(element).toHaveTextContent('SMB'); - expect(detailslinks[1]).toHaveAttribute('href', '/credential/4784'); + expect(detailsLinks[1]).toHaveAttribute('href', '/credential/4784'); }); test('should render full target details with elevate credentials and tasks', () => { @@ -176,7 +176,7 @@ describe('Target Details tests', () => { ); const headings = element.querySelectorAll('h2'); - const detailslinks = getAllByTestId('details-link'); + const detailsLinks = getAllByTestId('details-link'); expect(headings[0]).toHaveTextContent('Hosts'); @@ -194,26 +194,26 @@ describe('Target Details tests', () => { expect(element).toHaveTextContent('Alive TestScan Config Default'); expect(element).toHaveTextContent('Port List'); - expect(detailslinks[0]).toHaveAttribute('href', '/portlist/pl_id1'); - expect(detailslinks[0]).toHaveTextContent('pl1'); + expect(detailsLinks[0]).toHaveAttribute('href', '/portlist/pl_id1'); + expect(detailsLinks[0]).toHaveTextContent('pl1'); expect(headings[1]).toHaveTextContent('Credentials'); expect(element).toHaveTextContent('SSH'); - expect(detailslinks[1]).toHaveAttribute('href', '/credential/1235'); - expect(detailslinks[1]).toHaveTextContent('ssh'); + expect(detailsLinks[1]).toHaveAttribute('href', '/credential/1235'); + expect(detailsLinks[1]).toHaveTextContent('ssh'); expect(element).toHaveTextContent('on Port 22'); expect(element).toHaveTextContent('SSH elevate credential '); - expect(detailslinks[2]).toHaveAttribute('href', '/credential/3456'); - expect(detailslinks[2]).toHaveTextContent('ssh_elevate'); + expect(detailsLinks[2]).toHaveAttribute('href', '/credential/3456'); + expect(detailsLinks[2]).toHaveTextContent('ssh_elevate'); expect(element).toHaveTextContent('SMB'); - expect(detailslinks[3]).toHaveAttribute('href', '/credential/4784'); - expect(detailslinks[3]).toHaveTextContent('smb_credential'); + expect(detailsLinks[3]).toHaveAttribute('href', '/credential/4784'); + expect(detailsLinks[3]).toHaveTextContent('smb_credential'); expect(headings[2]).toHaveTextContent('Tasks using this Target (1)'); - expect(detailslinks[4]).toHaveAttribute('href', '/task/task_id'); - expect(detailslinks[4]).toHaveTextContent('task1'); + expect(detailsLinks[4]).toHaveAttribute('href', '/task/task_id'); + expect(detailsLinks[4]).toHaveTextContent('task1'); }); }); diff --git a/src/web/pages/targets/__tests__/dialog.jsx b/src/web/pages/targets/__tests__/dialog.jsx index f8c4e1122c..ed40096a8c 100644 --- a/src/web/pages/targets/__tests__/dialog.jsx +++ b/src/web/pages/targets/__tests__/dialog.jsx @@ -26,6 +26,16 @@ import Credential, { import {rendererWith, fireEvent, screen} from 'web/utils/testing'; import TargetDialog from 'web/pages/targets/dialog'; +import { + changeInputValue, + getDialogCloseButton, + getDialogSaveButton, + getFileInputs, + getRadioInputs, + getSelectElements, + getSelectItemElementsForSelect, + getTextInputs, +} from 'web/components/testing'; const cred1 = Credential.fromElement({ _id: '5678', @@ -80,118 +90,84 @@ describe('TargetDialog component tests', () => { />, ); - const inputs = baseElement.querySelectorAll('input'); - const formgroups = screen.getAllByTestId('formgroup-title'); - const radioTitles = screen.getAllByTestId('radio-title'); - const radioInputs = screen.getAllByTestId('radio-input'); + const inputs = getTextInputs(); + const fileInputs = getFileInputs(); + const radioInputs = getRadioInputs(); - expect(radioInputs.length).toEqual(10); - - expect(formgroups[0]).toHaveTextContent('Name'); expect(inputs[0]).toHaveAttribute('name', 'name'); - expect(inputs[0]).toHaveAttribute('value', 'Unnamed'); // name field + expect(inputs[0]).toHaveValue('Unnamed'); // name field - expect(formgroups[1]).toHaveTextContent('Comment'); expect(inputs[1]).toHaveAttribute('name', 'comment'); - expect(inputs[1]).toHaveAttribute('value', ''); // comment field + expect(inputs[1]).toHaveValue(''); // comment field - expect(formgroups[2]).toHaveTextContent('Hosts'); - expect(radioTitles[0]).toHaveTextContent('Manual'); expect(radioInputs[0]).toHaveAttribute('name', 'target_source'); expect(radioInputs[0]).toHaveAttribute('value', 'manual'); - expect(radioInputs[0]).toHaveAttribute('checked'); + expect(radioInputs[0]).toBeChecked(); - expect(inputs[3]).toHaveAttribute('name', 'hosts'); - expect(inputs[3]).toHaveAttribute('value', ''); + expect(inputs[2]).toHaveAttribute('name', 'hosts'); + expect(inputs[2]).toHaveValue(''); - expect(radioTitles[1]).toHaveTextContent('From file'); expect(radioInputs[1]).toHaveAttribute('name', 'target_source'); expect(radioInputs[1]).toHaveAttribute('value', 'file'); - expect(radioInputs[1]).not.toHaveAttribute('checked'); + expect(radioInputs[1]).not.toBeChecked(); - expect(inputs[5]).toHaveAttribute('name', 'file'); - expect(inputs[5]).toHaveAttribute('disabled'); + expect(fileInputs[0]).toBeDisabled(); - expect(formgroups[3]).toHaveTextContent('Exclude Hosts'); - expect(radioTitles[2]).toHaveTextContent('Manual'); expect(radioInputs[2]).toHaveAttribute('name', 'target_exclude_source'); expect(radioInputs[2]).toHaveAttribute('value', 'manual'); - expect(radioInputs[2]).toHaveAttribute('checked'); + expect(radioInputs[2]).toBeChecked(); - expect(inputs[7]).toHaveAttribute('name', 'exclude_hosts'); - expect(inputs[7]).toHaveAttribute('value', ''); + expect(inputs[3]).toHaveAttribute('name', 'exclude_hosts'); + expect(inputs[3]).toHaveValue(''); - expect(radioTitles[3]).toHaveTextContent('From file'); expect(radioInputs[3]).toHaveAttribute('name', 'target_exclude_source'); expect(radioInputs[3]).toHaveAttribute('value', 'file'); - expect(radioInputs[3]).not.toHaveAttribute('checked'); + expect(radioInputs[3]).not.toBeChecked(); - expect(inputs[9]).toHaveAttribute('name', 'exclude_file'); - expect(inputs[9]).toHaveAttribute('disabled'); + expect(fileInputs[1]).toBeDisabled(); - expect(formgroups[4]).toHaveTextContent( - 'Allow simultaneous scanning via multiple IPs', - ); - expect(radioTitles[4]).toHaveTextContent('Yes'); expect(radioInputs[4]).toHaveAttribute('value', '1'); - expect(radioInputs[4]).toHaveAttribute('checked'); + expect(radioInputs[4]).toBeChecked(); - expect(radioTitles[5]).toHaveTextContent('No'); expect(radioInputs[5]).toHaveAttribute('name', 'allowSimultaneousIPs'); expect(radioInputs[5]).toHaveAttribute('value', '0'); - expect(radioInputs[5]).not.toHaveAttribute('checked'); + expect(radioInputs[5]).not.toBeChecked(); - const selectedValues = screen.getAllByTestId('select-selected-value'); + const selects = getSelectElements(); expect(baseElement).not.toHaveTextContent('Elevate privileges'); // elevate privileges should not be rendered without valid ssh_credential_id - expect(selectedValues.length).toEqual(6); // Should only have 6 selects - expect(formgroups[5]).toHaveTextContent('Port List'); - expect(selectedValues[0]).toHaveTextContent('OpenVAS Default'); + expect(selects[0]).toHaveValue('OpenVAS Default'); expect( screen.getAllByTitle('Create a new port list')[0], ).toBeInTheDocument(); - expect(formgroups[6]).toHaveTextContent('Alive Test'); - expect(selectedValues[1]).toHaveTextContent('Scan Config Default'); + expect(selects[1]).toHaveValue('Scan Config Default'); const createCredentialIcons = screen.getAllByTitle( 'Create a new credential', ); expect(createCredentialIcons.length).toEqual(8); // Each icon has both a span and an svg icon. There should be 4 total - expect(formgroups[7]).toHaveTextContent('SSH'); - expect(selectedValues[2]).toHaveTextContent('--'); + expect(selects[2]).toHaveValue('--'); expect(baseElement).toHaveTextContent('on port'); + expect(selects[3]).toHaveValue('--'); + expect(selects[4]).toHaveValue('--'); + expect(selects[5]).toHaveValue('--'); - expect(formgroups[8]).toHaveTextContent('SMB'); - expect(selectedValues[3]).toHaveTextContent('--'); - - expect(formgroups[9]).toHaveTextContent('ESXi'); - expect(selectedValues[4]).toHaveTextContent('--'); - - expect(formgroups[10]).toHaveTextContent('SNMP'); - expect(selectedValues[5]).toHaveTextContent('--'); - - expect(formgroups[11]).toHaveTextContent('Reverse Lookup Only'); - expect(radioTitles[6]).toHaveTextContent('Yes'); expect(radioInputs[6]).toHaveAttribute('value', '1'); - expect(radioInputs[6]).not.toHaveAttribute('checked'); + expect(radioInputs[6]).not.toBeChecked(); - expect(radioTitles[7]).toHaveTextContent('No'); expect(radioInputs[7]).toHaveAttribute('name', 'reverse_lookup_only'); expect(radioInputs[7]).toHaveAttribute('value', '0'); - expect(radioInputs[7]).toHaveAttribute('checked'); + expect(radioInputs[7]).toBeChecked(); - expect(formgroups[12]).toHaveTextContent('Reverse Lookup Unify'); - expect(radioTitles[8]).toHaveTextContent('Yes'); expect(radioInputs[8]).toHaveAttribute('value', '1'); - expect(radioInputs[8]).not.toHaveAttribute('checked'); + expect(radioInputs[8]).not.toBeChecked(); - expect(radioTitles[9]).toHaveTextContent('No'); expect(radioInputs[9]).toHaveAttribute('name', 'reverse_lookup_unify'); expect(radioInputs[9]).toHaveAttribute('value', '0'); - expect(radioInputs[9]).toHaveAttribute('checked'); + expect(radioInputs[9]).toBeChecked(); }); test('should display value from props', () => { @@ -230,116 +206,82 @@ describe('TargetDialog component tests', () => { />, ); - const inputs = baseElement.querySelectorAll('input'); - const formgroups = screen.getAllByTestId('formgroup-title'); - const radioTitles = screen.getAllByTestId('radio-title'); - const radioInputs = screen.getAllByTestId('radio-input'); + const inputs = getTextInputs(); + const radioInputs = getRadioInputs(); + const fileInputs = getFileInputs(); + const selects = getSelectElements(); - expect(radioInputs.length).toEqual(10); - - expect(formgroups[0]).toHaveTextContent('Name'); expect(inputs[0]).toHaveAttribute('name', 'name'); - expect(inputs[0]).toHaveAttribute('value', 'target'); // name field + expect(inputs[0]).toHaveValue('target'); // name field - expect(formgroups[1]).toHaveTextContent('Comment'); expect(inputs[1]).toHaveAttribute('name', 'comment'); - expect(inputs[1]).toHaveAttribute('value', 'hello world'); // comment field + expect(inputs[1]).toHaveValue('hello world'); // comment field - expect(formgroups[2]).toHaveTextContent('Hosts'); - expect(radioTitles[0]).toHaveTextContent('Manual'); expect(radioInputs[0]).toHaveAttribute('name', 'target_source'); expect(radioInputs[0]).toHaveAttribute('value', 'manual'); - expect(radioInputs[0]).toHaveAttribute('checked'); + expect(radioInputs[0]).toBeChecked(); - expect(inputs[3]).toHaveAttribute('name', 'hosts'); - expect(inputs[3]).toHaveAttribute('value', '123.455.67.434'); + expect(inputs[2]).toHaveAttribute('name', 'hosts'); + expect(inputs[2]).toHaveAttribute('value', '123.455.67.434'); - expect(radioTitles[1]).toHaveTextContent('From file'); expect(radioInputs[1]).toHaveAttribute('name', 'target_source'); expect(radioInputs[1]).toHaveAttribute('value', 'file'); - expect(radioInputs[1]).not.toHaveAttribute('checked'); + expect(radioInputs[1]).not.toBeChecked(); - expect(inputs[5]).toHaveAttribute('name', 'file'); - expect(inputs[5]).toHaveAttribute('disabled'); + expect(fileInputs[0]).toHaveAttribute('disabled'); - expect(formgroups[3]).toHaveTextContent('Exclude Hosts'); - expect(radioTitles[2]).toHaveTextContent('Manual'); expect(radioInputs[2]).toHaveAttribute('name', 'target_exclude_source'); expect(radioInputs[2]).toHaveAttribute('value', 'manual'); - expect(radioInputs[2]).toHaveAttribute('checked'); + expect(radioInputs[2]).toBeChecked(); - expect(inputs[7]).toHaveAttribute('name', 'exclude_hosts'); - expect(inputs[7]).toHaveAttribute('value', ''); + expect(inputs[3]).toHaveAttribute('name', 'exclude_hosts'); + expect(inputs[3]).toHaveValue(''); - expect(radioTitles[3]).toHaveTextContent('From file'); expect(radioInputs[3]).toHaveAttribute('name', 'target_exclude_source'); expect(radioInputs[3]).toHaveAttribute('value', 'file'); - expect(radioInputs[3]).not.toHaveAttribute('checked'); + expect(radioInputs[3]).not.toBeChecked(); - expect(inputs[9]).toHaveAttribute('name', 'exclude_file'); - expect(inputs[9]).toHaveAttribute('disabled'); + expect(fileInputs[1]).toBeDisabled(); - expect(formgroups[4]).toHaveTextContent( - 'Allow simultaneous scanning via multiple IPs', - ); - expect(radioTitles[4]).toHaveTextContent('Yes'); expect(radioInputs[4]).toHaveAttribute('value', '1'); - expect(radioInputs[4]).not.toHaveAttribute('checked'); + expect(radioInputs[4]).not.toBeChecked(); - expect(radioTitles[5]).toHaveTextContent('No'); expect(radioInputs[5]).toHaveAttribute('name', 'allowSimultaneousIPs'); expect(radioInputs[5]).toHaveAttribute('value', '0'); - expect(radioInputs[5]).toHaveAttribute('checked'); - - const selectedValues = screen.getAllByTestId('select-selected-value'); - expect(selectedValues.length).toEqual(6); + expect(radioInputs[5]).toBeChecked(); - expect(formgroups[5]).toHaveTextContent('Port List'); - expect(selectedValues[0]).toHaveTextContent('OpenVAS Default'); + expect(selects[0]).toHaveValue('OpenVAS Default'); expect( screen.getAllByTitle('Create a new port list')[0], ).toBeInTheDocument(); - expect(formgroups[6]).toHaveTextContent('Alive Test'); - expect(selectedValues[1]).toHaveTextContent('Scan Config Default'); + expect(selects[1]).toHaveValue('Scan Config Default'); const createCredentialIcons = screen.getAllByTitle( 'Create a new credential', ); expect(createCredentialIcons.length).toEqual(8); // Each icon has both a span and an svg icon. There should be 4 total - expect(formgroups[7]).toHaveTextContent('SSH'); - expect(selectedValues[2]).toHaveTextContent('--'); expect(baseElement).toHaveTextContent('on port'); - expect(formgroups[8]).toHaveTextContent('SMB'); - expect(selectedValues[3]).toHaveTextContent('username+password'); - - expect(formgroups[9]).toHaveTextContent('ESXi'); - expect(selectedValues[4]).toHaveTextContent('--'); - - expect(formgroups[10]).toHaveTextContent('SNMP'); - expect(selectedValues[5]).toHaveTextContent('--'); + expect(selects[2]).toHaveValue('--'); + expect(selects[3]).toHaveValue('username+password'); + expect(selects[4]).toHaveValue('--'); + expect(selects[5]).toHaveValue('--'); - expect(formgroups[11]).toHaveTextContent('Reverse Lookup Only'); - expect(radioTitles[6]).toHaveTextContent('Yes'); expect(radioInputs[6]).toHaveAttribute('value', '1'); - expect(radioInputs[6]).not.toHaveAttribute('checked'); + expect(radioInputs[6]).not.toBeChecked(); - expect(radioTitles[7]).toHaveTextContent('No'); expect(radioInputs[7]).toHaveAttribute('name', 'reverse_lookup_only'); expect(radioInputs[7]).toHaveAttribute('value', '0'); - expect(radioInputs[7]).toHaveAttribute('checked'); + expect(radioInputs[7]).toBeChecked(); - expect(formgroups[12]).toHaveTextContent('Reverse Lookup Unify'); - expect(radioTitles[8]).toHaveTextContent('Yes'); expect(radioInputs[8]).toHaveAttribute('value', '1'); - expect(radioInputs[8]).not.toHaveAttribute('checked'); + expect(radioInputs[8]).not.toBeChecked(); - expect(radioTitles[9]).toHaveTextContent('No'); expect(radioInputs[9]).toHaveAttribute('name', 'reverse_lookup_unify'); expect(radioInputs[9]).toHaveAttribute('value', '0'); - expect(radioInputs[9]).toHaveAttribute('checked'); + expect(radioInputs[9]).toBeChecked(); }); test('should allow to change values and save the dialog', () => { @@ -350,7 +292,7 @@ describe('TargetDialog component tests', () => { const {render} = rendererWith({gmp, capabilities: true}); - const {getByTestId, getByName, getAllByName} = render( + const {getByName, getAllByName} = render( { // text input const nameInput = getByName('name'); - fireEvent.change(nameInput, {target: {value: 'ross'}}); + changeInputValue(nameInput, 'ross'); // radio input const simultaneousIPInput = getAllByName('allowSimultaneousIPs'); expect(simultaneousIPInput.length).toBe(2); - expect(simultaneousIPInput[0]).not.toHaveAttribute('checked'); + expect(simultaneousIPInput[0]).not.toBeChecked(); expect(simultaneousIPInput[0]).toHaveAttribute('value', '1'); - expect(simultaneousIPInput[1]).toHaveAttribute('checked'); + expect(simultaneousIPInput[1]).toBeChecked(); expect(simultaneousIPInput[1]).toHaveAttribute('value', '0'); fireEvent.click(simultaneousIPInput[0]); // radio button check yes - const saveButton = getByTestId('dialog-save-button'); - + const saveButton = getDialogSaveButton(); fireEvent.click(saveButton); expect(handleSave).toHaveBeenCalledWith({ @@ -462,8 +403,8 @@ describe('TargetDialog component tests', () => { expect(baseElement).toHaveTextContent('Elevate privileges'); - const selectedValues = screen.getAllByTestId('select-selected-value'); - expect(selectedValues.length).toEqual(7); // Should have 7 selects + const selects = getSelectElements(); + expect(selects.length).toEqual(7); // Should have 7 selects const createCredentialIcons = screen.getAllByTitle( 'Create a new credential', @@ -471,7 +412,7 @@ describe('TargetDialog component tests', () => { expect(createCredentialIcons.length).toEqual(10); // Each icon has both a span and an svg icon. There should be 5 total, including elevate privileges }); - test('ssh elevate credential dropdown should only allow username + password options and remove ssh credential from list', () => { + test('ssh elevate credential dropdown should only allow username + password options and remove ssh credential from list', async () => { const handleClose = testing.fn(); const handleChange = testing.fn(); const handleSave = testing.fn(); @@ -479,7 +420,7 @@ describe('TargetDialog component tests', () => { const {render} = rendererWith({gmp, capabilities: true}); - const {baseElement, queryAllByTestId} = render( + const {baseElement} = render( { expect(baseElement).toHaveTextContent('Elevate privileges'); - const selectedValues = screen.getAllByTestId('select-selected-value'); - expect(selectedValues.length).toEqual(7); // Should have 7 selects - - const selectOpenButton = screen.getAllByTestId('select-open-button'); - let selectItems = queryAllByTestId('select-item'); - - expect(selectItems.length).toBe(0); - - fireEvent.click(selectOpenButton[3]); + const selects = getSelectElements(); + expect(selects.length).toEqual(7); // Should have 7 selects - selectItems = queryAllByTestId('select-item'); + const selectItems = await getSelectItemElementsForSelect(selects[3]); expect(selectItems.length).toBe(2); // "original" ssh option removed expect(selectItems[0]).toHaveTextContent('--'); // null option expect(selectItems[1]).toHaveTextContent('up2'); }); - test('ssh credential dropdown should remove ssh elevate credential from list', () => { + test('ssh credential dropdown should remove ssh elevate credential from list', async () => { const handleClose = testing.fn(); const handleChange = testing.fn(); const handleSave = testing.fn(); @@ -535,7 +469,7 @@ describe('TargetDialog component tests', () => { const {render} = rendererWith({gmp, capabilities: true}); - const {baseElement, queryAllByTestId} = render( + const {baseElement} = render( { expect(baseElement).toHaveTextContent('Elevate privileges'); - const selectedValues = screen.getAllByTestId('select-selected-value'); - expect(selectedValues.length).toEqual(7); // Should have 7 selects - - const selectOpenButton = screen.getAllByTestId('select-open-button'); - let selectItems = queryAllByTestId('select-item'); - - expect(selectItems.length).toBe(0); + const selects = getSelectElements(); + expect(selects.length).toEqual(7); // Should have 7 selects - fireEvent.click(selectOpenButton[2]); - - selectItems = queryAllByTestId('select-item'); + const selectItems = await getSelectItemElementsForSelect(selects[2]); expect(selectItems.length).toBe(3); // ssh elevate option removed expect(selectItems[0]).toHaveTextContent('--'); // null option @@ -626,26 +553,26 @@ describe('TargetDialog component tests', () => { const newIcons = queryAllByTitle('Create a new credential'); expect(newIcons.length).toBe(0); // no new credential can be created - const selectedValues = screen.getAllByTestId('select-selected-value'); - expect(selectedValues.length).toEqual(7); // Should have 7 selects + const selects = getSelectElements(); + expect(selects.length).toEqual(7); // Should have 7 selects - expect(selectedValues[0]).toHaveTextContent('OpenVAS Default'); - expect(selectedValues[0]).toHaveAttribute('disabled'); + expect(selects[0]).toHaveValue('OpenVAS Default'); + expect(selects[0]).toBeDisabled(); - expect(selectedValues[1]).toHaveTextContent('Scan Config Default'); - expect(selectedValues[2]).toHaveTextContent('username+password'); - expect(selectedValues[2]).toHaveAttribute('disabled'); + expect(selects[1]).toHaveValue('Scan Config Default'); + expect(selects[2]).toHaveValue('username+password'); + expect(selects[2]).toBeDisabled(); - expect(selectedValues[3]).toHaveTextContent('up2'); - expect(selectedValues[3]).toHaveAttribute('disabled'); - expect(selectedValues[4]).toHaveTextContent('--'); - expect(selectedValues[4]).toHaveAttribute('disabled'); + expect(selects[3]).toHaveValue('up2'); + expect(selects[3]).toBeDisabled(); + expect(selects[4]).toHaveValue('--'); + expect(selects[4]).toBeDisabled(); - expect(selectedValues[5]).toHaveTextContent('--'); - expect(selectedValues[5]).toHaveAttribute('disabled'); + expect(selects[5]).toHaveValue('--'); + expect(selects[5]).toBeDisabled(); - expect(selectedValues[6]).toHaveTextContent('--'); - expect(selectedValues[6]).toHaveAttribute('disabled'); + expect(selects[6]).toHaveValue('--'); + expect(selects[6]).toBeDisabled(); }); test('should allow to close the dialog', () => { @@ -656,7 +583,7 @@ describe('TargetDialog component tests', () => { const {render} = rendererWith({gmp, capabilities: true}); - const {getByTestId} = render( + render( { />, ); - const closeButton = getByTestId('dialog-close-button'); - + const closeButton = getDialogCloseButton(); fireEvent.click(closeButton); expect(handleClose).toHaveBeenCalled(); diff --git a/src/web/pages/targets/__tests__/listpage.jsx b/src/web/pages/targets/__tests__/listpage.jsx index 22c438234b..500bd8f81b 100644 --- a/src/web/pages/targets/__tests__/listpage.jsx +++ b/src/web/pages/targets/__tests__/listpage.jsx @@ -31,6 +31,17 @@ import {loadingActions} from 'web/store/usersettings/defaults/actions'; import {rendererWith, fireEvent, screen, wait} from 'web/utils/testing'; +import { + clickElement, + getCheckBoxes, + getPowerFilter, + getSelectElement, + getSelectItemElementsForSelect, + getTableBody, + getTableFooter, + getTextInputs, +} from 'web/components/testing'; + import TargetPage, {ToolBarIcons} from '../listpage'; let currentSettings; @@ -137,10 +148,10 @@ describe('TargetPage tests', () => { store.dispatch(setTimezone('CET')); store.dispatch(setUsername('admin')); - const defaultSettingfilter = Filter.fromString('foo=bar'); + const defaultSettingFilter = Filter.fromString('foo=bar'); store.dispatch(loadingActions.success({rowsperpage: {value: '2'}})); store.dispatch( - defaultFilterLoadingActions.success('target', defaultSettingfilter), + defaultFilterLoadingActions.success('target', defaultSettingFilter), ); const counts = new CollectionCounts({ @@ -160,8 +171,9 @@ describe('TargetPage tests', () => { await wait(); - const inputs = baseElement.querySelectorAll('input'); - const selects = screen.getAllByTestId('select-selected-value'); + const powerFilter = getPowerFilter(); + const select = getSelectElement(powerFilter); + const inputs = getTextInputs(powerFilter); // Toolbar Icons expect(screen.getAllByTitle('Help: Targets')[0]).toBeInTheDocument(); @@ -176,8 +188,8 @@ describe('TargetPage tests', () => { ).toBeInTheDocument(); expect(screen.getAllByTitle('Help: Powerfilter')[0]).toBeInTheDocument(); expect(screen.getAllByTitle('Edit Filter')[0]).toBeInTheDocument(); - expect(selects[0]).toHaveAttribute('title', 'Loaded filter'); - expect(selects[0]).toHaveTextContent('--'); + expect(select).toHaveAttribute('title', 'Loaded filter'); + expect(select).toHaveValue('--'); // Table const header = baseElement.querySelectorAll('th'); @@ -205,6 +217,7 @@ describe('TargetPage tests', () => { expect(screen.getAllByTitle('Clone Target')[0]).toBeInTheDocument(); expect(screen.getAllByTitle('Export Target')[0]).toBeInTheDocument(); }); + test('should allow to bulk action on page contents', async () => { const deleteByFilter = testing.fn().mockResolvedValue({ foo: 'bar', @@ -237,10 +250,10 @@ describe('TargetPage tests', () => { store.dispatch(setTimezone('CET')); store.dispatch(setUsername('admin')); - const defaultSettingfilter = Filter.fromString('foo=bar'); + const defaultSettingFilter = Filter.fromString('foo=bar'); store.dispatch(loadingActions.success({rowsperpage: {value: '2'}})); store.dispatch( - defaultFilterLoadingActions.success('target', defaultSettingfilter), + defaultFilterLoadingActions.success('target', defaultSettingFilter), ); const counts = new CollectionCounts({ @@ -261,23 +274,15 @@ describe('TargetPage tests', () => { await wait(); // export page contents - const exportIcon = screen.getAllByTitle('Export page contents'); - - expect(exportIcon[0]).toBeInTheDocument(); - fireEvent.click(exportIcon[0]); - - await wait(); - + const exportIcon = screen.getAllByTitle('Export page contents')[0]; + await clickElement(exportIcon); expect(exportByFilter).toHaveBeenCalled(); // move page contents to trashcan - const deleteIcon = screen.getAllByTitle('Move page contents to trashcan'); - - expect(deleteIcon[0]).toBeInTheDocument(); - fireEvent.click(deleteIcon[0]); - - await wait(); - + const deleteIcon = screen.getAllByTitle( + 'Move page contents to trashcan', + )[0]; + await clickElement(deleteIcon); expect(deleteByFilter).toHaveBeenCalled(); }); @@ -314,10 +319,10 @@ describe('TargetPage tests', () => { store.dispatch(setTimezone('CET')); store.dispatch(setUsername('admin')); - const defaultSettingfilter = Filter.fromString('foo=bar'); + const defaultSettingFilter = Filter.fromString('foo=bar'); store.dispatch(loadingActions.success({rowsperpage: {value: '2'}})); store.dispatch( - defaultFilterLoadingActions.success('target', defaultSettingfilter), + defaultFilterLoadingActions.success('target', defaultSettingFilter), ); const counts = new CollectionCounts({ @@ -333,43 +338,30 @@ describe('TargetPage tests', () => { entitiesLoadingActions.success([target], filter, loadedFilter, counts), ); - const {element} = render(); + render(); await wait(); - const selectFields = screen.getAllByTestId('select-open-button'); - fireEvent.click(selectFields[1]); - - const selectItems = screen.getAllByTestId('select-item'); - fireEvent.click(selectItems[1]); - - const selected = screen.getAllByTestId('select-selected-value'); - expect(selected[1]).toHaveTextContent('Apply to selection'); - - const inputs = element.querySelectorAll('input'); + // change to apply to selection + const tableFooter = getTableFooter(); + const select = getSelectElement(tableFooter); + const selectItems = await getSelectItemElementsForSelect(select); + await clickElement(selectItems[1]); + expect(select).toHaveValue('Apply to selection'); // select an target - fireEvent.click(inputs[1]); - await wait(); + const tableBody = getTableBody(); + const inputs = getCheckBoxes(tableBody); + await clickElement(inputs[1]); // export selected target - const exportIcon = screen.getAllByTitle('Export selection'); - - expect(exportIcon[0]).toBeInTheDocument(); - fireEvent.click(exportIcon[0]); - - await wait(); - + const exportIcon = screen.getAllByTitle('Export selection')[0]; + await clickElement(exportIcon); expect(exportByIds).toHaveBeenCalled(); // move selected target to trashcan - const deleteIcon = screen.getAllByTitle('Move selection to trashcan'); - - expect(deleteIcon[0]).toBeInTheDocument(); - fireEvent.click(deleteIcon[0]); - - await wait(); - + const deleteIcon = screen.getAllByTitle('Move selection to trashcan')[0]; + await clickElement(deleteIcon); expect(deleteByIds).toHaveBeenCalled(); }); @@ -429,35 +421,21 @@ describe('TargetPage tests', () => { await wait(); - const selectFields = screen.getAllByTestId('select-open-button'); - fireEvent.click(selectFields[1]); - - const selectItems = screen.getAllByTestId('select-item'); - fireEvent.click(selectItems[2]); - - await wait(); - - const selected = screen.getAllByTestId('select-selected-value'); - expect(selected[1]).toHaveTextContent('Apply to all filtered'); + // change to all filtered + const tableFooter = getTableFooter(); + const select = getSelectElement(tableFooter); + const selectItems = await getSelectItemElementsForSelect(select); + await clickElement(selectItems[2]); + expect(select).toHaveValue('Apply to all filtered'); // export all filtered targets - const exportIcon = screen.getAllByTitle('Export all filtered'); - - expect(exportIcon[0]).toBeInTheDocument(); - fireEvent.click(exportIcon[0]); - - await wait(); - + const exportIcon = screen.getAllByTitle('Export all filtered')[0]; + await clickElement(exportIcon); expect(exportByFilter).toHaveBeenCalled(); // move all filtered targets to trashcan - const deleteIcon = screen.getAllByTitle('Move all filtered to trashcan'); - - expect(deleteIcon[0]).toBeInTheDocument(); - fireEvent.click(deleteIcon[0]); - - await wait(); - + const deleteIcon = screen.getAllByTitle('Move all filtered to trashcan')[0]; + await clickElement(deleteIcon); expect(deleteByFilter).toHaveBeenCalled(); }); });