summaryrefslogtreecommitdiff
path: root/spec/frontend/static_site_editor/store/actions_spec.js
blob: 6b0b77f59b7ae7ee80597d0af33e18621fd573f3 (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
import testAction from 'helpers/vuex_action_helper';
import createState from '~/static_site_editor/store/state';
import * as actions from '~/static_site_editor/store/actions';
import * as mutationTypes from '~/static_site_editor/store/mutation_types';
import loadSourceContent from '~/static_site_editor/services/load_source_content';
import submitContentChanges from '~/static_site_editor/services/submit_content_changes';

import createFlash from '~/flash';

import {
  username,
  projectId,
  sourcePath,
  sourceContentTitle as title,
  sourceContent as content,
  savedContentMeta,
  submitChangesError,
} from '../mock_data';

jest.mock('~/flash');
jest.mock('~/static_site_editor/services/load_source_content', () => jest.fn());
jest.mock('~/static_site_editor/services/submit_content_changes', () => jest.fn());

describe('Static Site Editor Store actions', () => {
  let state;

  beforeEach(() => {
    state = createState({
      projectId,
      sourcePath,
    });
  });

  describe('loadContent', () => {
    describe('on success', () => {
      const payload = { title, content };

      beforeEach(() => {
        loadSourceContent.mockResolvedValueOnce(payload);
      });

      it('commits receiveContentSuccess', () => {
        testAction(
          actions.loadContent,
          null,
          state,
          [
            { type: mutationTypes.LOAD_CONTENT },
            { type: mutationTypes.RECEIVE_CONTENT_SUCCESS, payload },
          ],
          [],
        );

        expect(loadSourceContent).toHaveBeenCalledWith({ projectId, sourcePath });
      });
    });

    describe('on error', () => {
      const expectedMutations = [
        { type: mutationTypes.LOAD_CONTENT },
        { type: mutationTypes.RECEIVE_CONTENT_ERROR },
      ];

      beforeEach(() => {
        loadSourceContent.mockRejectedValueOnce();
      });

      it('commits receiveContentError', () => {
        testAction(actions.loadContent, null, state, expectedMutations);
      });

      it('displays flash communicating error', () => {
        return testAction(actions.loadContent, null, state, expectedMutations).then(() => {
          expect(createFlash).toHaveBeenCalledWith(
            'An error ocurred while loading your content. Please try again.',
          );
        });
      });
    });
  });

  describe('setContent', () => {
    it('commits setContent mutation', () => {
      testAction(actions.setContent, content, state, [
        {
          type: mutationTypes.SET_CONTENT,
          payload: content,
        },
      ]);
    });
  });

  describe('submitChanges', () => {
    describe('on success', () => {
      beforeEach(() => {
        state = createState({
          projectId,
          content,
          username,
          sourcePath,
        });
        submitContentChanges.mockResolvedValueOnce(savedContentMeta);
      });

      it('commits submitChangesSuccess mutation', () => {
        testAction(
          actions.submitChanges,
          null,
          state,
          [
            { type: mutationTypes.SUBMIT_CHANGES },
            { type: mutationTypes.SUBMIT_CHANGES_SUCCESS, payload: savedContentMeta },
          ],
          [],
        );

        expect(submitContentChanges).toHaveBeenCalledWith({
          username,
          projectId,
          content,
          sourcePath,
        });
      });
    });

    describe('on error', () => {
      const error = new Error(submitChangesError);
      const expectedMutations = [
        { type: mutationTypes.SUBMIT_CHANGES },
        { type: mutationTypes.SUBMIT_CHANGES_ERROR, payload: error.message },
      ];

      beforeEach(() => {
        submitContentChanges.mockRejectedValueOnce(error);
      });

      it('dispatches receiveContentError', () => {
        testAction(actions.submitChanges, null, state, expectedMutations);
      });
    });
  });

  describe('dismissSubmitChangesError', () => {
    it('commits dismissSubmitChangesError', () => {
      testAction(actions.dismissSubmitChangesError, null, state, [
        {
          type: mutationTypes.DISMISS_SUBMIT_CHANGES_ERROR,
        },
      ]);
    });
  });
});