Commit 192b7553 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch...

Merge branch '212811-adding-new-task-always-shows-error-something-went-wrong-while-fetching-latest-comments' into 'master'

Fix polling for notes

See merge request gitlab-org/gitlab!36043
parents 6db6da7f 510a5fe1
...@@ -402,9 +402,8 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -402,9 +402,8 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
}; };
const pollSuccessCallBack = (resp, commit, state, getters, dispatch) => { const pollSuccessCallBack = (resp, commit, state, getters, dispatch) => {
if (resp.notes && resp.notes.length) { if (resp.notes?.length) {
updateOrCreateNotes({ commit, state, getters, dispatch }, resp.notes); dispatch('updateOrCreateNotes', resp.notes);
dispatch('startTaskList'); dispatch('startTaskList');
} }
...@@ -424,12 +423,12 @@ const getFetchDataParams = state => { ...@@ -424,12 +423,12 @@ const getFetchDataParams = state => {
return { endpoint, options }; return { endpoint, options };
}; };
export const fetchData = ({ commit, state, getters }) => { export const fetchData = ({ commit, state, getters, dispatch }) => {
const { endpoint, options } = getFetchDataParams(state); const { endpoint, options } = getFetchDataParams(state);
axios axios
.get(endpoint, options) .get(endpoint, options)
.then(({ data }) => pollSuccessCallBack(data, commit, state, getters)) .then(({ data }) => pollSuccessCallBack(data, commit, state, getters, dispatch))
.catch(() => Flash(__('Something went wrong while fetching latest comments.'))); .catch(() => Flash(__('Something went wrong while fetching latest comments.')));
}; };
...@@ -449,7 +448,7 @@ export const poll = ({ commit, state, getters, dispatch }) => { ...@@ -449,7 +448,7 @@ export const poll = ({ commit, state, getters, dispatch }) => {
if (!Visibility.hidden()) { if (!Visibility.hidden()) {
eTagPoll.makeRequest(); eTagPoll.makeRequest();
} else { } else {
fetchData({ commit, state, getters }); dispatch('fetchData');
} }
Visibility.change(() => { Visibility.change(() => {
......
---
title: Fix comment loading error in issues and merge requests
merge_request: 36043
author:
type: fixed
...@@ -274,9 +274,54 @@ describe('Actions Notes Store', () => { ...@@ -274,9 +274,54 @@ describe('Actions Notes Store', () => {
}); });
}); });
describe('fetchData', () => {
describe('given there are no notes', () => {
const lastFetchedAt = '13579';
beforeEach(() => {
axiosMock
.onGet(notesDataMock.notesPath)
.replyOnce(200, { notes: [], last_fetched_at: lastFetchedAt });
});
it('should commit SET_LAST_FETCHED_AT', () =>
testAction(
actions.fetchData,
undefined,
{ notesData: notesDataMock },
[{ type: 'SET_LAST_FETCHED_AT', payload: lastFetchedAt }],
[],
));
});
describe('given there are notes', () => {
const lastFetchedAt = '12358';
beforeEach(() => {
axiosMock
.onGet(notesDataMock.notesPath)
.replyOnce(200, { notes: discussionMock.notes, last_fetched_at: lastFetchedAt });
});
it('should dispatch updateOrCreateNotes, startTaskList and commit SET_LAST_FETCHED_AT', () =>
testAction(
actions.fetchData,
undefined,
{ notesData: notesDataMock },
[{ type: 'SET_LAST_FETCHED_AT', payload: lastFetchedAt }],
[
{ type: 'updateOrCreateNotes', payload: discussionMock.notes },
{ type: 'startTaskList' },
],
));
});
});
describe('poll', () => { describe('poll', () => {
beforeEach(done => { beforeEach(done => {
jest.spyOn(axios, 'get'); axiosMock
.onGet(notesDataMock.notesPath)
.reply(200, { notes: [], last_fetched_at: '123456' }, { 'poll-interval': '1000' });
store store
.dispatch('setNotesData', notesDataMock) .dispatch('setNotesData', notesDataMock)
...@@ -285,15 +330,10 @@ describe('Actions Notes Store', () => { ...@@ -285,15 +330,10 @@ describe('Actions Notes Store', () => {
}); });
it('calls service with last fetched state', done => { it('calls service with last fetched state', done => {
axiosMock
.onAny()
.reply(200, { notes: [], last_fetched_at: '123456' }, { 'poll-interval': '1000' });
store store
.dispatch('poll') .dispatch('poll')
.then(() => new Promise(resolve => requestAnimationFrame(resolve))) .then(() => new Promise(resolve => requestAnimationFrame(resolve)))
.then(() => { .then(() => {
expect(axios.get).toHaveBeenCalled();
expect(store.state.lastFetchedAt).toBe('123456'); expect(store.state.lastFetchedAt).toBe('123456');
jest.advanceTimersByTime(1500); jest.advanceTimersByTime(1500);
...@@ -305,8 +345,9 @@ describe('Actions Notes Store', () => { ...@@ -305,8 +345,9 @@ describe('Actions Notes Store', () => {
}), }),
) )
.then(() => { .then(() => {
expect(axios.get.mock.calls.length).toBe(2); const expectedGetRequests = 2;
expect(axios.get.mock.calls[axios.get.mock.calls.length - 1][1].headers).toEqual({ expect(axiosMock.history.get.length).toBe(expectedGetRequests);
expect(axiosMock.history.get[expectedGetRequests - 1].headers).toMatchObject({
'X-Last-Fetched-At': '123456', 'X-Last-Fetched-At': '123456',
}); });
}) })
......
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