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

award_utils_spec.js « notes « work_items « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ae32ce5f4081b1f6bf5097b55b44e361f61d5bd (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
import { getMutation, optimisticAwardUpdate } from '~/work_items/notes/award_utils';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import mockApollo from 'helpers/mock_apollo_helper';
import { __ } from '~/locale';
import workItemNotesByIidQuery from '~/work_items/graphql/notes/work_item_notes_by_iid.query.graphql';
import addAwardEmojiMutation from '~/work_items/graphql/notes/work_item_note_add_award_emoji.mutation.graphql';
import removeAwardEmojiMutation from '~/work_items/graphql/notes/work_item_note_remove_award_emoji.mutation.graphql';
import {
  mockWorkItemNotesResponseWithComments,
  mockAwardEmojiThumbsUp,
  mockAwardEmojiThumbsDown,
} from '../mock_data';

function getWorkItem(data) {
  return data.workspace.workItems.nodes[0];
}
function getFirstNote(workItem) {
  return workItem.widgets.find((w) => w.type === 'NOTES').discussions.nodes[0].notes.nodes[0];
}

describe('Work item note award utils', () => {
  const workItem = getWorkItem(mockWorkItemNotesResponseWithComments.data);
  const firstNote = getFirstNote(workItem);
  const fullPath = 'test-project-path';
  const workItemIid = workItem.iid;
  const currentUserId = getIdFromGraphQLId(mockAwardEmojiThumbsDown.user.id);

  beforeEach(() => {
    window.gon = { current_user_id: currentUserId };
  });

  describe('getMutation', () => {
    it('returns remove mutation when user has already awarded award', () => {
      const note = firstNote;
      const { name } = mockAwardEmojiThumbsDown;

      expect(getMutation({ note, name })).toEqual({
        mutation: removeAwardEmojiMutation,
        mutationName: 'awardEmojiRemove',
        errorMessage: __('Failed to remove emoji. Please try again'),
      });
    });

    it('returns remove mutation when user has not already awarded award', () => {
      const note = {};
      const { name } = mockAwardEmojiThumbsUp;

      expect(getMutation({ note, name })).toEqual({
        mutation: addAwardEmojiMutation,
        mutationName: 'awardEmojiAdd',
        errorMessage: __('Failed to add emoji. Please try again'),
      });
    });
  });

  describe('optimisticAwardUpdate', () => {
    let apolloProvider;
    beforeEach(() => {
      apolloProvider = mockApollo();

      apolloProvider.clients.defaultClient.writeQuery({
        query: workItemNotesByIidQuery,
        variables: { fullPath, iid: workItemIid },
        ...mockWorkItemNotesResponseWithComments,
      });
    });

    it('adds new emoji to cache', () => {
      const note = firstNote;
      const { name } = mockAwardEmojiThumbsUp;

      const updateFn = optimisticAwardUpdate({ note, name, fullPath, workItemIid });

      updateFn(apolloProvider.clients.defaultClient.cache);

      const updatedResult = apolloProvider.clients.defaultClient.readQuery({
        query: workItemNotesByIidQuery,
        variables: { fullPath, iid: workItemIid },
      });

      const updatedWorkItem = getWorkItem(updatedResult);
      const updatedNote = getFirstNote(updatedWorkItem);

      expect(updatedNote.awardEmoji.nodes).toEqual([
        mockAwardEmojiThumbsDown,
        mockAwardEmojiThumbsUp,
      ]);
    });

    it('removes existing emoji from cache', () => {
      const note = firstNote;
      const { name } = mockAwardEmojiThumbsDown;

      const updateFn = optimisticAwardUpdate({ note, name, fullPath, workItemIid });

      updateFn(apolloProvider.clients.defaultClient.cache);

      const updatedResult = apolloProvider.clients.defaultClient.readQuery({
        query: workItemNotesByIidQuery,
        variables: { fullPath, iid: workItemIid },
      });

      const updatedWorkItem = getWorkItem(updatedResult);
      const updatedNote = getFirstNote(updatedWorkItem);

      expect(updatedNote.awardEmoji.nodes).toEqual([]);
    });
  });
});