actions_spec.js 4.0 KB
Newer Older
1 2
import Vue from 'vue';
import _ from 'underscore';
F
Filipa Lacerda 已提交
3
import * as actions from '~/notes/stores/actions';
4
import store from '~/notes/stores';
5
import testAction from '../../helpers/vuex_action_helper';
6
import { resetStore } from '../helpers';
S
Simon Knox 已提交
7
import { discussionMock, notesDataMock, userDataMock, noteableDataMock, individualNote } from '../mock_data';
8

F
Filipa Lacerda 已提交
9
describe('Actions Notes Store', () => {
10 11 12 13
  afterEach(() => {
    resetStore(store);
  });

F
Filipa Lacerda 已提交
14
  describe('setNotesData', () => {
15 16 17 18
    it('should set received notes data', (done) => {
      testAction(actions.setNotesData, null, { notesData: {} }, [
        { type: 'SET_NOTES_DATA', payload: notesDataMock },
      ], done);
F
Filipa Lacerda 已提交
19 20 21
    });
  });

S
Simon Knox 已提交
22
  describe('setNoteableData', () => {
23
    it('should set received issue data', (done) => {
S
Simon Knox 已提交
24 25
      testAction(actions.setNoteableData, null, { noteableData: {} }, [
        { type: 'SET_NOTEABLE_DATA', payload: noteableDataMock },
26 27
      ], done);
    });
F
Filipa Lacerda 已提交
28 29 30
  });

  describe('setUserData', () => {
31 32 33 34 35
    it('should set received user data', (done) => {
      testAction(actions.setUserData, null, { userData: {} }, [
        { type: 'SET_USER_DATA', payload: userDataMock },
      ], done);
    });
F
Filipa Lacerda 已提交
36 37 38
  });

  describe('setLastFetchedAt', () => {
39 40 41 42 43
    it('should set received timestamp', (done) => {
      testAction(actions.setLastFetchedAt, null, { lastFetchedAt: {} }, [
        { type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' },
      ], done);
    });
F
Filipa Lacerda 已提交
44 45 46
  });

  describe('setInitialNotes', () => {
47 48
    it('should set initial notes', (done) => {
      testAction(actions.setInitialNotes, null, { notes: [] }, [
49
        { type: 'SET_INITIAL_NOTES', payload: [individualNote] },
50
      ], done);
F
Filipa Lacerda 已提交
51 52 53 54
    });
  });

  describe('setTargetNoteHash', () => {
55 56 57 58 59
    it('should set target note hash', (done) => {
      testAction(actions.setTargetNoteHash, null, { notes: [] }, [
        { type: 'SET_TARGET_NOTE_HASH', payload: 'hash' },
      ], done);
    });
F
Filipa Lacerda 已提交
60 61 62
  });

  describe('toggleDiscussion', () => {
63 64 65 66
    it('should toggle discussion', (done) => {
      testAction(actions.toggleDiscussion, null, { notes: [discussionMock] }, [
        { type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } },
      ], done);
F
Filipa Lacerda 已提交
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

  describe('async methods', () => {
    const interceptor = (request, next) => {
      next(request.respondWith(JSON.stringify({}), {
        status: 200,
      }));
    };

    beforeEach(() => {
      Vue.http.interceptors.push(interceptor);
    });

    afterEach(() => {
      Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
    });

    describe('closeIssue', () => {
      it('sets state as closed', (done) => {
        store.dispatch('closeIssue', { notesData: { closeIssuePath: '' } })
          .then(() => {
            expect(store.state.noteableData.state).toEqual('closed');
            done();
          })
          .catch(done.fail);
      });
    });

    describe('reopenIssue', () => {
      it('sets state as reopened', (done) => {
        store.dispatch('reopenIssue', { notesData: { reopenIssuePath: '' } })
          .then(() => {
            expect(store.state.noteableData.state).toEqual('reopened');
            done();
          })
          .catch(done.fail);
      });
    });
  });

  describe('emitStateChangedEvent', () => {
    it('emits an event on the document', () => {
      document.addEventListener('issuable_vue_app:change', (event) => {
        expect(event.detail.data).toEqual({ id: '1', state: 'closed' });
        expect(event.detail.isClosed).toEqual(true);
      });

      store.dispatch('emitStateChangedEvent', { id: '1', state: 'closed' });
    });
  });

  describe('toggleIssueLocalState', () => {
    it('sets issue state as closed', (done) => {
      testAction(actions.toggleIssueLocalState, 'closed', {}, [
        { type: 'CLOSE_ISSUE', payload: 'closed' },
      ], done);
    });

    it('sets issue state as reopened', (done) => {
      testAction(actions.toggleIssueLocalState, 'reopened', {}, [
        { type: 'REOPEN_ISSUE', payload: 'reopened' },
      ], done);
    });
  });
F
Filipa Lacerda 已提交
132
});