debian-mirror-gitlab/spec/frontend/clusters/agents/components/show_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

262 lines
8 KiB
JavaScript
Raw Normal View History

2021-11-18 22:05:49 +05:30
import { GlAlert, GlKeysetPagination, GlLoadingIcon, GlSprintf, GlTab } from '@gitlab/ui';
import { createLocalVue, shallowMount } from '@vue/test-utils';
2021-12-11 22:18:48 +05:30
import { nextTick } from 'vue';
2021-11-18 22:05:49 +05:30
import VueApollo from 'vue-apollo';
2021-12-11 22:18:48 +05:30
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
2021-11-18 22:05:49 +05:30
import ClusterAgentShow from '~/clusters/agents/components/show.vue';
import TokenTable from '~/clusters/agents/components/token_table.vue';
2022-01-26 12:08:38 +05:30
import ActivityEvents from '~/clusters/agents/components/activity_events_list.vue';
2022-10-11 01:57:18 +05:30
import IntegrationStatus from '~/clusters/agents/components/integration_status.vue';
2021-11-18 22:05:49 +05:30
import getAgentQuery from '~/clusters/agents/graphql/queries/get_cluster_agent.query.graphql';
import { useFakeDate } from 'helpers/fake_date';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
2022-04-04 11:22:00 +05:30
import { MAX_LIST_COUNT, TOKEN_STATUS_ACTIVE } from '~/clusters/agents/constants';
2021-11-18 22:05:49 +05:30
const localVue = createLocalVue();
localVue.use(VueApollo);
describe('ClusterAgentShow', () => {
let wrapper;
2022-04-04 11:22:00 +05:30
let agentQueryResponse;
2021-11-18 22:05:49 +05:30
useFakeDate([2021, 2, 15]);
2022-03-02 08:16:31 +05:30
const provide = {
2021-11-18 22:05:49 +05:30
agentName: 'cluster-agent',
projectPath: 'path/to/project',
};
const defaultClusterAgent = {
id: '1',
createdAt: '2021-02-13T00:00:00Z',
createdByUser: {
2022-01-26 12:08:38 +05:30
id: 'user-1',
2021-11-18 22:05:49 +05:30
name: 'user-1',
},
name: 'token-1',
tokens: {
count: 1,
nodes: [],
pageInfo: null,
},
};
const createWrapper = ({ clusterAgent, queryResponse = null }) => {
2022-04-04 11:22:00 +05:30
agentQueryResponse =
2022-01-26 12:08:38 +05:30
queryResponse ||
jest.fn().mockResolvedValue({ data: { project: { id: 'project-1', clusterAgent } } });
2021-11-18 22:05:49 +05:30
const apolloProvider = createMockApollo([[getAgentQuery, agentQueryResponse]]);
2021-12-11 22:18:48 +05:30
wrapper = extendedWrapper(
shallowMount(ClusterAgentShow, {
localVue,
apolloProvider,
2022-03-02 08:16:31 +05:30
provide,
2021-12-11 22:18:48 +05:30
stubs: { GlSprintf, TimeAgoTooltip, GlTab },
}),
);
2021-11-18 22:05:49 +05:30
};
2021-12-11 22:18:48 +05:30
const createWrapperWithoutApollo = ({ clusterAgent, loading = false, slots = {} }) => {
2021-11-18 22:05:49 +05:30
const $apollo = { queries: { clusterAgent: { loading } } };
2021-12-11 22:18:48 +05:30
wrapper = extendedWrapper(
shallowMount(ClusterAgentShow, {
2022-03-02 08:16:31 +05:30
provide,
2021-12-11 22:18:48 +05:30
mocks: { $apollo, clusterAgent },
slots,
stubs: { GlTab },
}),
);
2021-11-18 22:05:49 +05:30
};
2021-12-11 22:18:48 +05:30
const findCreatedText = () => wrapper.findByTestId('cluster-agent-create-info').text();
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findPaginationButtons = () => wrapper.findComponent(GlKeysetPagination);
const findTokenCount = () => wrapper.findByTestId('cluster-agent-token-count').text();
const findEESecurityTabSlot = () => wrapper.findByTestId('ee-security-tab');
2022-01-26 12:08:38 +05:30
const findActivity = () => wrapper.findComponent(ActivityEvents);
2022-10-11 01:57:18 +05:30
const findIntegrationStatus = () => wrapper.findComponent(IntegrationStatus);
2021-11-18 22:05:49 +05:30
afterEach(() => {
wrapper.destroy();
});
describe('default behaviour', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
createWrapper({ clusterAgent: defaultClusterAgent });
await waitForPromises();
});
it('sends expected params', () => {
const variables = {
agentName: provide.agentName,
projectPath: provide.projectPath,
tokenStatus: TOKEN_STATUS_ACTIVE,
first: MAX_LIST_COUNT,
last: null,
};
expect(agentQueryResponse).toHaveBeenCalledWith(variables);
2021-11-18 22:05:49 +05:30
});
it('displays the agent name', () => {
2022-03-02 08:16:31 +05:30
expect(wrapper.text()).toContain(provide.agentName);
2021-11-18 22:05:49 +05:30
});
it('displays agent create information', () => {
expect(findCreatedText()).toMatchInterpolatedText('Created by user-1 2 days ago');
});
2022-10-11 01:57:18 +05:30
it('displays agent integration status section', () => {
expect(findIntegrationStatus().exists()).toBe(true);
});
2021-11-18 22:05:49 +05:30
it('displays token count', () => {
expect(findTokenCount()).toMatchInterpolatedText(
`${ClusterAgentShow.i18n.tokens} ${defaultClusterAgent.tokens.count}`,
);
});
it('renders token table', () => {
2021-12-11 22:18:48 +05:30
expect(wrapper.findComponent(TokenTable).exists()).toBe(true);
2021-11-18 22:05:49 +05:30
});
it('should not render pagination buttons when there are no additional pages', () => {
expect(findPaginationButtons().exists()).toBe(false);
});
2022-01-26 12:08:38 +05:30
it('renders activity events list', () => {
expect(findActivity().exists()).toBe(true);
});
2021-11-18 22:05:49 +05:30
});
describe('when create user is unknown', () => {
const missingUser = {
...defaultClusterAgent,
createdByUser: null,
};
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
createWrapper({ clusterAgent: missingUser });
await waitForPromises();
2021-11-18 22:05:49 +05:30
});
2022-04-04 11:22:00 +05:30
it('displays agent create information with unknown user', async () => {
await waitForPromises();
2021-11-18 22:05:49 +05:30
expect(findCreatedText()).toMatchInterpolatedText('Created by Unknown user 2 days ago');
});
});
describe('when token count is missing', () => {
const missingTokens = {
...defaultClusterAgent,
tokens: null,
};
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
createWrapper({ clusterAgent: missingTokens });
await waitForPromises();
2021-11-18 22:05:49 +05:30
});
2022-04-04 11:22:00 +05:30
it('displays token header with no count', async () => {
await waitForPromises();
2021-11-18 22:05:49 +05:30
expect(findTokenCount()).toMatchInterpolatedText(`${ClusterAgentShow.i18n.tokens}`);
});
});
describe('when the token list has additional pages', () => {
2022-04-04 11:22:00 +05:30
const pageInfoResponse = {
2021-11-18 22:05:49 +05:30
hasNextPage: true,
hasPreviousPage: false,
startCursor: 'prev',
endCursor: 'next',
};
2022-04-04 11:22:00 +05:30
const pageInfo = {
...pageInfoResponse,
__typename: 'PageInfo',
};
2021-11-18 22:05:49 +05:30
const tokenPagination = {
...defaultClusterAgent,
tokens: {
...defaultClusterAgent.tokens,
pageInfo,
},
};
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
createWrapper({ clusterAgent: tokenPagination });
await waitForPromises();
2021-11-18 22:05:49 +05:30
});
it('should render pagination buttons', () => {
expect(findPaginationButtons().exists()).toBe(true);
});
it('should pass pageInfo to the pagination component', () => {
2022-04-04 11:22:00 +05:30
expect(findPaginationButtons().props()).toMatchObject(pageInfoResponse);
2021-11-18 22:05:49 +05:30
});
});
describe('when the agent query is loading', () => {
describe('when the clusterAgent is missing', () => {
beforeEach(() => {
return createWrapper({
clusterAgent: null,
queryResponse: jest.fn().mockReturnValue(new Promise(() => {})),
});
});
it('displays a loading icon and hides the token tab', () => {
expect(findLoadingIcon().exists()).toBe(true);
expect(wrapper.text()).not.toContain(ClusterAgentShow.i18n.tokens);
});
});
describe('when the clusterAgent is present', () => {
beforeEach(() => {
createWrapperWithoutApollo({ clusterAgent: defaultClusterAgent, loading: true });
});
it('displays a loading icon and token tab', () => {
expect(findLoadingIcon().exists()).toBe(true);
expect(wrapper.text()).toContain(ClusterAgentShow.i18n.tokens);
});
});
});
describe('when the agent query has errored', () => {
beforeEach(() => {
createWrapper({ clusterAgent: null, queryResponse: jest.fn().mockRejectedValue() });
return waitForPromises();
});
it('displays an alert message', () => {
2021-12-11 22:18:48 +05:30
expect(wrapper.findComponent(GlAlert).exists()).toBe(true);
2021-11-18 22:05:49 +05:30
expect(wrapper.text()).toContain(ClusterAgentShow.i18n.loadingError);
});
});
2021-12-11 22:18:48 +05:30
describe('ee-security-tab slot', () => {
it('does not display when a slot is not passed in', async () => {
createWrapperWithoutApollo({ clusterAgent: defaultClusterAgent });
await nextTick();
expect(findEESecurityTabSlot().exists()).toBe(false);
});
it('does display when a slot is passed in', async () => {
createWrapperWithoutApollo({
clusterAgent: defaultClusterAgent,
slots: {
'ee-security-tab': `<gl-tab data-testid="ee-security-tab">Security Tab!</gl-tab>`,
},
});
await nextTick();
expect(findEESecurityTabSlot().exists()).toBe(true);
});
});
2021-11-18 22:05:49 +05:30
});