summaryrefslogtreecommitdiff
path: root/spec/frontend/mr_notes/stores/actions_spec.js
blob: 568c1b930c99dc06a55ab4cde699a0101ffc4327 (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
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';

import { createStore } from '~/mr_notes/stores';

describe('MR Notes Mutator Actions', () => {
  let store;

  beforeEach(() => {
    store = createStore();
  });

  describe('setEndpoints', () => {
    it('sets endpoints', async () => {
      const endpoints = { endpointA: 'a' };

      await store.dispatch('setEndpoints', endpoints);

      expect(store.state.page.endpoints).toEqual(endpoints);
    });
  });

  describe('fetchMrMetadata', () => {
    const mrMetadata = { meta: true, data: 'foo' };
    const metadata = 'metadata';
    const endpoints = { metadata };
    let mock;

    beforeEach(async () => {
      await store.dispatch('setEndpoints', endpoints);

      mock = new MockAdapter(axios);

      mock.onGet(metadata).reply(200, mrMetadata);
    });

    afterEach(() => {
      mock.restore();
    });

    it('should fetch the data from the API', async () => {
      await store.dispatch('fetchMrMetadata');

      await axios.waitForAll();

      expect(mock.history.get).toHaveLength(1);
      expect(mock.history.get[0].url).toBe(metadata);
    });

    it('should set the fetched data into state', async () => {
      await store.dispatch('fetchMrMetadata');

      expect(store.state.page.mrMetadata).toEqual(mrMetadata);
    });

    it('should set failedToLoadMetadata flag when request fails', async () => {
      mock.onGet(metadata).reply(500);

      await store.dispatch('fetchMrMetadata');

      expect(store.state.page.failedToLoadMetadata).toBe(true);
    });
  });
});