2022-02-24 23:22:56 +00:00
|
|
|
import { renderWithQueryClient, within } from '@/react-tools/test-utils';
|
2022-11-13 08:10:18 +00:00
|
|
|
import { UserContext } from '@/react/hooks/useUser';
|
2022-02-24 23:22:56 +00:00
|
|
|
import { UserViewModel } from '@/portainer/models/user';
|
|
|
|
import { server, rest } from '@/setup-tests/server';
|
|
|
|
import {
|
|
|
|
createMockResourceGroups,
|
|
|
|
createMockSubscriptions,
|
|
|
|
} from '@/react-tools/test-mocks';
|
|
|
|
|
|
|
|
import { DashboardView } from './DashboardView';
|
|
|
|
|
|
|
|
jest.mock('@uirouter/react', () => ({
|
|
|
|
...jest.requireActual('@uirouter/react'),
|
|
|
|
useCurrentStateAndParams: jest.fn(() => ({
|
|
|
|
params: { endpointId: 1 },
|
|
|
|
})),
|
|
|
|
}));
|
|
|
|
|
|
|
|
test('dashboard items should render correctly', async () => {
|
2022-07-26 19:44:08 +00:00
|
|
|
const { findByLabelText } = await renderComponent();
|
2022-02-24 23:22:56 +00:00
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const subscriptionsItem = await findByLabelText('Subscription');
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(subscriptionsItem).toBeVisible();
|
|
|
|
|
|
|
|
const subscriptionElements = within(subscriptionsItem);
|
|
|
|
expect(subscriptionElements.getByLabelText('value')).toBeVisible();
|
2022-07-26 19:44:08 +00:00
|
|
|
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(subscriptionElements.getByLabelText('resourceType')).toHaveTextContent(
|
|
|
|
'Subscriptions'
|
|
|
|
);
|
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const resourceGroupsItem = await findByLabelText('Resource group');
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(resourceGroupsItem).toBeVisible();
|
|
|
|
|
|
|
|
const resourceGroupElements = within(resourceGroupsItem);
|
|
|
|
expect(resourceGroupElements.getByLabelText('value')).toBeVisible();
|
2022-07-26 19:44:08 +00:00
|
|
|
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(
|
|
|
|
resourceGroupElements.getByLabelText('resourceType')
|
|
|
|
).toHaveTextContent('Resource groups');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when there are no subscriptions, should show 0 subscriptions and 0 resource groups', async () => {
|
2022-07-26 19:44:08 +00:00
|
|
|
const { findByLabelText } = await renderComponent();
|
2022-02-24 23:22:56 +00:00
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const subscriptionElements = within(await findByLabelText('Subscription'));
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(subscriptionElements.getByLabelText('value')).toHaveTextContent('0');
|
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const resourceGroupElements = within(await findByLabelText('Resource group'));
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(resourceGroupElements.getByLabelText('value')).toHaveTextContent('0');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when there is subscription & resource group data, should display these', async () => {
|
2022-07-26 19:44:08 +00:00
|
|
|
const { findByLabelText } = await renderComponent(1, { 'subscription-1': 2 });
|
2022-02-24 23:22:56 +00:00
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const subscriptionElements = within(await findByLabelText('Subscription'));
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(subscriptionElements.getByLabelText('value')).toHaveTextContent('1');
|
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const resourceGroupElements = within(await findByLabelText('Resource group'));
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(resourceGroupElements.getByLabelText('value')).toHaveTextContent('2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should correctly show total number of resource groups across multiple subscriptions', async () => {
|
2022-07-26 19:44:08 +00:00
|
|
|
const { findByLabelText } = await renderComponent(2, {
|
2022-02-24 23:22:56 +00:00
|
|
|
'subscription-1': 2,
|
|
|
|
'subscription-2': 3,
|
|
|
|
});
|
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
const resourceGroupElements = within(await findByLabelText('Resource group'));
|
2022-02-24 23:22:56 +00:00
|
|
|
expect(resourceGroupElements.getByLabelText('value')).toHaveTextContent('5');
|
|
|
|
});
|
|
|
|
|
2022-07-26 19:44:08 +00:00
|
|
|
test("when only subscriptions fail to load, don't show the dashboard", async () => {
|
2022-02-24 23:22:56 +00:00
|
|
|
const { queryByLabelText } = await renderComponent(
|
|
|
|
1,
|
|
|
|
{ 'subscription-1': 1 },
|
|
|
|
500,
|
|
|
|
200
|
|
|
|
);
|
2022-07-06 08:23:53 +00:00
|
|
|
expect(queryByLabelText('Subscription')).not.toBeInTheDocument();
|
|
|
|
expect(queryByLabelText('Resource group')).not.toBeInTheDocument();
|
2022-02-24 23:22:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('when only resource groups fail to load, still show the subscriptions', async () => {
|
2022-07-26 19:44:08 +00:00
|
|
|
const { queryByLabelText, findByLabelText } = await renderComponent(
|
2022-02-24 23:22:56 +00:00
|
|
|
1,
|
|
|
|
{ 'subscription-1': 1 },
|
|
|
|
200,
|
|
|
|
500
|
|
|
|
);
|
2022-07-26 19:44:08 +00:00
|
|
|
await expect(findByLabelText('Subscription')).resolves.toBeInTheDocument();
|
2022-07-06 08:23:53 +00:00
|
|
|
expect(queryByLabelText('Resource group')).not.toBeInTheDocument();
|
2022-02-24 23:22:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
async function renderComponent(
|
|
|
|
subscriptionsCount = 0,
|
|
|
|
resourceGroups: Record<string, number> = {},
|
|
|
|
subscriptionsStatus = 200,
|
|
|
|
resourceGroupsStatus = 200
|
|
|
|
) {
|
|
|
|
const user = new UserViewModel({ Username: 'user' });
|
|
|
|
const state = { user };
|
|
|
|
|
|
|
|
server.use(
|
|
|
|
rest.get(
|
|
|
|
'/api/endpoints/:endpointId/azure/subscriptions',
|
|
|
|
(req, res, ctx) =>
|
|
|
|
res(
|
|
|
|
ctx.json(createMockSubscriptions(subscriptionsCount)),
|
|
|
|
ctx.status(subscriptionsStatus)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
rest.get(
|
|
|
|
'/api/endpoints/:endpointId/azure/subscriptions/:subscriptionId/resourcegroups',
|
|
|
|
(req, res, ctx) => {
|
|
|
|
if (typeof req.params.subscriptionId !== 'string') {
|
|
|
|
throw new Error("Provided subscriptionId must be of type: 'string'");
|
|
|
|
}
|
|
|
|
|
|
|
|
const { subscriptionId } = req.params;
|
|
|
|
return res(
|
|
|
|
ctx.json(
|
|
|
|
createMockResourceGroups(
|
|
|
|
req.params.subscriptionId,
|
|
|
|
resourceGroups[subscriptionId] || 0
|
|
|
|
)
|
|
|
|
),
|
|
|
|
ctx.status(resourceGroupsStatus)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const renderResult = renderWithQueryClient(
|
|
|
|
<UserContext.Provider value={state}>
|
|
|
|
<DashboardView />
|
|
|
|
</UserContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
await expect(renderResult.findByText(/Home/)).resolves.toBeVisible();
|
|
|
|
|
|
|
|
return renderResult;
|
|
|
|
}
|