Skip to content

Commit

Permalink
Refactor Accounts.test.tsx and add new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hervedombya committed Nov 21, 2023
1 parent a90094c commit b86551a
Showing 1 changed file with 77 additions and 12 deletions.
89 changes: 77 additions & 12 deletions src/react/account/__tests__/Accounts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@ import { screen, waitFor } from '@testing-library/react';
import { List } from 'immutable';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import {
ACCOUNT_ID,
USERS,
getConfigOverlay,
getStorageConsumptionMetricsHandlers,
} from '../../../js/mock/managementClientMSWHandlers';
import { INSTANCE_ID } from '../../actions/__tests__/utils/testUtil';
import { useAuth } from '../../next-architecture/ui/AuthProvider';
import { useConfig } from '../../next-architecture/ui/ConfigProvider';
import { initialErrorsUIState } from '../../reducers/initialConstants';
import { formatSimpleDate } from '../../utils';
import {
TEST_API_BASE_URL,
WrapperAsStorageManager,
mockOffsetSize,
reduxRender,
renderWithRouterMatch,
TEST_API_BASE_URL,
WrapperAsStorageManager,
zenkoUITestConfig,
} from '../../utils/testUtil';
import Accounts from '../Accounts';
import { debug } from 'jest-preview';
import {
ACCOUNT_ID,
USERS,
getConfigOverlay,
getStorageConsumptionMetricsHandlers,
} from '../../../js/mock/managementClientMSWHandlers';
import { INSTANCE_ID } from '../../actions/__tests__/utils/testUtil';
import { useAuth } from '../../next-architecture/ui/AuthProvider';

const TEST_ACCOUNT =
USERS.find((user) => user.id === '064609833007')?.userName ?? '';
const TEST_ACCOUNT_CREATION_DATE =
USERS.find((user) => user.id === '064609833007')?.createDate ?? '';
const NO_ACCOUNT_MESSAGE = "You don't have any account yet.";

const mockUseConfig = useConfig as jest.MockedFunction<typeof useConfig>;
const mockUseAuth = useAuth as jest.MockedFunction<typeof useAuth>;

const server = setupServer(
rest.post(`${TEST_API_BASE_URL}/`, (req, res, ctx) => {
Expand Down Expand Up @@ -69,6 +73,13 @@ afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe('Accounts', () => {
const selectors = {
createAccountButton: () =>
screen.getByRole('button', { name: /Create Account/i }),
startVeeamConfgurationButton: () =>
screen.getByRole('button', { name: /Start Configuration for Veeam/i }),
};

it('should list accounts on which user can assume a role', async () => {
//E
renderWithRouterMatch(<Accounts />);
Expand Down Expand Up @@ -240,13 +251,14 @@ describe('Accounts', () => {
});

it('should hide Create Account Button for Storage Account Owner', async () => {
useAuth.mockImplementation(() => {
mockUseAuth.mockImplementation(() => {
return {
userData: {
id: 'xxx-yyy-zzzz-id',
token: 'xxx-yyy-zzz-token',
username: 'Renard ADMIN',
email: '[email protected]',
roles: ['PlatformAdmin'],
groups: ['user', 'PlatformAdmin'],
},
};
Expand Down Expand Up @@ -281,4 +293,57 @@ describe('Accounts', () => {
screen.queryByRole('button', { name: /Create Account/i }),
).not.toBeInTheDocument();
});

it('should display Veeam Configuration for Veeam User and Storage Manager when there are no accounts', async () => {
// S
server.use(
rest.post(`${TEST_API_BASE_URL}/`, (req, res, ctx) =>
res(
ctx.json({
IsTruncated: false,
Accounts: [],
}),
),
),
);

mockUseConfig.mockImplementation(() => {
return {
...zenkoUITestConfig,
iamInternalFQDN: TEST_API_BASE_URL,
s3InternalFQDN: TEST_API_BASE_URL,
basePath: '',
features: ['Veeam'],
};
});

mockUseAuth.mockImplementation(() => {
return {
userData: {
id: 'xxx-yyy-zzzz-id',
token: 'xxx-yyy-zzz-token',
username: 'Renard ADMIN',
email: '[email protected]',
roles: ['StorageManager'],
groups: ['user', 'StorageManager'],
},
};
});

//E
reduxRender(
<WrapperAsStorageManager isStorageManager>
<Accounts />
</WrapperAsStorageManager>,
{
auth: { config: { iamEndpoint: TEST_API_BASE_URL } },
},
);

//V
await waitFor(() => screen.getByText(NO_ACCOUNT_MESSAGE));

expect(selectors.createAccountButton()).toBeInTheDocument();
expect(selectors.startVeeamConfgurationButton()).toBeInTheDocument();
});
});

0 comments on commit b86551a

Please sign in to comment.