pagination_spec.js 1.99 KB
Newer Older
1
import { GlPagination } from '@gitlab/ui';
2
import { shallowMount } from '@vue/test-utils';
3
import Pagination from 'ee/compliance_dashboard/components/shared/pagination.vue';
4
import setWindowLocation from 'helpers/set_window_location_helper';
5

6
describe('Pagination component', () => {
7
  let wrapper;
8
  const origin = 'https://localhost';
9

10
  const findGlPagination = () => wrapper.findComponent(GlPagination);
11
  const getLink = (query) => wrapper.find(query).element.getAttribute('href');
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  const findPrevPageLink = () => getLink('a.prev-page-item');
  const findNextPageLink = () => getLink('a.next-page-item');

  const createComponent = (isLastPage = false) => {
    return shallowMount(Pagination, {
      propsData: {
        isLastPage,
      },
      stubs: {
        GlPagination,
      },
    });
  };

  beforeEach(() => {
27
    setWindowLocation(origin);
28 29 30 31 32 33 34 35
  });

  afterEach(() => {
    wrapper.destroy();
  });

  describe('when initialized', () => {
    beforeEach(() => {
36
      setWindowLocation('?page=2');
37 38 39 40 41 42 43 44
      wrapper = createComponent();
    });

    it('should get the page number from the URL', () => {
      expect(findGlPagination().props().value).toBe(2);
    });

    it('should create a link to the previous page', () => {
45
      expect(findPrevPageLink()).toBe(`${origin}/?page=1`);
46 47 48
    });

    it('should create a link to the next page', () => {
49
      expect(findNextPageLink()).toBe(`${origin}/?page=3`);
50 51 52 53 54
    });
  });

  describe('when on last page', () => {
    beforeEach(() => {
55
      setWindowLocation('?page=2');
56 57 58 59 60 61 62 63 64 65
      wrapper = createComponent(true);
    });

    it('should not have a nextPage if on the last page', () => {
      expect(findGlPagination().props().nextPage).toBe(null);
    });
  });

  describe('when there is only one page', () => {
    beforeEach(() => {
66
      setWindowLocation('?page=1');
67 68 69 70 71 72 73 74
      wrapper = createComponent(true);
    });

    it('should not display if there is only one page of results', () => {
      expect(findGlPagination().exists()).toEqual(false);
    });
  });
});