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

approvals_summary_spec.js « approvals « components « vue_mr_widget « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c26063462927c8c9ca6e56c6db63f6e9e0d5dc36 (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
import { shallowMount } from '@vue/test-utils';
import { toNounSeriesText } from '~/lib/utils/grammar';
import ApprovalsSummary from '~/vue_merge_request_widget/components/approvals/approvals_summary.vue';
import {
  APPROVED_BY_OTHERS,
  APPROVED_BY_YOU,
  APPROVED_BY_YOU_AND_OTHERS,
} from '~/vue_merge_request_widget/components/approvals/messages';
import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue';

const exampleUserId = 1;
const testApprovers = () => Array.from({ length: 5 }, (_, i) => i).map((id) => ({ id }));
const testRulesLeft = () => ['Lorem', 'Ipsum', 'dolar & sit'];
const TEST_APPROVALS_LEFT = 3;

describe('MRWidget approvals summary', () => {
  const originalUserId = gon.current_user_id;
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(ApprovalsSummary, {
      propsData: {
        approved: false,
        approvers: testApprovers(),
        approvalsLeft: TEST_APPROVALS_LEFT,
        rulesLeft: testRulesLeft(),
        ...props,
      },
    });
  };

  const findAvatars = () => wrapper.find(UserAvatarList);

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
    gon.current_user_id = originalUserId;
  });

  describe('when approved', () => {
    beforeEach(() => {
      createComponent({
        approved: true,
      });
    });

    it('shows approved message', () => {
      expect(wrapper.text()).toContain(APPROVED_BY_OTHERS);
    });

    it('renders avatar list for approvers', () => {
      const avatars = findAvatars();

      expect(avatars.exists()).toBe(true);
      expect(avatars.props()).toEqual(
        expect.objectContaining({
          items: testApprovers(),
        }),
      );
    });

    describe('by the current user', () => {
      beforeEach(() => {
        gon.current_user_id = exampleUserId;
        createComponent({
          approvers: [{ id: exampleUserId }],
          approved: true,
        });
      });

      it('shows "Approved by you" message', () => {
        expect(wrapper.text()).toContain(APPROVED_BY_YOU);
      });
    });

    describe('by the current user and others', () => {
      beforeEach(() => {
        gon.current_user_id = exampleUserId;
        createComponent({
          approvers: [{ id: exampleUserId }, { id: exampleUserId + 1 }],
          approved: true,
        });
      });

      it('shows "Approved by you and others" message', () => {
        expect(wrapper.text()).toContain(APPROVED_BY_YOU_AND_OTHERS);
      });
    });

    describe('by other users than the current user', () => {
      beforeEach(() => {
        gon.current_user_id = exampleUserId;
        createComponent({
          approvers: [{ id: exampleUserId + 1 }],
          approved: true,
        });
      });

      it('shows "Approved by others" message', () => {
        expect(wrapper.text()).toContain(APPROVED_BY_OTHERS);
      });
    });
  });

  describe('when not approved', () => {
    beforeEach(() => {
      createComponent();
    });

    it('render message', () => {
      const names = toNounSeriesText(testRulesLeft());

      expect(wrapper.text()).toContain(`Requires ${TEST_APPROVALS_LEFT} approvals from ${names}.`);
    });
  });

  describe('when no rulesLeft', () => {
    beforeEach(() => {
      createComponent({
        rulesLeft: [],
      });
    });

    it('renders message', () => {
      expect(wrapper.text()).toContain(
        `Requires ${TEST_APPROVALS_LEFT} approvals from eligible users`,
      );
    });
  });

  describe('when no approvers', () => {
    beforeEach(() => {
      createComponent({
        approvers: [],
      });
    });

    it('does not render avatar list', () => {
      expect(wrapper.find(UserAvatarList).exists()).toBe(false);
    });
  });
});