Skip to content

Commit 63d3581

Browse files
committed
fixed note polling not sending updated last fetched at date
added spec for polling
1 parent 98e31bf commit 63d3581

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

app/assets/javascripts/notes/services/notes_service.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export default {
2727
return Vue.http[method](endpoint);
2828
},
2929
poll(data = {}) {
30-
const { endpoint, lastFetchedAt } = data;
30+
const endpoint = data.notesData.notesPath;
31+
const lastFetchedAt = data.lastFetchedAt;
3132
const options = {
3233
headers: {
33-
'X-Last-Fetched-At': `${lastFetchedAt}`,
34+
'X-Last-Fetched-At': lastFetchedAt ? `${lastFetchedAt}` : undefined,
3435
},
3536
};
3637

app/assets/javascripts/notes/stores/actions.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,10 @@ const pollSuccessCallBack = (resp, commit, state, getters) => {
203203
};
204204

205205
export const poll = ({ commit, state, getters }) => {
206-
const requestData = { endpoint: state.notesData.notesPath, lastFetchedAt: state.lastFetchedAt };
207-
208206
eTagPoll = new Poll({
209207
resource: service,
210208
method: 'poll',
211-
data: requestData,
209+
data: state,
212210
successCallback: resp => resp.json()
213211
.then(data => pollSuccessCallBack(data, commit, state, getters)),
214212
errorCallback: () => Flash('Something went wrong while fetching latest comments.'),
@@ -217,7 +215,7 @@ export const poll = ({ commit, state, getters }) => {
217215
if (!Visibility.hidden()) {
218216
eTagPoll.makeRequest();
219217
} else {
220-
service.poll(requestData);
218+
service.poll(state);
221219
}
222220

223221
Visibility.change(() => {

spec/javascripts/notes/mock_data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
export const notesDataMock = {
33
discussionsPath: '/gitlab-org/gitlab-ce/issues/26/discussions.json',
4-
lastFetchedAt: '1501862675',
4+
lastFetchedAt: 1501862675,
55
markdownDocsPath: '/help/user/markdown',
66
newSessionPath: '/users/sign_in?redirect_to_referer=yes',
77
notesPath: '/gitlab-org/gitlab-ce/noteable/issue/98/notes',

spec/javascripts/notes/stores/actions_spec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from 'vue';
22
import _ from 'underscore';
3+
import { headersInterceptor } from 'spec/helpers/vue_resource_helper';
34
import * as actions from '~/notes/stores/actions';
45
import store from '~/notes/stores';
56
import testAction from '../../helpers/vuex_action_helper';
@@ -129,4 +130,68 @@ describe('Actions Notes Store', () => {
129130
], done);
130131
});
131132
});
133+
134+
describe('poll', () => {
135+
beforeEach((done) => {
136+
jasmine.clock().install();
137+
138+
spyOn(Vue.http, 'get').and.callThrough();
139+
140+
store.dispatch('setNotesData', notesDataMock)
141+
.then(done)
142+
.catch(done.fail);
143+
});
144+
145+
afterEach(() => {
146+
jasmine.clock().uninstall();
147+
});
148+
149+
it('calls service with last fetched state', (done) => {
150+
const interceptor = (request, next) => {
151+
next(request.respondWith(JSON.stringify({
152+
notes: [],
153+
last_fetched_at: '123456',
154+
}), {
155+
status: 200,
156+
headers: {
157+
'poll-interval': '1000',
158+
},
159+
}));
160+
};
161+
162+
Vue.http.interceptors.push(interceptor);
163+
Vue.http.interceptors.push(headersInterceptor);
164+
165+
store.dispatch('poll')
166+
.then(() => new Promise(resolve => requestAnimationFrame(resolve)))
167+
.then(() => {
168+
expect(Vue.http.get).toHaveBeenCalledWith(jasmine.anything(), {
169+
url: jasmine.anything(),
170+
method: 'get',
171+
headers: {
172+
'X-Last-Fetched-At': undefined,
173+
},
174+
});
175+
expect(store.state.lastFetchedAt).toBe('123456');
176+
177+
jasmine.clock().tick(1500);
178+
})
179+
.then(() => new Promise((resolve) => {
180+
requestAnimationFrame(resolve);
181+
}))
182+
.then(() => {
183+
expect(Vue.http.get.calls.count()).toBe(2);
184+
expect(Vue.http.get.calls.mostRecent().args[1].headers).toEqual({
185+
'X-Last-Fetched-At': '123456',
186+
});
187+
})
188+
.then(() => store.dispatch('stopPolling'))
189+
.then(() => {
190+
Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
191+
Vue.http.interceptors = _.without(Vue.http.interceptors, headersInterceptor);
192+
})
193+
.then(done)
194+
.catch(done.fail);
195+
});
196+
});
132197
});

0 commit comments

Comments
 (0)