Commit b179b752 authored by Arun Kumar Mohan's avatar Arun Kumar Mohan

Refactor nextUnresolvedDiscussionId and previousUnresolvedDiscussionId getters

parent 6480bd6d
...@@ -171,26 +171,33 @@ export const isLastUnresolvedDiscussion = (state, getters) => (discussionId, dif ...@@ -171,26 +171,33 @@ export const isLastUnresolvedDiscussion = (state, getters) => (discussionId, dif
return lastDiscussionId === discussionId; return lastDiscussionId === discussionId;
}; };
// Gets the ID of the discussion following the one provided, respecting order (diff or date) export const findUnresolvedDiscussionIdNeighbor = (state, getters) => ({
// @param {Boolean} discussionId - id of the current discussion discussionId,
// @param {Boolean} diffOrder - is ordered by diff? diffOrder,
export const nextUnresolvedDiscussionId = (state, getters) => (discussionId, diffOrder) => { step,
const idsOrdered = getters.unresolvedDiscussionsIdsOrdered(diffOrder); }) => {
const currentIndex = idsOrdered.indexOf(discussionId); const ids = getters.unresolvedDiscussionsIdsOrdered(diffOrder);
const slicedIds = idsOrdered.slice(currentIndex + 1, currentIndex + 2); const index = ids.indexOf(discussionId) + step;
if (index < 0 && step < 0) {
return ids[ids.length - 1];
}
if (index === ids.length && step > 0) {
return ids[0];
}
// Get the first ID if there is none after the currentIndex return ids[index];
return slicedIds.length ? idsOrdered.slice(currentIndex + 1, currentIndex + 2)[0] : idsOrdered[0];
}; };
export const previousUnresolvedDiscussionId = (state, getters) => (discussionId, diffOrder) => { // Gets the ID of the discussion following the one provided, respecting order (diff or date)
const idsOrdered = getters.unresolvedDiscussionsIdsOrdered(diffOrder); // @param {Boolean} discussionId - id of the current discussion
const currentIndex = idsOrdered.indexOf(discussionId); // @param {Boolean} diffOrder - is ordered by diff?
const slicedIds = idsOrdered.slice(currentIndex - 1, currentIndex); export const nextUnresolvedDiscussionId = (state, getters) => (discussionId, diffOrder) =>
getters.findUnresolvedDiscussionIdNeighbor({ discussionId, diffOrder, step: 1 });
// Get the last ID if there is none after the currentIndex export const previousUnresolvedDiscussionId = (state, getters) => (discussionId, diffOrder) =>
return slicedIds.length ? slicedIds[0] : idsOrdered[idsOrdered.length - 1]; getters.findUnresolvedDiscussionIdNeighbor({ discussionId, diffOrder, step: -1 });
};
// @param {Boolean} diffOrder - is ordered by diff? // @param {Boolean} diffOrder - is ordered by diff?
export const firstUnresolvedDiscussionId = (state, getters) => diffOrder => { export const firstUnresolvedDiscussionId = (state, getters) => diffOrder => {
......
...@@ -14,6 +14,13 @@ import { ...@@ -14,6 +14,13 @@ import {
const discussionWithTwoUnresolvedNotes = 'merge_requests/resolved_diff_discussion.json'; const discussionWithTwoUnresolvedNotes = 'merge_requests/resolved_diff_discussion.json';
// Helper function to ensure that we're using the same schema across tests.
const createDiscussionNeighborParams = (discussionId, diffOrder, step) => ({
discussionId,
diffOrder,
step,
});
describe('Getters Notes Store', () => { describe('Getters Notes Store', () => {
let state; let state;
...@@ -25,7 +32,6 @@ describe('Getters Notes Store', () => { ...@@ -25,7 +32,6 @@ describe('Getters Notes Store', () => {
targetNoteHash: 'hash', targetNoteHash: 'hash',
lastFetchedAt: 'timestamp', lastFetchedAt: 'timestamp',
isNotesFetched: false, isNotesFetched: false,
notesData: notesDataMock, notesData: notesDataMock,
userData: userDataMock, userData: userDataMock,
noteableData: noteableDataMock, noteableData: noteableDataMock,
...@@ -244,62 +250,104 @@ describe('Getters Notes Store', () => { ...@@ -244,62 +250,104 @@ describe('Getters Notes Store', () => {
}); });
}); });
describe('nextUnresolvedDiscussionId', () => { describe('findUnresolvedDiscussionIdNeighbor', () => {
const localGetters = { let localGetters;
unresolvedDiscussionsIdsOrdered: () => ['123', '456', '789'], beforeEach(() => {
}; localGetters = {
unresolvedDiscussionsIdsOrdered: () => ['123', '456', '789'],
};
});
it('should return the ID of the discussion after the ID provided', () => { [
expect(getters.nextUnresolvedDiscussionId(state, localGetters)('123')).toBe('456'); { step: 1, id: '123', expected: '456' },
expect(getters.nextUnresolvedDiscussionId(state, localGetters)('456')).toBe('789'); { step: 1, id: '456', expected: '789' },
expect(getters.nextUnresolvedDiscussionId(state, localGetters)('789')).toBe('123'); { step: 1, id: '789', expected: '123' },
{ step: -1, id: '123', expected: '789' },
{ step: -1, id: '456', expected: '123' },
{ step: -1, id: '789', expected: '456' },
].forEach(({ step, id, expected }) => {
it(`with step ${step} and id ${id}, returns next value`, () => {
const params = createDiscussionNeighborParams(id, true, step);
expect(getters.findUnresolvedDiscussionIdNeighbor(state, localGetters)(params)).toBe(
expected,
);
});
}); });
});
describe('previousUnresolvedDiscussionId', () => { describe('with 1 unresolved discussion', () => {
describe('with unresolved discussions', () => { beforeEach(() => {
const localGetters = { localGetters = {
unresolvedDiscussionsIdsOrdered: () => ['123', '456', '789'], unresolvedDiscussionsIdsOrdered: () => ['123'],
}; };
});
[{ step: 1, id: '123', expected: '123' }, { step: -1, id: '123', expected: '123' }].forEach(
({ step, id, expected }) => {
it(`with step ${step} and match, returns only value`, () => {
const params = createDiscussionNeighborParams(id, true, step);
it('with bogus returns falsey', () => { expect(getters.findUnresolvedDiscussionIdNeighbor(state, localGetters)(params)).toBe(
expect(getters.previousUnresolvedDiscussionId(state, localGetters)('bogus')).toBe('456'); expected,
);
});
},
);
it('with no match, returns only value', () => {
const params = createDiscussionNeighborParams('bogus', true, 1);
expect(getters.findUnresolvedDiscussionIdNeighbor(state, localGetters)(params)).toBe('123');
}); });
});
[ describe('with 0 unresolved discussions', () => {
{ id: '123', expected: '789' }, beforeEach(() => {
{ id: '456', expected: '123' }, localGetters = {
{ id: '789', expected: '456' }, unresolvedDiscussionsIdsOrdered: () => [],
].forEach(({ id, expected }) => { };
it(`with ${id}, returns previous value`, () => { });
expect(getters.previousUnresolvedDiscussionId(state, localGetters)(id)).toBe(expected);
[{ step: 1 }, { step: -1 }].forEach(({ step }) => {
it(`with step ${step}, returns undefined`, () => {
const params = createDiscussionNeighborParams('bogus', true, step);
expect(
getters.findUnresolvedDiscussionIdNeighbor(state, localGetters)(params),
).toBeUndefined();
}); });
}); });
}); });
});
describe('with 1 unresolved discussion', () => { describe('findUnresolvedDiscussionIdNeighbor aliases', () => {
const localGetters = { let neighbor;
unresolvedDiscussionsIdsOrdered: () => ['123'], let findUnresolvedDiscussionIdNeighbor;
}; let localGetters;
it('with bogus returns id', () => { beforeEach(() => {
expect(getters.previousUnresolvedDiscussionId(state, localGetters)('bogus')).toBe('123'); neighbor = {};
}); findUnresolvedDiscussionIdNeighbor = jasmine.createSpy().and.returnValue(neighbor);
localGetters = { findUnresolvedDiscussionIdNeighbor };
});
it('with match, returns value', () => { describe('nextUnresolvedDiscussionId', () => {
expect(getters.previousUnresolvedDiscussionId(state, localGetters)('123')).toEqual('123'); it('should return result of find neighbor', () => {
const expectedParams = createDiscussionNeighborParams('123', true, 1);
const result = getters.nextUnresolvedDiscussionId(state, localGetters)('123', true);
expect(findUnresolvedDiscussionIdNeighbor).toHaveBeenCalledWith(expectedParams);
expect(result).toBe(neighbor);
}); });
}); });
describe('with 0 unresolved discussions', () => { describe('previosuUnresolvedDiscussionId', () => {
const localGetters = { it('should return result of find neighbor', () => {
unresolvedDiscussionsIdsOrdered: () => [], const expectedParams = createDiscussionNeighborParams('123', true, -1);
}; const result = getters.previousUnresolvedDiscussionId(state, localGetters)('123', true);
it('returns undefined', () => { expect(findUnresolvedDiscussionIdNeighbor).toHaveBeenCalledWith(expectedParams);
expect( expect(result).toBe(neighbor);
getters.previousUnresolvedDiscussionId(state, localGetters)('bogus'),
).toBeUndefined();
}); });
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment