Skip to content

Commit

Permalink
ZKUI-397: Add test for useBucketTagging
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengYanJin committed Nov 7, 2023
1 parent 613649e commit a8e2166
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/js/mock/S3ClientMSWHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,33 @@ export const mockObjectEmpty = (bucketName: string) => {
},
);
};

export const mockGetBucketTagging = (bucketName: string) => {
return rest.get(
`${zenkoUITestConfig.zenkoEndpoint}/${bucketName}`,
(req, res, ctx) => {
if (req.url.searchParams.has('tagging')) {
return res(
ctx.xml(`
<?xml version="1.0" encoding="UTF-8"?>
<Tagging>
<TagSet>
<Tag><Key>Usecase</Key><Value>Veeam 12</Value></Tag>
</TagSet>
</Tagging>`),
);
}
},
);
};

export const mockGetBucketTaggingError = (bucketName: string) => {
return rest.get(
`${zenkoUITestConfig.zenkoEndpoint}/${bucketName}`,
(req, res, ctx) => {
if (req.url.searchParams.has('tagging')) {
return res(ctx.status(500));
}
},
);
};
57 changes: 55 additions & 2 deletions src/react/next-architecture/domain/business/buckets.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import {
DEFAULT_LOCATION,
useBucketLatestUsedCapacity,
useBucketLocationConstraint,
useBucketTagging,
useListBucketsForCurrentAccount,
} from './buckets';
import { IMetricsAdapter } from '../../adapters/metrics/IMetricsAdapter';
import { RenderResult, WaitFor } from '@testing-library/react-hooks';
import {
RenderResult,
WaitFor,
renderHook,
} from '@testing-library/react-hooks';
import { MockedMetricsAdapter } from '../../adapters/metrics/MockedMetricsAdapter';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
Expand All @@ -22,10 +27,16 @@ import {
RenderAdditionalHook,
} from '../../../utils/testMultipleHooks';
import { _AuthContext } from '../../ui/AuthProvider';
import { Wrapper, zenkoUITestConfig } from '../../../utils/testUtil';
import {
NewWrapper,
Wrapper,
zenkoUITestConfig,
} from '../../../utils/testUtil';
import {
mockBucketListing,
mockBucketOperations,
mockGetBucketTagging,
mockGetBucketTaggingError,
} from '../../../../js/mock/S3ClientMSWHandlers';

jest.setTimeout(30000);
Expand Down Expand Up @@ -887,4 +898,46 @@ describe('Buckets domain', () => {
});
});
});

describe('useBucketTagging', () => {
it('should return the tags for a specific bucket', async () => {
//Setup
const BUCKET_NAME = 'bucket-name';
server.use(mockGetBucketTagging(BUCKET_NAME));
const { waitFor, result } = renderHook(
() => useBucketTagging({ bucketName: BUCKET_NAME }),
{ wrapper: NewWrapper() },
);
//Exercise
await waitFor(() => result.current.tags.status === 'success');
//Verify
expect(result.current).toEqual({
tags: {
status: 'success',
value: {
Usecase: 'Veeam 12',
},
},
});
});
it('should return an error if the tags fetching failed', async () => {
//Setup
const BUCKET_NAME = 'bucket-name';
server.use(mockGetBucketTaggingError(BUCKET_NAME));
const { waitFor, result } = renderHook(
() => useBucketTagging({ bucketName: BUCKET_NAME }),
{ wrapper: NewWrapper() },
);
//Exercise
await waitFor(() => result.current.tags.status === 'error');
//Verify
expect(result.current).toEqual({
tags: {
status: 'error',
title: 'An error occurred while fetching the tags',
reason: 'Internal Server Error',
},
});
});
});
});
10 changes: 9 additions & 1 deletion src/react/utils/testUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mount, ReactWrapper } from 'enzyme';
import thunk from 'redux-thunk';
import { createMemoryHistory } from 'history';
import IAMClient from '../../js/IAMClient';
import { QueryClient, QueryClientProvider } from 'react-query';
import { QueryClient, QueryClientProvider, setLogger } from 'react-query';
import { Route, Router } from 'react-router-dom';

import { fireEvent, render } from '@testing-library/react';
Expand Down Expand Up @@ -144,6 +144,14 @@ export const queryClient = new QueryClient({
},
});

//Note: React Query version 4 setLogger function is removed.
setLogger({
log: console.log,
warn: console.warn,
// ✅ no more errors on the console
error: () => {},

Check failure on line 152 in src/react/utils/testUtil.tsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected empty method 'error'
});

export const zenkoUITestConfig = {
iamEndpoint: TEST_API_BASE_URL,
managementEndpoint: TEST_API_BASE_URL,
Expand Down

0 comments on commit a8e2166

Please sign in to comment.