Skip to content

Commit 6a883d7

Browse files
committed
feat: added unit tests
1 parent 1ee8e5b commit 6a883d7

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

Diff for: tests/unit/TinyPagination.spec.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { shallowMount } from '@vue/test-utils';
2+
import TinyPagination from '../../packages/TinyPagination/src/main.vue';
3+
4+
const vm = shallowMount(TinyPagination, {
5+
propsData: {
6+
total: 100,
7+
},
8+
});
9+
10+
// Helper function to create a component
11+
const createComponent = propsData => shallowMount(TinyPagination, { propsData });
12+
13+
describe('TinyPagination.vue', () => {
14+
let cmp;
15+
it('has a created hook', () => {
16+
expect(typeof TinyPagination.created).toBe('function');
17+
});
18+
19+
it('should match the snapshot', () => {
20+
expect(vm.$el).toMatchSnapshot();
21+
});
22+
23+
describe('Properties', () => {
24+
it('when the component is created without page prop, Page 1 is the page by default', () => {
25+
cmp = createComponent({ total: 300 });
26+
expect(cmp.vm.page).toBe(1);
27+
});
28+
29+
it('when the page property is set, the currentPage is equals', () => {
30+
cmp = createComponent({ total: 300, page: 2 });
31+
expect(cmp.vm.currentPage).toBe(2);
32+
});
33+
34+
it('when the component is created, English is the language by default', () => {
35+
cmp = createComponent({ total: 300 });
36+
expect(cmp.vm.lang).toBe('en');
37+
expect(cmp.vm.translation.title).toBe('Page');
38+
});
39+
40+
it('when the lang prop is set to spanish, the component is translated', () => {
41+
cmp = createComponent({ total: 300, lang: 'es' });
42+
expect(cmp.vm.lang).toBe('es');
43+
expect(cmp.vm.translation.title).toBe('Página');
44+
});
45+
46+
it('when the lang prop is set to not available language, English is the language by default', () => {
47+
cmp = createComponent({ total: 300, lang: 'fr' });
48+
expect(cmp.vm.translation.title).toBe('Page');
49+
});
50+
51+
it('when the show limit is not set, true is by default', () => {
52+
cmp = createComponent({ total: 100 });
53+
expect(cmp.vm.showLimit).toBe(true);
54+
});
55+
56+
it('when the showLimit is set, prop is same', () => {
57+
const showLimit = true;
58+
cmp = createComponent({ total: 100, showLimit });
59+
expect(cmp.vm.showLimit).toBe(showLimit);
60+
});
61+
});
62+
63+
describe('Watchers', () => {
64+
it('currentPage watcher is called with the new value', () => {
65+
cmp = createComponent({ total: 100 });
66+
cmp.setData({ currentPage: 3 });
67+
expect(cmp.emitted()['tiny:change-page']).toBeTruthy();
68+
});
69+
70+
it('currentLimit watcher is called with the new value', () => {
71+
cmp = createComponent({ total: 200 });
72+
cmp.setData({ currentLimit: 20 });
73+
expect(cmp.emitted()['tiny:change-limit']).toBeTruthy();
74+
});
75+
76+
it('when the currentPage watcher is called, the tiny:change-page event is emitted', () => {
77+
const stub = jest.fn();
78+
cmp = createComponent({ total: 100 });
79+
cmp.vm.$on('tiny:change-page', stub);
80+
81+
cmp.setData({ currentPage: 3 });
82+
expect(stub).toBeCalledWith({ page: 3 });
83+
});
84+
85+
it('when the currentLimit watcher is called, the tiny:change-limit event is emitted', () => {
86+
const stub = jest.fn();
87+
cmp = createComponent({ total: 100 });
88+
cmp.vm.$on('tiny:change-limit', stub);
89+
90+
cmp.setData({ currentLimit: 20 });
91+
expect(stub).toBeCalledWith({ limit: 20 });
92+
});
93+
});
94+
95+
describe('Events', () => {
96+
beforeEach(() => {
97+
cmp = createComponent({ total: 20 });
98+
});
99+
100+
it('calls nextPage when click on next button', () => {
101+
cmp.vm.nextPage = jest.fn();
102+
103+
const el = cmp.find('.btn-next-page').trigger('click');
104+
expect(cmp.vm.nextPage).toBeCalled();
105+
});
106+
107+
it('call lastPage when click on prev button', () => {
108+
cmp.vm.lastPage = jest.fn();
109+
110+
const el = cmp.find('.btn-prev-page').trigger('click');
111+
expect(cmp.vm.lastPage).toBeCalled();
112+
});
113+
});
114+
});

Diff for: tests/unit/__snapshots__/TinyPagination.spec.js.snap

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`TinyPagination.vue should match the snapshot 1`] = `undefined`;

0 commit comments

Comments
 (0)