Commit 2929db33 authored by Clement Ho's avatar Clement Ho

Merge branch 'migrate-table-pagination-specs' into 'master'

Migrate table_pagination specs to Jest & VTU

See merge request gitlab-org/gitlab!18698
parents a1c6a1d3 8fe04479
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import paginationComp from '~/vue_shared/components/pagination/table_pagination.vue'; import TablePagination from '~/vue_shared/components/pagination/table_pagination.vue';
describe('Pagination component', () => { describe('Pagination component', () => {
let component; let wrapper;
let PaginationComponent;
let spy; let spy;
let mountComponent;
beforeEach(() => { const mountComponent = props => {
spy = jasmine.createSpy('spy'); wrapper = shallowMount(TablePagination, {
PaginationComponent = Vue.extend(paginationComp); sync: false,
mountComponent = function(props) {
return new PaginationComponent({
propsData: props, propsData: props,
}).$mount(); });
}; };
const findFirstButtonLink = () => wrapper.find('.js-first-button .page-link');
const findPreviousButton = () => wrapper.find('.js-previous-button');
const findPreviousButtonLink = () => wrapper.find('.js-previous-button .page-link');
const findNextButton = () => wrapper.find('.js-next-button');
const findNextButtonLink = () => wrapper.find('.js-next-button .page-link');
const findLastButtonLink = () => wrapper.find('.js-last-button .page-link');
const findPages = () => wrapper.findAll('.page');
const findSeparator = () => wrapper.find('.separator');
beforeEach(() => {
spy = jest.fn();
});
afterEach(() => {
wrapper.destroy();
}); });
describe('render', () => { describe('render', () => {
it('should not render anything', () => { it('should not render anything', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: NaN, nextPage: NaN,
page: 1, page: 1,
...@@ -32,12 +43,12 @@ describe('Pagination component', () => { ...@@ -32,12 +43,12 @@ describe('Pagination component', () => {
change: spy, change: spy,
}); });
expect(component.$el.childNodes.length).toEqual(0); expect(wrapper.isEmpty()).toBe(true);
}); });
describe('prev button', () => { describe('prev button', () => {
it('should be disabled and non clickable', () => { it('should be disabled and non clickable', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 2, nextPage: 2,
page: 1, page: 1,
...@@ -49,17 +60,13 @@ describe('Pagination component', () => { ...@@ -49,17 +60,13 @@ describe('Pagination component', () => {
change: spy, change: spy,
}); });
expect( expect(findPreviousButton().classes()).toContain('disabled');
component.$el.querySelector('.js-previous-button').classList.contains('disabled'), findPreviousButtonLink().trigger('click');
).toEqual(true);
component.$el.querySelector('.js-previous-button .page-link').click();
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
}); });
it('should be disabled and non clickable when total and totalPages are NaN', () => { it('should be disabled and non clickable when total and totalPages are NaN', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 2, nextPage: 2,
page: 1, page: 1,
...@@ -70,18 +77,13 @@ describe('Pagination component', () => { ...@@ -70,18 +77,13 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(findPreviousButton().classes()).toContain('disabled');
expect( findPreviousButtonLink().trigger('click');
component.$el.querySelector('.js-previous-button').classList.contains('disabled'),
).toEqual(true);
component.$el.querySelector('.js-previous-button .page-link').click();
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
}); });
it('should be enabled and clickable', () => { it('should be enabled and clickable', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 3, nextPage: 3,
page: 2, page: 2,
...@@ -92,14 +94,12 @@ describe('Pagination component', () => { ...@@ -92,14 +94,12 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
findPreviousButtonLink().trigger('click');
component.$el.querySelector('.js-previous-button .page-link').click();
expect(spy).toHaveBeenCalledWith(1); expect(spy).toHaveBeenCalledWith(1);
}); });
it('should be enabled and clickable when total and totalPages are NaN', () => { it('should be enabled and clickable when total and totalPages are NaN', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 3, nextPage: 3,
page: 2, page: 2,
...@@ -110,16 +110,14 @@ describe('Pagination component', () => { ...@@ -110,16 +110,14 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
findPreviousButtonLink().trigger('click');
component.$el.querySelector('.js-previous-button .page-link').click();
expect(spy).toHaveBeenCalledWith(1); expect(spy).toHaveBeenCalledWith(1);
}); });
}); });
describe('first button', () => { describe('first button', () => {
it('should call the change callback with the first page', () => { it('should call the change callback with the first page', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 3, nextPage: 3,
page: 2, page: 2,
...@@ -130,18 +128,14 @@ describe('Pagination component', () => { ...@@ -130,18 +128,14 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
const button = findFirstButtonLink();
const button = component.$el.querySelector('.js-first-button .page-link'); expect(button.text().trim()).toEqual('« First');
button.trigger('click');
expect(button.textContent.trim()).toEqual('« First');
button.click();
expect(spy).toHaveBeenCalledWith(1); expect(spy).toHaveBeenCalledWith(1);
}); });
it('should call the change callback with the first page when total and totalPages are NaN', () => { it('should call the change callback with the first page when total and totalPages are NaN', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 3, nextPage: 3,
page: 2, page: 2,
...@@ -152,20 +146,16 @@ describe('Pagination component', () => { ...@@ -152,20 +146,16 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
const button = findFirstButtonLink();
const button = component.$el.querySelector('.js-first-button .page-link'); expect(button.text().trim()).toEqual('« First');
button.trigger('click');
expect(button.textContent.trim()).toEqual('« First');
button.click();
expect(spy).toHaveBeenCalledWith(1); expect(spy).toHaveBeenCalledWith(1);
}); });
}); });
describe('last button', () => { describe('last button', () => {
it('should call the change callback with the last page', () => { it('should call the change callback with the last page', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 3, nextPage: 3,
page: 2, page: 2,
...@@ -176,18 +166,14 @@ describe('Pagination component', () => { ...@@ -176,18 +166,14 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
const button = findLastButtonLink();
const button = component.$el.querySelector('.js-last-button .page-link'); expect(button.text().trim()).toEqual('Last »');
button.trigger('click');
expect(button.textContent.trim()).toEqual('Last »');
button.click();
expect(spy).toHaveBeenCalledWith(5); expect(spy).toHaveBeenCalledWith(5);
}); });
it('should not render', () => { it('should not render', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 3, nextPage: 3,
page: 2, page: 2,
...@@ -198,14 +184,13 @@ describe('Pagination component', () => { ...@@ -198,14 +184,13 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(findLastButtonLink().exists()).toBe(false);
expect(component.$el.querySelector('.js-last-button .page-link')).toBeNull();
}); });
}); });
describe('next button', () => { describe('next button', () => {
it('should be disabled and non clickable', () => { it('should be disabled and non clickable', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: NaN, nextPage: NaN,
page: 5, page: 5,
...@@ -216,16 +201,17 @@ describe('Pagination component', () => { ...@@ -216,16 +201,17 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(
expect(component.$el.querySelector('.js-next-button').textContent.trim()).toEqual('Next ›'); findNextButton()
.text()
component.$el.querySelector('.js-next-button .page-link').click(); .trim(),
).toEqual('Next ›');
findNextButtonLink().trigger('click');
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
}); });
it('should be disabled and non clickable when total and totalPages are NaN', () => { it('should be disabled and non clickable when total and totalPages are NaN', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: NaN, nextPage: NaN,
page: 5, page: 5,
...@@ -236,16 +222,17 @@ describe('Pagination component', () => { ...@@ -236,16 +222,17 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(
expect(component.$el.querySelector('.js-next-button').textContent.trim()).toEqual('Next ›'); findNextButton()
.text()
component.$el.querySelector('.js-next-button .page-link').click(); .trim(),
).toEqual('Next ›');
findNextButtonLink().trigger('click');
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
}); });
it('should be enabled and clickable', () => { it('should be enabled and clickable', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 4, nextPage: 4,
page: 3, page: 3,
...@@ -256,14 +243,12 @@ describe('Pagination component', () => { ...@@ -256,14 +243,12 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
findNextButtonLink().trigger('click');
component.$el.querySelector('.js-next-button .page-link').click();
expect(spy).toHaveBeenCalledWith(4); expect(spy).toHaveBeenCalledWith(4);
}); });
it('should be enabled and clickable when total and totalPages are NaN', () => { it('should be enabled and clickable when total and totalPages are NaN', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 4, nextPage: 4,
page: 3, page: 3,
...@@ -274,16 +259,14 @@ describe('Pagination component', () => { ...@@ -274,16 +259,14 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
findNextButtonLink().trigger('click');
component.$el.querySelector('.js-next-button .page-link').click();
expect(spy).toHaveBeenCalledWith(4); expect(spy).toHaveBeenCalledWith(4);
}); });
}); });
describe('numbered buttons', () => { describe('numbered buttons', () => {
it('should render 5 pages', () => { it('should render 5 pages', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 4, nextPage: 4,
page: 3, page: 3,
...@@ -294,12 +277,11 @@ describe('Pagination component', () => { ...@@ -294,12 +277,11 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(findPages().length).toEqual(5);
expect(component.$el.querySelectorAll('.page').length).toEqual(5);
}); });
it('should not render any page', () => { it('should not render any page', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 4, nextPage: 4,
page: 3, page: 3,
...@@ -310,14 +292,13 @@ describe('Pagination component', () => { ...@@ -310,14 +292,13 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(findPages().length).toEqual(0);
expect(component.$el.querySelectorAll('.page').length).toEqual(0);
}); });
}); });
describe('spread operator', () => { describe('spread operator', () => {
it('should render', () => { it('should render', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 4, nextPage: 4,
page: 3, page: 3,
...@@ -328,12 +309,15 @@ describe('Pagination component', () => { ...@@ -328,12 +309,15 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(
expect(component.$el.querySelector('.separator').textContent.trim()).toEqual('...'); findSeparator()
.text()
.trim(),
).toEqual('...');
}); });
it('should not render', () => { it('should not render', () => {
component = mountComponent({ mountComponent({
pageInfo: { pageInfo: {
nextPage: 4, nextPage: 4,
page: 3, page: 3,
...@@ -344,8 +328,7 @@ describe('Pagination component', () => { ...@@ -344,8 +328,7 @@ describe('Pagination component', () => {
}, },
change: spy, change: spy,
}); });
expect(findSeparator().exists()).toBe(false);
expect(component.$el.querySelector('.separator')).toBeNull();
}); });
}); });
}); });
......
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