Skip to content

Commit

Permalink
Improve testing for react 18
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWatenbergScality committed Feb 20, 2025
1 parent 66a06dc commit 57a84cd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('Policy Attachments', () => {
const firstRow = screen.getAllByRole('row')[1];
expect(
getByRole(firstRow, 'gridcell', {
name: /attached-user/i,
name: /attached-group/i,
}),
).toBeInTheDocument();
expect(
Expand Down
9 changes: 3 additions & 6 deletions src/react/databrowser/objects/ObjectLockSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,9 @@ export default function ObjectLockSetting() {
.putObjectRetention(params)
.promise()
.then(() => {
const event = new CustomEvent('HistoryPushEvent', {
detail: {
path: `/accounts/${account.Name}/buckets/${bucketNameParam}/objects?prefix=${objectKey}`,
},
});
window.dispatchEvent(event);
navigate(
`/accounts/${account.Name}/buckets/${bucketNameParam}/objects?prefix=${objectKey}`,
);
});
},
});
Expand Down
121 changes: 99 additions & 22 deletions src/react/databrowser/objects/__tests__/ObjectLockSetting.test.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,111 @@
import { screen } from '@testing-library/react';
import { XDM_FEATURE } from '../../../../js/config';
import { AppConfig } from '../../../../types/entities';
import { screen, waitFor } from '@testing-library/react';
import { renderWithRouterMatch } from '../../../utils/testUtil';
import ObjectLockSetting from '../ObjectLockSetting';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import userEvent from '@testing-library/user-event';

const config: AppConfig = {
zenkoEndpoint: 'http://localhost:8000',
stsEndpoint: 'http://localhost:9000',
iamEndpoint: 'http://localhost:10000',
managementEndpoint: 'http://localhost:11000',
//@ts-expect-error fix this when you are working on it
navbarEndpoint: 'http://localhost:12000',
navbarConfigUrl: 'http://localhost:13000',
features: [XDM_FEATURE],
};

const server = setupServer();
describe('ObjectLockSetting', () => {
const errorMessage = 'This is an error test message';
beforeEach(() => {
server.listen({ onUnhandledRequest: 'error' });
});

it('should submit the form accordingly', async () => {
//S
const putInterceptor = jest.fn();
server.use(
rest.put('http://testendpoint/test-bucket/object', (req, res, ctx) => {
if (req.url.searchParams.has('retention')) {
putInterceptor(req.body);
return res(ctx.status(200), ctx.json({}));
} else {
return res(ctx.status(500));
}
}),
);

it('should render ObjectLockSetting component with an error banner', async () => {
renderWithRouterMatch(<ObjectLockSetting />, undefined, {
uiErrors: {
errorMsg: errorMessage,
errorType: 'byComponent',
//E
renderWithRouterMatch(
<ObjectLockSetting />,
{
route: '/test-bucket/objects?prefix=object&versionId=1',
path: '/:bucketName/objects',
},
});
{
s3: {
objectMetadata: {
bucketName: 'test-bucket',
objectRetention: {
mode: 'GOVERNANCE',
retainUntilDate: new Date('2022-01-31 00:00:00'),
},
},
},
},
);

expect(screen.getByText(errorMessage)).toBeInTheDocument();
await userEvent.click(screen.getByRole('button', { name: /Save/i }));

//V
await waitFor(() => {
expect(putInterceptor).toHaveBeenCalledWith(
'<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>GOVERNANCE</Mode><RetainUntilDate>2022-01-31T00:00:00Z</RetainUntilDate></Retention>',
);
});
});

it('should display and error when the submission failed', async () => {
//S
const putInterceptor = jest.fn();
server.use(
rest.put('http://testendpoint/test-bucket/object', (req, res, ctx) => {
if (req.url.searchParams.has('retention')) {
putInterceptor(req.body);
return res(ctx.status(500));
} else {
return res(ctx.status(500));
}
}),
);

//E
renderWithRouterMatch(
<ObjectLockSetting />,
{
route: '/test-bucket/objects?prefix=object&versionId=1',
path: '/:bucketName/objects',
},
{
s3: {
objectMetadata: {
bucketName: 'test-bucket',
objectRetention: {
mode: 'GOVERNANCE',
retainUntilDate: new Date('2022-01-31 00:00:00'),
},
},
},
},
);

await userEvent.click(screen.getByRole('button', { name: /Save/i }));

//V
await waitFor(() => {
expect(putInterceptor).toHaveBeenCalledWith(
'<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>GOVERNANCE</Mode><RetainUntilDate>2022-01-31T00:00:00Z</RetainUntilDate></Retention>',
);
});

await waitFor(() => {
expect(
screen.getByText(
'An error occurred while saving the retention settings.',
),
).toBeInTheDocument();
});
});
it('should render ObjectLockSetting component with current data filled', async () => {
renderWithRouterMatch(
<ObjectLockSetting />,
Expand Down

0 comments on commit 57a84cd

Please sign in to comment.