Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(pagination): ensure page never drops below first page
Browse files Browse the repository at this point in the history
Closes: #182
  • Loading branch information
rayrutjes committed Jul 19, 2017
1 parent 545d1e3 commit e851553
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/components/Pagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export default {
},
methods: {
goToPage(page) {
this.searchStore.page = Math.min(this.totalPages, page);
let p = page;
if (page <= 1) {
p = 1;
}
this.searchStore.page = Math.min(this.totalPages, p);
},
goToFirstPage() {
this.goToPage(1);
Expand Down
38 changes: 34 additions & 4 deletions src/components/__tests__/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import Vue from 'vue';
import { Pagination } from 'vue-instantsearch';

test('renders proper HTML', () => {
const goToPage = jest.fn();
const searchStore = {
page: 2,
totalPages: 10,
goToPage,
};
const Component = Vue.extend(Pagination);
const vm = new Component({
Expand All @@ -20,11 +18,9 @@ test('renders proper HTML', () => {
});

test('accepts custom padding', () => {
const goToPage = jest.fn();
const searchStore = {
page: 5,
totalPages: 10,
goToPage,
};
const Component = Vue.extend(Pagination);
const vm = new Component({
Expand All @@ -37,3 +33,37 @@ test('accepts custom padding', () => {

expect(vm.$el.outerHTML).toMatchSnapshot();
});

test('it should not try to go to a previous page that would be inferior to 1', () => {
const searchStore = {
page: 1,
totalPages: 20,
};
const Component = Vue.extend(Pagination);
const vm = new Component({
propsData: {
searchStore,
},
});

vm.goToPreviousPage();

expect(searchStore.page).toEqual(1);
});

test('it should not try to go to a next page that would be superior to total existing pages', () => {
const searchStore = {
page: 20,
totalPages: 20,
};
const Component = Vue.extend(Pagination);
const vm = new Component({
propsData: {
searchStore,
},
});

vm.goToNextPage();

expect(searchStore.page).toEqual(20);
});

0 comments on commit e851553

Please sign in to comment.