Commit b0ee9a50 authored by peterhegman's avatar peterhegman Committed by Peter Hegman

Fix infinite activities requests on profile

On a user's profile if you click the activities tab, scroll to the
bottom of the page, click the overview tab, and then scroll to the
bottom of the page there will be infinite API requests.

Changelog: fixed
parent 8492e153
...@@ -22,7 +22,10 @@ export default { ...@@ -22,7 +22,10 @@ export default {
this.prepareData = prepareData; this.prepareData = prepareData;
this.successCallback = successCallback; this.successCallback = successCallback;
this.errorCallback = errorCallback; this.errorCallback = errorCallback;
this.loading = $(`${container} .loading`).first(); this.$container = $(container);
this.$loading = this.$container.length
? this.$container.find('.loading').first()
: $('.loading').first();
if (preload) { if (preload) {
this.offset = 0; this.offset = 0;
this.getOld(); this.getOld();
...@@ -31,7 +34,7 @@ export default { ...@@ -31,7 +34,7 @@ export default {
}, },
getOld() { getOld() {
this.loading.show(); this.$loading.show();
const url = $('.content_list').data('href') || removeParams(['limit', 'offset']); const url = $('.content_list').data('href') || removeParams(['limit', 'offset']);
axios axios
...@@ -49,11 +52,11 @@ export default { ...@@ -49,11 +52,11 @@ export default {
if (!this.disable && !this.isScrollable()) { if (!this.disable && !this.isScrollable()) {
this.getOld(); this.getOld();
} else { } else {
this.loading.hide(); this.$loading.hide();
} }
}) })
.catch((err) => this.errorCallback(err)) .catch((err) => this.errorCallback(err))
.finally(() => this.loading.hide()); .finally(() => this.$loading.hide());
}, },
append(count, html) { append(count, html) {
...@@ -83,8 +86,12 @@ export default { ...@@ -83,8 +86,12 @@ export default {
fireOnce: true, fireOnce: true,
ceaseFire: () => this.disable === true, ceaseFire: () => this.disable === true,
callback: () => { callback: () => {
if (!this.loading.is(':visible')) { if (this.$container.length && !this.$container.is(':visible')) {
this.loading.show(); return;
}
if (!this.$loading.is(':visible')) {
this.$loading.show();
this.getOld(); this.getOld();
} }
}, },
......
...@@ -68,34 +68,34 @@ describe('pager', () => { ...@@ -68,34 +68,34 @@ describe('pager', () => {
it('shows loader while loading next page', async () => { it('shows loader while loading next page', async () => {
mockSuccess(); mockSuccess();
jest.spyOn(Pager.loading, 'show').mockImplementation(() => {}); jest.spyOn(Pager.$loading, 'show').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
await waitForPromises(); await waitForPromises();
expect(Pager.loading.show).toHaveBeenCalled(); expect(Pager.$loading.show).toHaveBeenCalled();
}); });
it('hides loader on success', async () => { it('hides loader on success', async () => {
mockSuccess(); mockSuccess();
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {}); jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
await waitForPromises(); await waitForPromises();
expect(Pager.loading.hide).toHaveBeenCalled(); expect(Pager.$loading.hide).toHaveBeenCalled();
}); });
it('hides loader on error', async () => { it('hides loader on error', async () => {
mockError(); mockError();
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {}); jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
await waitForPromises(); await waitForPromises();
expect(Pager.loading.hide).toHaveBeenCalled(); expect(Pager.$loading.hide).toHaveBeenCalled();
}); });
it('sends request to url with offset and limit params', async () => { it('sends request to url with offset and limit params', async () => {
...@@ -122,12 +122,12 @@ describe('pager', () => { ...@@ -122,12 +122,12 @@ describe('pager', () => {
Pager.limit = 20; Pager.limit = 20;
mockSuccess(1); mockSuccess(1);
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {}); jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
await waitForPromises(); await waitForPromises();
expect(Pager.loading.hide).toHaveBeenCalled(); expect(Pager.$loading.hide).toHaveBeenCalled();
expect(Pager.disable).toBe(true); expect(Pager.disable).toBe(true);
}); });
...@@ -175,5 +175,46 @@ describe('pager', () => { ...@@ -175,5 +175,46 @@ describe('pager', () => {
expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object)); expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object));
}); });
}); });
describe('when `container` is passed', () => {
const href = '/some_list';
const container = '#js-pager';
let endlessScrollCallback;
beforeEach(() => {
jest.spyOn(axios, 'get');
jest.spyOn($.fn, 'endlessScroll').mockImplementation(({ callback }) => {
endlessScrollCallback = callback;
});
});
describe('when `container` is visible', () => {
it('makes API request', () => {
setFixtures(
`<div id="js-pager"><div class="content_list" data-href="${href}"></div></div>`,
);
Pager.init({ container });
endlessScrollCallback();
expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object));
});
});
describe('when `container` is not visible', () => {
it('does not make API request', () => {
setFixtures(
`<div id="js-pager" style="display: none;"><div class="content_list" data-href="${href}"></div></div>`,
);
Pager.init({ container });
endlessScrollCallback();
expect(axios.get).not.toHaveBeenCalled();
});
});
});
}); });
}); });
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