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