summaryrefslogtreecommitdiff
path: root/spec/frontend/batch_comments/stores/modules/batch_comments/mutations_spec.js
blob: 1406f66fd105d8cc478366015577c14edb2e13e0 (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
import createState from '~/batch_comments/stores/modules/batch_comments/state';
import mutations from '~/batch_comments/stores/modules/batch_comments/mutations';
import * as types from '~/batch_comments/stores/modules/batch_comments/mutation_types';

describe('Batch comments mutations', () => {
  let state;

  beforeEach(() => {
    state = createState();
  });

  describe(types.ADD_NEW_DRAFT, () => {
    it('adds processed object into drafts array', () => {
      const draft = { id: 1, note: 'test' };

      mutations[types.ADD_NEW_DRAFT](state, draft);

      expect(state.drafts).toEqual([
        {
          ...draft,
          isDraft: true,
        },
      ]);
    });
  });

  describe(types.DELETE_DRAFT, () => {
    it('removes draft from array by ID', () => {
      state.drafts.push({ id: 1 }, { id: 2 });

      mutations[types.DELETE_DRAFT](state, 1);

      expect(state.drafts).toEqual([{ id: 2 }]);
    });
  });

  describe(types.SET_BATCH_COMMENTS_DRAFTS, () => {
    it('adds to processed drafts in state', () => {
      const drafts = [{ id: 1 }, { id: 2 }];

      mutations[types.SET_BATCH_COMMENTS_DRAFTS](state, drafts);

      expect(state.drafts).toEqual([
        {
          id: 1,
          isDraft: true,
        },
        {
          id: 2,
          isDraft: true,
        },
      ]);
    });
  });

  describe(types.REQUEST_PUBLISH_REVIEW, () => {
    it('sets isPublishing to true', () => {
      mutations[types.REQUEST_PUBLISH_REVIEW](state);

      expect(state.isPublishing).toBe(true);
    });
  });

  describe(types.RECEIVE_PUBLISH_REVIEW_SUCCESS, () => {
    it('resets drafts', () => {
      state.drafts.push('test');

      mutations[types.RECEIVE_PUBLISH_REVIEW_SUCCESS](state);

      expect(state.drafts).toEqual([]);
    });

    it('sets isPublishing to false', () => {
      state.isPublishing = true;

      mutations[types.RECEIVE_PUBLISH_REVIEW_SUCCESS](state);

      expect(state.isPublishing).toBe(false);
    });
  });

  describe(types.RECEIVE_PUBLISH_REVIEW_ERROR, () => {
    it('updates isPublishing to false', () => {
      state.isPublishing = true;

      mutations[types.RECEIVE_PUBLISH_REVIEW_ERROR](state);

      expect(state.isPublishing).toBe(false);
    });
  });

  describe(types.RECEIVE_DRAFT_UPDATE_SUCCESS, () => {
    it('updates draft in store', () => {
      state.drafts.push({ id: 1 });

      mutations[types.RECEIVE_DRAFT_UPDATE_SUCCESS](state, { id: 1, note: 'test' });

      expect(state.drafts).toEqual([
        {
          id: 1,
          note: 'test',
          isDraft: true,
        },
      ]);
    });
  });
});