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

work_item_note_awards_list_spec.js « notes « components « work_items « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce9156359465fa23d40a841be1523cc10de8024d (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
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import mockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { __ } from '~/locale';
import AwardsList from '~/vue_shared/components/awards_list.vue';
import WorkItemNoteAwardsList from '~/work_items/components/notes/work_item_note_awards_list.vue';
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 workItemNotesByIidQuery from '~/work_items/graphql/notes/work_item_notes_by_iid.query.graphql';
import {
  mockWorkItemNotesResponseWithComments,
  mockAwardEmojiThumbsUp,
} from 'jest/work_items/mock_data';
import { EMOJI_THUMBSUP, EMOJI_THUMBSDOWN } from '~/work_items/constants';

Vue.use(VueApollo);

describe('Work Item Note Awards List', () => {
  let wrapper;
  const workItem = mockWorkItemNotesResponseWithComments.data.workspace.workItems.nodes[0];
  const firstNote = workItem.widgets.find((w) => w.type === 'NOTES').discussions.nodes[0].notes
    .nodes[0];
  const fullPath = 'test-project-path';
  const workItemIid = workItem.iid;
  const currentUserId = getIdFromGraphQLId(mockAwardEmojiThumbsUp.user.id);

  const addAwardEmojiMutationSuccessHandler = jest.fn().mockResolvedValue({
    data: {
      awardEmojiAdd: {
        errors: [],
      },
    },
  });
  const removeAwardEmojiMutationSuccessHandler = jest.fn().mockResolvedValue({
    data: {
      awardEmojiRemove: {
        errors: [],
      },
    },
  });

  const findAwardsList = () => wrapper.findComponent(AwardsList);

  const createComponent = ({
    note = firstNote,
    addAwardEmojiMutationHandler = addAwardEmojiMutationSuccessHandler,
    removeAwardEmojiMutationHandler = removeAwardEmojiMutationSuccessHandler,
  } = {}) => {
    const apolloProvider = mockApollo([
      [addAwardEmojiMutation, addAwardEmojiMutationHandler],
      [removeAwardEmojiMutation, removeAwardEmojiMutationHandler],
    ]);

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

    wrapper = shallowMount(WorkItemNoteAwardsList, {
      propsData: {
        fullPath,
        workItemIid,
        note,
        isModal: false,
      },
      apolloProvider,
    });
  };

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

  describe('when not editing', () => {
    it.each([true, false])('passes emoji permission to awards-list', (hasAwardEmojiPermission) => {
      const note = {
        ...firstNote,
        userPermissions: {
          ...firstNote.userPermissions,
          awardEmoji: hasAwardEmojiPermission,
        },
      };
      createComponent({ note });

      expect(findAwardsList().props('canAwardEmoji')).toBe(hasAwardEmojiPermission);
    });

    it('adds award if not already awarded', async () => {
      createComponent();
      await waitForPromises();

      findAwardsList().vm.$emit('award', EMOJI_THUMBSUP);

      expect(addAwardEmojiMutationSuccessHandler).toHaveBeenCalledWith({
        awardableId: firstNote.id,
        name: EMOJI_THUMBSUP,
      });
    });

    it('emits error if awarding emoji fails', async () => {
      createComponent({
        addAwardEmojiMutationHandler: jest.fn().mockRejectedValue('oh no'),
      });
      await waitForPromises();

      findAwardsList().vm.$emit('award', EMOJI_THUMBSUP);

      await waitForPromises();

      expect(wrapper.emitted('error')).toEqual([[__('Failed to add emoji. Please try again')]]);
    });

    it('removes award if already awarded', async () => {
      const removeAwardEmojiMutationHandler = removeAwardEmojiMutationSuccessHandler;

      createComponent({ removeAwardEmojiMutationHandler });

      findAwardsList().vm.$emit('award', EMOJI_THUMBSDOWN);

      await waitForPromises();

      expect(removeAwardEmojiMutationHandler).toHaveBeenCalledWith({
        awardableId: firstNote.id,
        name: EMOJI_THUMBSDOWN,
      });
    });

    it('restores award if remove fails', async () => {
      createComponent({
        removeAwardEmojiMutationHandler: jest.fn().mockRejectedValue('oh no'),
      });
      await waitForPromises();

      findAwardsList().vm.$emit('award', EMOJI_THUMBSDOWN);

      await waitForPromises();

      expect(wrapper.emitted('error')).toEqual([[__('Failed to remove emoji. Please try again')]]);
    });
  });
});