diff --git a/.jest-setup-after-env.js b/.jest-setup-after-env.js index 4c5b38985..852ca5bc8 100644 --- a/.jest-setup-after-env.js +++ b/.jest-setup-after-env.js @@ -1,5 +1,10 @@ import '@testing-library/jest-dom'; import fetch from 'node-fetch'; +import { history } from './src/react/utils/test.tsx'; window.fetch = (url, ...rest) => fetch(/^https?:/.test(url) ? url : new URL(url, 'http://localhost'), ...rest); + +afterEach(() => { + history.push('/'); +}); diff --git a/src/react/account/iamAttachment/__tests__/Attachments.test.tsx b/src/react/account/iamAttachment/__tests__/Attachments.test.tsx index 9558eb7b2..04d23d280 100644 --- a/src/react/account/iamAttachment/__tests__/Attachments.test.tsx +++ b/src/react/account/iamAttachment/__tests__/Attachments.test.tsx @@ -26,123 +26,121 @@ const tobeAttachedUserArn = 'arn:aws:iam::718643629313:user/dev1'; const groupName = 'devs'; const groupArn = 'arn:aws:iam::718643629313:group/devs'; -const server = setupServer( - rest.post(`${TEST_API_BASE_URL}/`, (req, res, ctx) => { - const params = new URLSearchParams(req.body); - if (params.get('Action') === 'ListUsers') { - return res( - ctx.xml(` - - - - - ANAOQ65XCATLB2KMVXI508ZFXTOK12W5 - / - ${tobeAttachedUserName} - ${tobeAttachedUserArn} - 2022-07-04T13:34:25Z - - - false - - - e43fc97c3c4f9895c92e - - `), - ); - } - if (params.get('Action') === 'ListGroups') { - return res( - ctx.xml(` - - - - - / - ${groupName} - N0L8CM7DIHD6YWN8V8796U3PTM8EEB01 - ${groupArn} - 2022-06-23T12:29:50Z - - - false - - - 99ed69425849972f4e3d - +const initialMock = (req, res, ctx) => { + const params = new URLSearchParams(req.body); + if (params.get('Action') === 'ListUsers') { + return res( + ctx.xml(` + + + + + ANAOQ65XCATLB2KMVXI508ZFXTOK12W5 + / + ${tobeAttachedUserName} + ${tobeAttachedUserArn} + 2022-07-04T13:34:25Z + + + false + + + e43fc97c3c4f9895c92e + + `), + ); + } + if (params.get('Action') === 'ListGroups') { + return res( + ctx.xml(` + + + + + / + ${groupName} + N0L8CM7DIHD6YWN8V8796U3PTM8EEB01 + ${groupArn} + 2022-06-23T12:29:50Z + + + false + + + 99ed69425849972f4e3d + ; - `), - ); - } - if (params.get('Action') === 'AttachUserPolicy') { - return res( - ctx.xml(` - - - 2e30c3c68e45ad7122f7 - - ; - `), - ); - } - if (params.get('Action') === 'AttachGroupPolicy') { - return res(ctx.status(500)); - } - if (params.get('Action') === 'ListGroupsForUser') { - return res( - ctx.xml(` - - - - - / - ${groupName} - N0L8CM7DIHD6YWN8V8796U3PTM8EEB01 - ${groupArn} - 2022-06-23T12:29:50Z - - - false - - - e23e78bd624aba46bb85 - + `), + ); + } + if (params.get('Action') === 'AttachUserPolicy') { + return res( + ctx.xml(` + + + 2e30c3c68e45ad7122f7 + + ; + `), + ); + } + if (params.get('Action') === 'ListGroupsForUser') { + return res( + ctx.xml(` + + + + + / + ${groupName} + N0L8CM7DIHD6YWN8V8796U3PTM8EEB01 + ${groupArn} + 2022-06-23T12:29:50Z + + + false + + + e23e78bd624aba46bb85 + ; - `), - ); - } - - if (params.get('Action') === 'RemoveUserFromGroup') { - return res( - ctx.xml(` - - - b6c00e6cc143a8a66b8e - -; - `), - ); - } + `), + ); + } + + if (params.get('Action') === 'RemoveUserFromGroup') { return res( ctx.xml(` - - - - ${attachedRoleName} - - - ${attachedGroupName} - - - ${attachedUserName} - - false - - d5199fe5464489b0f507 - + + + b6c00e6cc143a8a66b8e + +; `), ); - }), + } + return res( + ctx.xml(` + + + + ${attachedRoleName} + + + ${attachedGroupName} + + + ${attachedUserName} + + false + + d5199fe5464489b0f507 + + `), + ); +}; +const server = setupServer( + rest.post(`${TEST_API_BASE_URL}/`, initialMock), rest.get('http://localhost/account-seeds.json', (req, res, ctx) => { return res(ctx.json(accountSeeds)); @@ -319,13 +317,24 @@ describe('Policy Attachments', () => { }); it('should render Retry button if attachment failed', async () => { + //S + server.use( + rest.post(`${TEST_API_BASE_URL}/`, (req, res, ctx) => { + const params = new URLSearchParams(req.body); + if (params.get('Action') === 'AttachGroupPolicy') { + return res(ctx.status(500)); + } + return initialMock(req, res, ctx); + }), + ); + //E userEvent.click(screen.getByRole('tab', { name: /groups/i })); await waitFor(() => expect( screen.getByPlaceholderText('Search by entity name'), ).not.toBeDisabled(), ); - //E + userEvent.click(screen.getByPlaceholderText('Search by entity name')); await waitFor(() => screen.getByRole('option', { name: new RegExp(groupName, 'i') }), @@ -403,6 +412,7 @@ describe('User Attachments', () => { }); it('should remove the user from group after confirmation', async () => { + await waitFor(() => screen.getByRole('tab', { name: /groups/i })); await waitFor(() => screen.getByText('Attachment status')); //V expect( diff --git a/src/react/utils/test.tsx b/src/react/utils/test.tsx index 13dbde97a..e8071e7ea 100644 --- a/src/react/utils/test.tsx +++ b/src/react/utils/test.tsx @@ -71,7 +71,7 @@ const theme = { info: '#434343', }, }; -const history = createMemoryHistory(); +export const history = createMemoryHistory(); export const configuration = { latest: { version: 1,