Commit 510a5fe1 authored by Mark Florian's avatar Mark Florian Committed by Kushal Pandya

Fix polling for notes

Previously, the `dispatch` store function wasn't passed to the
`pollSuccessCallback` helper, which was causing an error message to
appear when loading an MR. This ensures that is passed correctly along.

Addresses https://gitlab.com/gitlab-org/gitlab/-/issues/212811
parent 016bfb5e
...@@ -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