Welcome to mirror list, hosted at ThFree Co, Russian Federation.

work_item_ancestors_spec.js « work_item_ancestors « components « work_items « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9f66b20f06b2ee669b0350acc0ccf87e4590dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlPopover } from '@gitlab/ui';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';

import { createAlert } from '~/alert';
import DisclosureHierarchy from '~/work_items/components/work_item_ancestors/disclosure_hierarchy.vue';
import WorkItemAncestors from '~/work_items/components/work_item_ancestors/work_item_ancestors.vue';
import workItemAncestorsQuery from '~/work_items/graphql/work_item_ancestors.query.graphql';
import { formatAncestors } from '~/work_items/utils';

import { workItemTask } from '../../mock_data';
import {
  workItemAncestorsQueryResponse,
  workItemEmptyAncestorsQueryResponse,
  workItemThreeAncestorsQueryResponse,
} from './mock_data';

Vue.use(VueApollo);
jest.mock('~/alert');

describe('WorkItemAncestors', () => {
  let wrapper;
  let mockApollo;

  const workItemAncestorsQueryHandler = jest.fn().mockResolvedValue(workItemAncestorsQueryResponse);
  const workItemEmptyAncestorsQueryHandler = jest
    .fn()
    .mockResolvedValue(workItemEmptyAncestorsQueryResponse);
  const workItemThreeAncestorsQueryHandler = jest
    .fn()
    .mockResolvedValue(workItemThreeAncestorsQueryResponse);
  const workItemAncestorsFailureHandler = jest.fn().mockRejectedValue(new Error());

  const findDisclosureHierarchy = () => wrapper.findComponent(DisclosureHierarchy);
  const findPopover = () => wrapper.findComponent(GlPopover);

  const createComponent = ({
    props = {},
    options = {},
    ancestorsQueryHandler = workItemAncestorsQueryHandler,
  } = {}) => {
    mockApollo = createMockApollo([[workItemAncestorsQuery, ancestorsQueryHandler]]);
    return mountExtended(WorkItemAncestors, {
      apolloProvider: mockApollo,
      propsData: {
        workItem: workItemTask,
        ...props,
      },
      ...options,
    });
  };

  beforeEach(async () => {
    createAlert.mockClear();
    wrapper = createComponent();
    await waitForPromises();
  });

  it('fetches work item ancestors', () => {
    expect(workItemAncestorsQueryHandler).toHaveBeenCalled();
  });

  it('displays DisclosureHierarchy component with ancestors when work item has at least one ancestor', () => {
    expect(findDisclosureHierarchy().exists()).toBe(true);
    expect(findDisclosureHierarchy().props('items')).toEqual(
      expect.objectContaining(formatAncestors(workItemAncestorsQueryResponse.data.workItem)),
    );
  });

  it('does not display DisclosureHierarchy component when work item has no ancestor', async () => {
    wrapper = createComponent({ ancestorsQueryHandler: workItemEmptyAncestorsQueryHandler });
    await waitForPromises();

    expect(findDisclosureHierarchy().exists()).toBe(false);
  });

  it('displays work item info in popover on hover and focus', () => {
    expect(findPopover().exists()).toBe(true);
    expect(findPopover().props('triggers')).toBe('hover focus');

    const ancestor = findDisclosureHierarchy().props('items')[0];

    expect(findPopover().text()).toContain(ancestor.title);
    expect(findPopover().text()).toContain(ancestor.reference);
  });

  describe('when work item has less than 3 ancestors', () => {
    it('does not activate ellipsis option for DisclosureHierarchy component', () => {
      expect(findDisclosureHierarchy().props('withEllipsis')).toBe(false);
    });
  });

  describe('when work item has at least 3 ancestors', () => {
    beforeEach(async () => {
      wrapper = createComponent({ ancestorsQueryHandler: workItemThreeAncestorsQueryHandler });
      await waitForPromises();
    });

    it('activates ellipsis option for DisclosureHierarchy component', () => {
      expect(findDisclosureHierarchy().props('withEllipsis')).toBe(true);
    });
  });

  it('creates alert when the query fails', async () => {
    createComponent({ ancestorsQueryHandler: workItemAncestorsFailureHandler });
    await waitForPromises();

    expect(createAlert).toHaveBeenCalledWith({
      captureError: true,
      error: expect.any(Object),
      message: 'Something went wrong while fetching ancestors.',
    });
  });
});