Commit ff500c73 authored by Phil Hughes's avatar Phil Hughes

Merge branch 'nd/fix-error-when-calling-user-usage-endpoint' into 'master'

Call RedisHllUserEvent only when a user is set

See merge request gitlab-org/gitlab!62560
parents c04dd69f 95b44716
...@@ -866,7 +866,7 @@ const Api = { ...@@ -866,7 +866,7 @@ const Api = {
}, },
trackRedisHllUserEvent(event) { trackRedisHllUserEvent(event) {
if (!gon.features?.usageDataApi) { if (!gon.current_user_id || !gon.features?.usageDataApi) {
return null; return null;
} }
......
...@@ -1503,33 +1503,55 @@ describe('Api', () => { ...@@ -1503,33 +1503,55 @@ describe('Api', () => {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
describe('when usage data increment unique users is called with feature flag disabled', () => { describe('when user is set', () => {
beforeEach(() => { beforeEach(() => {
gon.features = { ...gon.features, usageDataApi: false }; window.gon.current_user_id = 1;
}); });
it('returns null', () => { describe('when usage data increment unique users is called with feature flag disabled', () => {
jest.spyOn(axios, 'post'); beforeEach(() => {
mock.onPost(expectedUrl).replyOnce(httpStatus.OK, true); gon.features = { ...gon.features, usageDataApi: false };
});
expect(axios.post).toHaveBeenCalledTimes(0); it('returns null and does not call the endpoint', () => {
expect(Api.trackRedisHllUserEvent(event)).toEqual(null); jest.spyOn(axios, 'post');
const result = Api.trackRedisHllUserEvent(event);
expect(result).toEqual(null);
expect(axios.post).toHaveBeenCalledTimes(0);
});
});
describe('when usage data increment unique users is called', () => {
beforeEach(() => {
gon.features = { ...gon.features, usageDataApi: true };
});
it('resolves the Promise', () => {
jest.spyOn(axios, 'post');
mock.onPost(expectedUrl, { event }).replyOnce(httpStatus.OK, true);
return Api.trackRedisHllUserEvent(event).then(({ data }) => {
expect(data).toEqual(true);
expect(axios.post).toHaveBeenCalledWith(expectedUrl, postData, { headers });
});
});
}); });
}); });
describe('when usage data increment unique users is called', () => { describe('when user is not set and feature flag enabled', () => {
beforeEach(() => { beforeEach(() => {
gon.features = { ...gon.features, usageDataApi: true }; gon.features = { ...gon.features, usageDataApi: true };
}); });
it('resolves the Promise', () => { it('returns null and does not call the endpoint', () => {
jest.spyOn(axios, 'post'); jest.spyOn(axios, 'post');
mock.onPost(expectedUrl, { event }).replyOnce(httpStatus.OK, true);
return Api.trackRedisHllUserEvent(event).then(({ data }) => { const result = Api.trackRedisHllUserEvent(event);
expect(data).toEqual(true);
expect(axios.post).toHaveBeenCalledWith(expectedUrl, postData, { headers }); expect(result).toEqual(null);
}); expect(axios.post).toHaveBeenCalledTimes(0);
}); });
}); });
}); });
......
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