debian-mirror-gitlab/spec/frontend/crm/contacts_root_spec.js

112 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-01-26 12:08:38 +05:30
import { GlAlert, GlLoadingIcon } from '@gitlab/ui';
2021-12-11 22:18:48 +05:30
import Vue from 'vue';
import VueApollo from 'vue-apollo';
2022-01-26 12:08:38 +05:30
import VueRouter from 'vue-router';
2021-12-11 22:18:48 +05:30
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
2022-06-21 17:19:12 +05:30
import ContactsRoot from '~/crm/contacts/components/contacts_root.vue';
import getGroupContactsQuery from '~/crm/contacts/components/graphql/get_group_contacts.query.graphql';
import routes from '~/crm/contacts/routes';
2021-12-11 22:18:48 +05:30
import { getGroupContactsQueryResponse } from './mock_data';
describe('Customer relations contacts root app', () => {
Vue.use(VueApollo);
2022-01-26 12:08:38 +05:30
Vue.use(VueRouter);
2021-12-11 22:18:48 +05:30
let wrapper;
let fakeApollo;
2022-01-26 12:08:38 +05:30
let router;
2021-12-11 22:18:48 +05:30
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findRowByName = (rowName) => wrapper.findAllByRole('row', { name: rowName });
2022-01-26 12:08:38 +05:30
const findIssuesLinks = () => wrapper.findAllByTestId('issues-link');
const findNewContactButton = () => wrapper.findByTestId('new-contact-button');
const findError = () => wrapper.findComponent(GlAlert);
2021-12-11 22:18:48 +05:30
const successQueryHandler = jest.fn().mockResolvedValue(getGroupContactsQueryResponse);
2022-01-26 12:08:38 +05:30
const basePath = '/groups/flightjs/-/crm/contacts';
2021-12-11 22:18:48 +05:30
const mountComponent = ({
queryHandler = successQueryHandler,
mountFunction = shallowMountExtended,
2022-01-26 12:08:38 +05:30
canAdminCrmContact = true,
2021-12-11 22:18:48 +05:30
} = {}) => {
fakeApollo = createMockApollo([[getGroupContactsQuery, queryHandler]]);
wrapper = mountFunction(ContactsRoot, {
2022-01-26 12:08:38 +05:30
router,
provide: {
groupFullPath: 'flightjs',
groupId: 26,
2022-06-21 17:19:12 +05:30
groupIssuesPath: '/issues',
2022-01-26 12:08:38 +05:30
canAdminCrmContact,
},
2021-12-11 22:18:48 +05:30
apolloProvider: fakeApollo,
});
};
2022-01-26 12:08:38 +05:30
beforeEach(() => {
router = new VueRouter({
base: basePath,
mode: 'history',
routes,
});
});
2021-12-11 22:18:48 +05:30
afterEach(() => {
wrapper.destroy();
fakeApollo = null;
2022-01-26 12:08:38 +05:30
router = null;
2021-12-11 22:18:48 +05:30
});
it('should render loading spinner', () => {
mountComponent();
expect(findLoadingIcon().exists()).toBe(true);
});
2022-01-26 12:08:38 +05:30
describe('new contact button', () => {
it('should exist when user has permission', () => {
mountComponent();
expect(findNewContactButton().exists()).toBe(true);
});
it('should not exist when user has no permission', () => {
mountComponent({ canAdminCrmContact: false });
expect(findNewContactButton().exists()).toBe(false);
});
});
describe('error', () => {
it('should exist on reject', async () => {
mountComponent({ queryHandler: jest.fn().mockRejectedValue('ERROR') });
await waitForPromises();
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
expect(findError().exists()).toBe(true);
});
2021-12-11 22:18:48 +05:30
});
2022-01-26 12:08:38 +05:30
describe('on successful load', () => {
it('should not render error', async () => {
mountComponent();
await waitForPromises();
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
expect(findError().exists()).toBe(false);
});
it('renders correct results', async () => {
mountComponent({ mountFunction: mountExtended });
await waitForPromises();
expect(findRowByName(/Marty/i)).toHaveLength(1);
expect(findRowByName(/George/i)).toHaveLength(1);
expect(findRowByName(/jd@gitlab.com/i)).toHaveLength(1);
const issueLink = findIssuesLinks().at(0);
expect(issueLink.exists()).toBe(true);
2022-07-16 23:28:13 +05:30
expect(issueLink.attributes('href')).toBe('/issues?crm_contact_id=16');
2022-01-26 12:08:38 +05:30
});
2021-12-11 22:18:48 +05:30
});
});