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

144 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-08-27 11:52:29 +05:30
import { 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';
2022-08-27 11:52:29 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2021-12-11 22:18:48 +05:30
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';
2022-08-27 11:52:29 +05:30
import getGroupContactsCountByStateQuery from '~/crm/contacts/components/graphql/get_group_contacts_count_by_state.graphql';
2022-06-21 17:19:12 +05:30
import routes from '~/crm/contacts/routes';
2022-08-27 11:52:29 +05:30
import PaginatedTableWithSearchAndTabs from '~/vue_shared/components/paginated_table_with_search_and_tabs/paginated_table_with_search_and_tabs.vue';
import { getGroupContactsQueryResponse, getGroupContactsCountQueryResponse } from './mock_data';
2021-12-11 22:18:48 +05:30
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');
2022-08-27 11:52:29 +05:30
const findTable = () => wrapper.findComponent(PaginatedTableWithSearchAndTabs);
2021-12-11 22:18:48 +05:30
const successQueryHandler = jest.fn().mockResolvedValue(getGroupContactsQueryResponse);
2022-08-27 11:52:29 +05:30
const successCountQueryHandler = jest.fn().mockResolvedValue(getGroupContactsCountQueryResponse);
2021-12-11 22:18:48 +05:30
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,
2022-08-27 11:52:29 +05:30
countQueryHandler = successCountQueryHandler,
2022-01-26 12:08:38 +05:30
canAdminCrmContact = true,
2022-08-27 11:52:29 +05:30
textQuery = null,
2021-12-11 22:18:48 +05:30
} = {}) => {
2022-08-27 11:52:29 +05:30
fakeApollo = createMockApollo([
[getGroupContactsQuery, queryHandler],
[getGroupContactsCountByStateQuery, countQueryHandler],
]);
wrapper = mountExtended(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,
2022-08-27 11:52:29 +05:30
textQuery,
2022-01-26 12:08:38 +05:30
},
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
});
2022-08-27 11:52:29 +05:30
it('should render table with default props and loading state', () => {
2021-12-11 22:18:48 +05:30
mountComponent();
2022-08-27 11:52:29 +05:30
expect(findTable().props()).toMatchObject({
items: [],
itemsCount: {},
pageInfo: {},
statusTabs: [
{ title: 'Active', status: 'ACTIVE', filters: 'active' },
{ title: 'Inactive', status: 'INACTIVE', filters: 'inactive' },
{ title: 'All', status: 'ALL', filters: 'all' },
],
showItems: true,
showErrorMsg: false,
trackViewsOptions: { category: 'Customer Relations', action: 'view_contacts_list' },
i18n: {
emptyText: 'No contacts found',
issuesButtonLabel: 'View issues',
editButtonLabel: 'Edit',
title: 'Customer relations contacts',
newContact: 'New contact',
errorText: 'Something went wrong. Please try again.',
},
serverErrorMessage: '',
filterSearchKey: 'contacts',
filterSearchTokens: [],
});
2021-12-11 22:18:48 +05:30
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-08-27 11:52:29 +05:30
expect(wrapper.text()).toContain('Something went wrong. Please try again.');
2022-01-26 12:08:38 +05:30
});
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-08-27 11:52:29 +05:30
expect(wrapper.text()).not.toContain('Something went wrong. Please try again.');
2022-01-26 12:08:38 +05:30
});
it('renders correct results', async () => {
2022-08-27 11:52:29 +05:30
mountComponent();
2022-01-26 12:08:38 +05:30
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-08-27 11:52:29 +05:30
expect(issueLink.attributes('href')).toBe('/issues?crm_contact_id=12');
2022-01-26 12:08:38 +05:30
});
2021-12-11 22:18:48 +05:30
});
});