summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/assignees_realtime_spec.js
blob: 0fab6a29f71df4683bca0bbac3f1013ebf2d1b3e (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
import ActionCable from '@rails/actioncable';
import { shallowMount } from '@vue/test-utils';
import query from '~/issuable_sidebar/queries/issue_sidebar.query.graphql';
import AssigneesRealtime from '~/sidebar/components/assignees/assignees_realtime.vue';
import SidebarMediator from '~/sidebar/sidebar_mediator';
import Mock from './mock_data';

jest.mock('@rails/actioncable', () => {
  const mockConsumer = {
    subscriptions: { create: jest.fn().mockReturnValue({ unsubscribe: jest.fn() }) },
  };
  return {
    createConsumer: jest.fn().mockReturnValue(mockConsumer),
  };
});

describe('Assignees Realtime', () => {
  let wrapper;
  let mediator;

  const createComponent = () => {
    wrapper = shallowMount(AssigneesRealtime, {
      propsData: {
        issuableIid: '1',
        mediator,
        projectPath: 'path/to/project',
      },
      mocks: {
        $apollo: {
          query,
          queries: {
            project: {
              refetch: jest.fn(),
            },
          },
        },
      },
    });
  };

  beforeEach(() => {
    mediator = new SidebarMediator(Mock.mediator);
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
    SidebarMediator.singleton = null;
  });

  describe('when handleFetchResult is called from smart query', () => {
    it('sets assignees to the store', () => {
      const data = {
        project: {
          issue: {
            assignees: {
              nodes: [{ id: 'gid://gitlab/Environments/123', avatarUrl: 'url' }],
            },
          },
        },
      };
      const expected = [{ id: 123, avatar_url: 'url', avatarUrl: 'url' }];
      createComponent();

      wrapper.vm.handleFetchResult({ data });

      expect(mediator.store.assignees).toEqual(expected);
    });
  });

  describe('when mounted', () => {
    it('calls create subscription', () => {
      const cable = ActionCable.createConsumer();

      createComponent();

      return wrapper.vm.$nextTick().then(() => {
        expect(cable.subscriptions.create).toHaveBeenCalledTimes(1);
        expect(cable.subscriptions.create).toHaveBeenCalledWith(
          {
            channel: 'IssuesChannel',
            iid: wrapper.props('issuableIid'),
            project_path: wrapper.props('projectPath'),
          },
          { received: wrapper.vm.received },
        );
      });
    });
  });

  describe('when subscription is recieved', () => {
    it('refetches the GraphQL project query', () => {
      createComponent();

      wrapper.vm.received({ event: 'updated' });

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.vm.$apollo.queries.project.refetch).toHaveBeenCalledTimes(1);
      });
    });
  });
});