gitlab-org--gitlab-foss/spec/javascripts/notes/stores/mutation_spec.js

220 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-08-11 14:54:26 -04:00
import mutations from '~/notes/stores/mutations';
import { note, discussionMock, notesDataMock, userDataMock, issueDataMock, individualNote } from '../mock_data';
2017-08-04 11:51:35 -04:00
describe('Mutation Notes Store', () => {
describe('ADD_NEW_NOTE', () => {
2017-09-21 21:04:52 -04:00
let state;
let noteData;
beforeEach(() => {
state = { notes: [] };
noteData = {
expanded: true,
id: note.discussion_id,
individual_note: true,
notes: [note],
reply_id: note.discussion_id,
};
2017-08-11 14:54:26 -04:00
mutations.ADD_NEW_NOTE(state, note);
2017-09-21 21:04:52 -04:00
});
2017-08-11 14:54:26 -04:00
2017-09-21 21:04:52 -04:00
it('should add a new note to an array of notes', () => {
2017-08-11 14:54:26 -04:00
expect(state).toEqual({
2017-09-21 21:04:52 -04:00
notes: [noteData],
2017-08-11 14:54:26 -04:00
});
2017-09-21 21:04:52 -04:00
expect(state.notes.length).toBe(1);
});
it('should not add the same note to the notes array', () => {
mutations.ADD_NEW_NOTE(state, note);
expect(state.notes.length).toBe(1);
2017-08-04 11:51:35 -04:00
});
});
describe('ADD_NEW_REPLY_TO_DISCUSSION', () => {
it('should add a reply to a specific discussion', () => {
2017-08-11 14:54:26 -04:00
const state = { notes: [discussionMock] };
const newReply = Object.assign({}, note, { discussion_id: discussionMock.id });
mutations.ADD_NEW_REPLY_TO_DISCUSSION(state, newReply);
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
expect(state.notes[0].notes.length).toEqual(4);
2017-08-04 11:51:35 -04:00
});
});
describe('DELETE_NOTE', () => {
2017-08-11 14:54:26 -04:00
it('should delete a note ', () => {
const state = { notes: [discussionMock] };
const toDelete = discussionMock.notes[0];
const lengthBefore = discussionMock.notes.length;
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.DELETE_NOTE(state, toDelete);
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
expect(state.notes[0].notes.length).toEqual(lengthBefore - 1);
2017-08-04 11:51:35 -04:00
});
});
describe('REMOVE_PLACEHOLDER_NOTES', () => {
it('should remove all placeholder notes in indivudal notes and discussion', () => {
2017-08-11 14:54:26 -04:00
const placeholderNote = Object.assign({}, individualNote, { isPlaceholderNote: true });
const state = { notes: [placeholderNote] };
mutations.REMOVE_PLACEHOLDER_NOTES(state);
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
expect(state.notes).toEqual([]);
2017-08-04 11:51:35 -04:00
});
});
describe('SET_NOTES_DATA', () => {
it('should set an object with notesData', () => {
2017-08-11 14:54:26 -04:00
const state = {
notesData: {},
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.SET_NOTES_DATA(state, notesDataMock);
expect(state.notesData).toEqual(notesDataMock);
2017-08-04 11:51:35 -04:00
});
});
describe('SET_ISSUE_DATA', () => {
it('should set the issue data', () => {
2017-08-11 14:54:26 -04:00
const state = {
issueData: {},
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.SET_ISSUE_DATA(state, issueDataMock);
expect(state.issueData).toEqual(issueDataMock);
2017-08-04 11:51:35 -04:00
});
});
describe('SET_USER_DATA', () => {
it('should set the user data', () => {
2017-08-11 14:54:26 -04:00
const state = {
userData: {},
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.SET_USER_DATA(state, userDataMock);
expect(state.userData).toEqual(userDataMock);
2017-08-04 11:51:35 -04:00
});
});
2017-08-11 14:54:26 -04:00
describe('SET_INITIAL_NOTES', () => {
2017-08-04 11:51:35 -04:00
it('should set the initial notes received', () => {
2017-08-11 14:54:26 -04:00
const state = {
notes: [],
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.SET_INITIAL_NOTES(state, [note]);
expect(state.notes).toEqual([note]);
2017-08-04 11:51:35 -04:00
});
});
describe('SET_LAST_FETCHED_AT', () => {
2017-08-11 14:54:26 -04:00
it('should set timestamp', () => {
const state = {
lastFetchedAt: [],
};
mutations.SET_LAST_FETCHED_AT(state, 'timestamp');
expect(state.lastFetchedAt).toEqual('timestamp');
});
2017-08-04 11:51:35 -04:00
});
describe('SET_TARGET_NOTE_HASH', () => {
2017-08-11 14:54:26 -04:00
it('should set the note hash', () => {
const state = {
targetNoteHash: [],
};
mutations.SET_TARGET_NOTE_HASH(state, 'hash');
expect(state.targetNoteHash).toEqual('hash');
});
2017-08-04 11:51:35 -04:00
});
describe('SHOW_PLACEHOLDER_NOTE', () => {
it('should set a placeholder note', () => {
2017-08-11 14:54:26 -04:00
const state = {
notes: [],
};
mutations.SHOW_PLACEHOLDER_NOTE(state, note);
expect(state.notes[0].isPlaceholderNote).toEqual(true);
2017-08-04 11:51:35 -04:00
});
});
describe('TOGGLE_AWARD', () => {
it('should add award if user has not reacted yet', () => {
2017-08-11 14:54:26 -04:00
const state = {
notes: [note],
userData: userDataMock,
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
const data = {
note,
awardName: 'cartwheel',
};
mutations.TOGGLE_AWARD(state, data);
const lastIndex = state.notes[0].award_emoji.length - 1;
expect(state.notes[0].award_emoji[lastIndex]).toEqual({
name: 'cartwheel',
user: { id: userDataMock.id, name: userDataMock.name, username: userDataMock.username },
});
2017-08-04 11:51:35 -04:00
});
it('should remove award if user already reacted', () => {
2017-08-11 14:54:26 -04:00
const state = {
notes: [note],
userData: {
id: 1,
name: 'Administrator',
username: 'root',
},
};
const data = {
note,
awardName: 'bath_tone3',
};
mutations.TOGGLE_AWARD(state, data);
expect(state.notes[0].award_emoji.length).toEqual(2);
2017-08-04 11:51:35 -04:00
});
});
describe('TOGGLE_DISCUSSION', () => {
it('should open a closed discussion', () => {
2017-08-11 14:54:26 -04:00
const discussion = Object.assign({}, discussionMock, { expanded: false });
const state = {
notes: [discussion],
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.TOGGLE_DISCUSSION(state, { discussionId: discussion.id });
expect(state.notes[0].expanded).toEqual(true);
2017-08-04 11:51:35 -04:00
});
it('should close a opened discussion', () => {
2017-08-11 14:54:26 -04:00
const state = {
notes: [discussionMock],
};
mutations.TOGGLE_DISCUSSION(state, { discussionId: discussionMock.id });
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
expect(state.notes[0].expanded).toEqual(false);
2017-08-04 11:51:35 -04:00
});
});
describe('UPDATE_NOTE', () => {
2017-08-11 14:54:26 -04:00
it('should update a note', () => {
const state = {
notes: [individualNote],
};
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
const updated = Object.assign({}, individualNote.notes[0], { note: 'Foo' });
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
mutations.UPDATE_NOTE(state, updated);
2017-08-04 11:51:35 -04:00
2017-08-11 14:54:26 -04:00
expect(state.notes[0].notes[0].note).toEqual('Foo');
2017-08-04 11:51:35 -04:00
});
});
});