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

user_counts_manager_spec.js « super_sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b2ee5b09918e21be936cfcab3f75771ccec2fba (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import waitForPromises from 'helpers/wait_for_promises';

import * as UserApi from '~/api/user_api';
import {
  createUserCountsManager,
  userCounts,
  destroyUserCountsManager,
} from '~/super_sidebar/user_counts_manager';
import { fetchUserCounts } from '~/super_sidebar/user_counts_fetch';

jest.mock('~/api');

const USER_ID = 123;
const userCountDefaults = {
  todos: 1,
  assigned_issues: 2,
  assigned_merge_requests: 3,
  review_requested_merge_requests: 4,
};

const userCountUpdate = {
  todos: 123,
  assigned_issues: 456,
  assigned_merge_requests: 789,
  review_requested_merge_requests: 101112,
};

describe('User Merge Requests', () => {
  let channelMock;
  let newBroadcastChannelMock;

  beforeEach(() => {
    jest.spyOn(document, 'removeEventListener');
    jest.spyOn(document, 'addEventListener');

    global.gon.current_user_id = USER_ID;

    channelMock = {
      postMessage: jest.fn(),
      close: jest.fn(),
    };
    newBroadcastChannelMock = jest.fn().mockImplementation(() => channelMock);

    Object.assign(userCounts, userCountDefaults, { last_update: 0 });

    global.BroadcastChannel = newBroadcastChannelMock;
  });

  describe('createUserCountsManager', () => {
    beforeEach(() => {
      createUserCountsManager();
    });

    it('creates BroadcastChannel which updates counts on message received', () => {
      expect(newBroadcastChannelMock).toHaveBeenCalledWith(`user_counts_${USER_ID}`);
    });

    it('closes BroadCastchannel if called while already open', () => {
      expect(channelMock.close).not.toHaveBeenCalled();

      createUserCountsManager();

      expect(channelMock.close).toHaveBeenCalled();
    });

    describe('BroadcastChannel onmessage handler', () => {
      it('updates counts on message received', () => {
        expect(userCounts).toMatchObject(userCountDefaults);

        channelMock.onmessage({ data: { ...userCountUpdate, last_update: Date.now() } });

        expect(userCounts).toMatchObject(userCountUpdate);
      });

      it('ignores updates with older data', () => {
        expect(userCounts).toMatchObject(userCountDefaults);
        userCounts.last_update = Date.now();

        channelMock.onmessage({
          data: { ...userCountUpdate, last_update: userCounts.last_update - 1000 },
        });

        expect(userCounts).toMatchObject(userCountDefaults);
      });

      it('ignores unknown fields', () => {
        expect(userCounts).toMatchObject(userCountDefaults);

        channelMock.onmessage({ data: { ...userCountUpdate, i_am_unknown: 5 } });

        expect(userCounts).toMatchObject(userCountUpdate);
        expect(userCounts.i_am_unknown).toBeUndefined();
      });
    });

    it('broadcasts user counts during initialization', () => {
      expect(channelMock.postMessage).toHaveBeenCalledWith(
        expect.objectContaining(userCountDefaults),
      );
    });

    it('setups event listener without leaking them', () => {
      expect(document.removeEventListener).toHaveBeenCalledWith(
        'userCounts:fetch',
        expect.any(Function),
      );
      expect(document.addEventListener).toHaveBeenCalledWith(
        'userCounts:fetch',
        expect.any(Function),
      );
    });
  });

  describe('Event listener userCounts:fetch', () => {
    beforeEach(() => {
      jest.spyOn(UserApi, 'getUserCounts').mockResolvedValue({
        data: { ...userCountUpdate, merge_requests: 'FOO' },
      });
      createUserCountsManager();
    });

    describe('manually created event', () => {
      it('fetches counts from API, stores and rebroadcasts them', async () => {
        expect(userCounts).toMatchObject(userCountDefaults);

        document.dispatchEvent(new CustomEvent('userCounts:fetch'));
        await waitForPromises();

        expect(UserApi.getUserCounts).toHaveBeenCalled();
        expect(userCounts).toMatchObject(userCountUpdate);
        expect(channelMock.postMessage).toHaveBeenLastCalledWith(userCounts);
      });
    });

    describe('fetchUserCounts helper', () => {
      it('fetches counts from API, stores and rebroadcasts them', async () => {
        expect(userCounts).toMatchObject(userCountDefaults);

        fetchUserCounts();
        await waitForPromises();

        expect(UserApi.getUserCounts).toHaveBeenCalled();
        expect(userCounts).toMatchObject(userCountUpdate);
        expect(channelMock.postMessage).toHaveBeenLastCalledWith(userCounts);
      });
    });
  });

  describe('destroyUserCountsManager', () => {
    it('unregisters event handler', () => {
      expect(document.removeEventListener).not.toHaveBeenCalledWith();

      destroyUserCountsManager();

      expect(document.removeEventListener).toHaveBeenCalledWith(
        'userCounts:fetch',
        expect.any(Function),
      );
    });

    describe('when BroadcastChannel is not opened', () => {
      it('does nothing', () => {
        destroyUserCountsManager();
        expect(channelMock.close).not.toHaveBeenCalled();
      });
    });

    describe('when BroadcastChannel is opened', () => {
      beforeEach(() => {
        createUserCountsManager();
      });

      it('closes BroadcastChannel', () => {
        expect(channelMock.close).not.toHaveBeenCalled();

        destroyUserCountsManager();

        expect(channelMock.close).toHaveBeenCalled();
      });
    });
  });
});