2022-10-11 01:57:18 +05:30
|
|
|
import { GlSearchBoxByType, GlSkeletonLoader } from '@gitlab/ui';
|
2022-04-04 11:22:00 +05:30
|
|
|
import Vue from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
import { createAlert } from '~/flash';
|
|
|
|
import { sprintf } from '~/locale';
|
|
|
|
import {
|
|
|
|
I18N_ASSIGNED_PROJECTS,
|
2022-10-11 01:57:18 +05:30
|
|
|
I18N_CLEAR_FILTER_PROJECTS,
|
|
|
|
I18N_FILTER_PROJECTS,
|
|
|
|
I18N_NO_PROJECTS_FOUND,
|
2022-04-04 11:22:00 +05:30
|
|
|
RUNNER_DETAILS_PROJECTS_PAGE_SIZE,
|
2023-01-13 00:05:48 +05:30
|
|
|
} from '~/ci/runner/constants';
|
|
|
|
import RunnerProjects from '~/ci/runner/components/runner_projects.vue';
|
|
|
|
import RunnerAssignedItem from '~/ci/runner/components/runner_assigned_item.vue';
|
|
|
|
import RunnerPagination from '~/ci/runner/components/runner_pagination.vue';
|
|
|
|
import { captureException } from '~/ci/runner/sentry_utils';
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
import runnerProjectsQuery from '~/ci/runner/graphql/show/runner_projects.query.graphql';
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
import { runnerData, runnerProjectsData } from '../mock_data';
|
|
|
|
|
|
|
|
jest.mock('~/flash');
|
2023-01-13 00:05:48 +05:30
|
|
|
jest.mock('~/ci/runner/sentry_utils');
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
const mockRunner = runnerData.data.runner;
|
|
|
|
const mockRunnerWithProjects = runnerProjectsData.data.runner;
|
|
|
|
const mockProjects = mockRunnerWithProjects.projects.nodes;
|
|
|
|
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
|
|
|
describe('RunnerProjects', () => {
|
|
|
|
let wrapper;
|
|
|
|
let mockRunnerProjectsQuery;
|
|
|
|
|
|
|
|
const findHeading = () => wrapper.find('h3');
|
2022-07-23 23:45:48 +05:30
|
|
|
const findGlSkeletonLoading = () => wrapper.findComponent(GlSkeletonLoader);
|
2022-10-11 01:57:18 +05:30
|
|
|
const findGlSearchBoxByType = () => wrapper.findComponent(GlSearchBoxByType);
|
2022-04-04 11:22:00 +05:30
|
|
|
const findRunnerAssignedItems = () => wrapper.findAllComponents(RunnerAssignedItem);
|
|
|
|
const findRunnerPagination = () => wrapper.findComponent(RunnerPagination);
|
|
|
|
|
|
|
|
const createComponent = ({ mountFn = shallowMountExtended } = {}) => {
|
|
|
|
wrapper = mountFn(RunnerProjects, {
|
2022-05-07 20:08:51 +05:30
|
|
|
apolloProvider: createMockApollo([[runnerProjectsQuery, mockRunnerProjectsQuery]]),
|
2022-04-04 11:22:00 +05:30
|
|
|
propsData: {
|
|
|
|
runner: mockRunner,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockRunnerProjectsQuery = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockRunnerProjectsQuery.mockReset();
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Requests runner projects', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledWith({
|
|
|
|
id: mockRunner.id,
|
2022-10-11 01:57:18 +05:30
|
|
|
search: '',
|
2022-04-04 11:22:00 +05:30
|
|
|
first: RUNNER_DETAILS_PROJECTS_PAGE_SIZE,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it('Shows a filter box', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(findGlSearchBoxByType().attributes()).toMatchObject({
|
|
|
|
clearbuttontitle: I18N_CLEAR_FILTER_PROJECTS,
|
|
|
|
debounce: '500',
|
|
|
|
placeholder: I18N_FILTER_PROJECTS,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
describe('When there are projects assigned', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mockRunnerProjectsQuery.mockResolvedValueOnce(runnerProjectsData);
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
await waitForPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Shows a heading', async () => {
|
|
|
|
const expected = sprintf(I18N_ASSIGNED_PROJECTS, { projectCount: mockProjects.length });
|
|
|
|
|
|
|
|
expect(findHeading().text()).toBe(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Shows projects', () => {
|
|
|
|
expect(findRunnerAssignedItems().length).toBe(mockProjects.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Shows a project', () => {
|
|
|
|
const item = findRunnerAssignedItems().at(0);
|
|
|
|
const { webUrl, name, nameWithNamespace, avatarUrl } = mockProjects[0];
|
|
|
|
|
|
|
|
expect(item.props()).toMatchObject({
|
|
|
|
href: webUrl,
|
|
|
|
name,
|
|
|
|
fullName: nameWithNamespace,
|
|
|
|
avatarUrl,
|
2022-08-27 11:52:29 +05:30
|
|
|
isOwner: true, // first project is always owner
|
2022-04-04 11:22:00 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When "Next" page is clicked', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
findRunnerPagination().vm.$emit('input', { page: 3, after: 'AFTER_CURSOR' });
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('A new page is requested', () => {
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(2);
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenLastCalledWith({
|
|
|
|
id: mockRunner.id,
|
2022-10-11 01:57:18 +05:30
|
|
|
search: '',
|
2022-04-04 11:22:00 +05:30
|
|
|
first: RUNNER_DETAILS_PROJECTS_PAGE_SIZE,
|
|
|
|
after: 'AFTER_CURSOR',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('When "Prev" page is clicked, the previous page is requested', async () => {
|
|
|
|
findRunnerPagination().vm.$emit('input', { page: 2, before: 'BEFORE_CURSOR' });
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(3);
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenLastCalledWith({
|
|
|
|
id: mockRunner.id,
|
2022-10-11 01:57:18 +05:30
|
|
|
search: '',
|
2022-04-04 11:22:00 +05:30
|
|
|
last: RUNNER_DETAILS_PROJECTS_PAGE_SIZE,
|
|
|
|
before: 'BEFORE_CURSOR',
|
|
|
|
});
|
|
|
|
});
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
it('When user filters after paginating, the first page is requested', async () => {
|
|
|
|
findGlSearchBoxByType().vm.$emit('input', 'my search');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(3);
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenLastCalledWith({
|
|
|
|
id: mockRunner.id,
|
|
|
|
search: 'my search',
|
|
|
|
first: RUNNER_DETAILS_PROJECTS_PAGE_SIZE,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When user filters', () => {
|
|
|
|
it('Filtered results are requested', async () => {
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
findGlSearchBoxByType().vm.$emit('input', 'my search');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(2);
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenLastCalledWith({
|
|
|
|
id: mockRunner.id,
|
|
|
|
search: 'my search',
|
|
|
|
first: RUNNER_DETAILS_PROJECTS_PAGE_SIZE,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Filtered results are not requested for short searches', async () => {
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
findGlSearchBoxByType().vm.$emit('input', 'm');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
findGlSearchBoxByType().vm.$emit('input', 'my');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockRunnerProjectsQuery).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2022-04-04 11:22:00 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When loading', () => {
|
|
|
|
it('shows loading indicator and no other content', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(findGlSkeletonLoading().exists()).toBe(true);
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(wrapper.findByText(I18N_NO_PROJECTS_FOUND).exists()).toBe(false);
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(findRunnerAssignedItems().length).toBe(0);
|
|
|
|
|
|
|
|
expect(findRunnerPagination().attributes('disabled')).toBe('true');
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(findGlSearchBoxByType().props('isLoading')).toBe(true);
|
2022-04-04 11:22:00 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When there are no projects', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mockRunnerProjectsQuery.mockResolvedValueOnce({
|
|
|
|
data: {
|
|
|
|
runner: {
|
|
|
|
id: mockRunner.id,
|
|
|
|
projectCount: 0,
|
|
|
|
projects: {
|
|
|
|
nodes: [],
|
|
|
|
pageInfo: {
|
|
|
|
hasNextPage: false,
|
|
|
|
hasPreviousPage: false,
|
|
|
|
startCursor: '',
|
|
|
|
endCursor: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
await waitForPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Shows a "None" label', () => {
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(wrapper.findByText(I18N_NO_PROJECTS_FOUND).exists()).toBe(true);
|
2022-04-04 11:22:00 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('When an error occurs', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mockRunnerProjectsQuery.mockRejectedValue(new Error('Error!'));
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
await waitForPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows an error', () => {
|
|
|
|
expect(createAlert).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reports an error', () => {
|
|
|
|
expect(captureException).toHaveBeenCalledWith({
|
|
|
|
component: 'RunnerProjects',
|
|
|
|
error: expect.any(Error),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|