Skip to content

Commit

Permalink
ZKUI-374 // the list versions toggle inactive without page refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
hervedombya committed Sep 5, 2023
1 parent 8087801 commit cf48fb3
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 8 deletions.
23 changes: 19 additions & 4 deletions src/react/databrowser/buckets/details/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import { useWorkflows } from '../../../workflow/Workflows';
import { useCurrentAccount } from '../../../DataServiceRoleProvider';
import { DumbErrorModal } from '../../../ui-elements/ErrorHandlerModal';
import { Bucket } from '../../../next-architecture/domain/entities/bucket';
import { useDeleteBucket } from '../../../next-architecture/domain/business/buckets';
import {
useChangeBucketVersionning,
useDeleteBucket,
} from '../../../next-architecture/domain/business/buckets';

function capitalize(string: string) {
return string.toLowerCase().replace(/^\w/, (c) => {
Expand Down Expand Up @@ -108,6 +111,7 @@ function Overview({ bucket, ingestionStates }: Props) {
}, [dispatch, bucket.name]);

const { mutate: deleteBucket } = useDeleteBucket();
const { mutate: changeBucketVersionning } = useChangeBucketVersionning();

const workflows = workflowsQuery.data;
const attachedWorkflowsCount =
Expand Down Expand Up @@ -153,6 +157,16 @@ function Overview({ bucket, ingestionStates }: Props) {
locations && locations[bucketInfo.locationConstraint]?.locationType;
const isBucketHostedOnAzureOrGCP =
locationType === 'location-azure-v1' || locationType === 'location-gcp-v1';

const updateBucketVersioning = (isVersioning: boolean) => {
changeBucketVersionning({
Bucket: bucketInfo.name,
VersioningConfiguration: {
Status: isVersioning ? 'Enabled' : 'Disabled',
},
});
};

return (
<TableContainer>
<DeleteConfirmation
Expand Down Expand Up @@ -222,14 +236,15 @@ function Overview({ bucket, ingestionStates }: Props) {
? 'Inactive'
: bucketInfo.versioning
}
onChange={() =>
onChange={() => {
dispatch(
toggleBucketVersioning(
bucket.name,
!bucketInfo.isVersioning,
),
)
}
);
updateBucketVersioning(!bucketInfo.isVersioning);
}}
/>
</Tooltip>
)}
Expand Down
104 changes: 102 additions & 2 deletions src/react/databrowser/buckets/details/__tests__/Overview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import {
} from '../../../../../js/mock/S3Client';
import Overview from '../Overview';
import { Toggle } from '@scality/core-ui';
import { reduxMount, reduxRender } from '../../../../utils/testUtil';
import { screen, waitFor, within } from '@testing-library/react';
import {
reduxMount,
reduxRender,
zenkoUITestConfig,
} from '../../../../utils/testUtil';
import { fireEvent, screen, waitFor, within } from '@testing-library/react';
import Immutable from 'immutable';
import userEvent from '@testing-library/user-event';
const BUCKET = {
Expand Down Expand Up @@ -212,3 +216,99 @@ describe('Overview', () => {
expect(versioningToggleItem).toHaveAttribute('disabled');
});
});

//
//
//
//
//

import { rest } from 'msw';
import { setupServer } from 'msw/node';
import {
BUCKET_NAME,
INSTANCE_ID,
} from '../../../../actions/__tests__/utils/testUtil';
import { TEST_API_BASE_URL } from '../../../../utils/testUtil';
import {
ACCOUNT_ID,
USERS,
getConfigOverlay,
getStorageConsumptionMetricsHandlers,
} from '../../../../../js/mock/managementClientMSWHandlers';
const mockResponse =
'<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>Enabled</Status></VersioningConfiguration>';
const TEST_ACCOUNT =
USERS.find((user) => user.id === '064609833007')?.userName ?? '';
const TEST_ACCOUNT_CREATION_DATE =
USERS.find((user) => user.id === '064609833007')?.createDate ?? '';
const server = setupServer(
rest.post(`${TEST_API_BASE_URL}/`, (req, res, ctx) => {
return res(
ctx.json({
IsTruncated: false,
Accounts: [
{
Name: TEST_ACCOUNT,
CreationDate: TEST_ACCOUNT_CREATION_DATE,
Roles: [
{
Name: 'storage-manager-role',
Arn: 'arn:aws:iam::064609833007:role/scality-internal/storage-manager-role',
},
],
},
],
}),
);
}),
rest.post(
`${TEST_API_BASE_URL}/api/v1/instance/${INSTANCE_ID}/account/${ACCOUNT_ID}/bucket/bucket/workflow/replication`,
(req, res, ctx) => {
return res(ctx.json([]));
},
),
getConfigOverlay(zenkoUITestConfig.managementEndpoint, INSTANCE_ID),
...getStorageConsumptionMetricsHandlers(
zenkoUITestConfig.managementEndpoint,
INSTANCE_ID,
),
);
beforeAll(() => {
server.listen({ onUnhandledRequest: 'error' });
});
afterAll(() => server.close());
afterEach(() => server.resetHandlers());

describe('Overview', () => {
it('should call the updateBucketVersioning function when clicking on the toggle versioning button', async () => {
const useUpdateBucketVersioningMock = jest.fn();
server.use(
rest.put(`${TEST_API_BASE_URL}/${BUCKET_NAME}`, (req, res, ctx) => {
useUpdateBucketVersioningMock(req.body);
return res(ctx.status(200));
}),
);

reduxRender(<Overview bucket={BUCKET} />, {
...TEST_STATE,
...{ s3: { bucketInfo: bucketInfoResponseVersioningDisabled } },
});

const versioningToggleItem = screen
.getByRole('checkbox', {
name: /inactive/i,
})
.querySelector('input');

await waitFor(() => {
expect(versioningToggleItem).toBeInTheDocument();
});

versioningToggleItem && fireEvent.click(versioningToggleItem);

await waitFor(() => {
expect(useUpdateBucketVersioningMock).toHaveBeenCalledWith(mockResponse);
});
});
});
58 changes: 56 additions & 2 deletions src/react/databrowser/objects/__tests__/ObjectList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,46 @@ import {
SECOND_FORMATTED_OBJECT,
} from './utils/testUtil';
import { LIST_OBJECTS_S3_TYPE } from '../../../utils/s3';
import { checkBox, reduxMount } from '../../../utils/testUtil';
import {
TEST_API_BASE_URL,
checkBox,
reduxMount,
reduxRender,
} from '../../../utils/testUtil';
import { BUCKET_NAME } from '../../../actions/__tests__/utils/testUtil';
import { List } from 'immutable';
import ObjectList from '../ObjectList';
import router from 'react-router';
import { waitFor } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import { rest } from 'msw';
import { setupServer } from 'msw/node';

const server = setupServer(
rest.get(`${TEST_API_BASE_URL}/${BUCKET_NAME}`, (req, res, ctx) => {
if (req.url.searchParams.has('versioning')) {
return res(
ctx.status(200),
ctx.xml(
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/" />`,
),
);
}
}),
);

describe('ObjectList', () => {
beforeAll(() => {
jest.spyOn(router, 'useLocation').mockReturnValue({
pathname: '/buckets/test/objects',
});
server.listen({ onUnhandledRequest: 'error' });
});
afterEach(() => {
jest.clearAllMocks();
server.resetHandlers();
});
afterAll(() => server.close());
it('should render ObjectList with no object', () => {
const { component } = reduxMount(
<ObjectList
Expand Down Expand Up @@ -204,4 +229,33 @@ describe('ObjectList', () => {
expect(toggle.prop('disabled')).toBe(true);
});
});

it('should enable versioning toggle after updating bucket version', async () => {
server.use(
rest.get(`${TEST_API_BASE_URL}/${BUCKET_NAME}`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.xml(
`<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Status>Enabled</Status>
</VersioningConfiguration>`,
),
);
}),
);

reduxRender(
<ObjectList
objects={List([FIRST_FORMATTED_OBJECT])}
toggled={List()}
bucketName={BUCKET_NAME}
prefixWithSlash=""
listType={LIST_OBJECTS_S3_TYPE}
/>,
);

await waitFor(() => {
expect(screen.getByText('List Versions')).toBeEnabled();
});
});
});

0 comments on commit cf48fb3

Please sign in to comment.