|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import ComplexTable from './widget'; |
| 3 | +import { nextTick } from 'vue' |
| 4 | + |
| 5 | + |
| 6 | +describe('ComplexTable widget', () => { |
| 7 | + let result; |
| 8 | + |
| 9 | + |
| 10 | + describe('computed', () => { |
| 11 | + describe('#totalPages', () => { |
| 12 | + it('returns the total amount of pages', () => { |
| 13 | + const wrapper = mount(ComplexTable, { |
| 14 | + propsData: { |
| 15 | + totalItems: 35, |
| 16 | + items: ['hh'], |
| 17 | + headers: ['jj'], |
| 18 | + }, |
| 19 | + }) |
| 20 | + |
| 21 | + result = wrapper.vm.totalPages; |
| 22 | + |
| 23 | + expect(result).toEqual(4); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('#previousButtonDisabled', () => { |
| 28 | + it('returns true if the current page is 1', () => { |
| 29 | + const wrapper = mount(ComplexTable, { |
| 30 | + propsData: { |
| 31 | + totalItems: 35, |
| 32 | + items: ['hh'], |
| 33 | + headers: ['jj'], |
| 34 | + currentPage: 1, |
| 35 | + }, |
| 36 | + }) |
| 37 | + |
| 38 | + result = wrapper.vm.previousButtonDisabled; |
| 39 | + |
| 40 | + expect(result).toEqual(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns false if the current page is not 1', () => { |
| 44 | + const wrapper = mount(ComplexTable, { |
| 45 | + propsData: { |
| 46 | + totalItems: 35, |
| 47 | + items: ['hh'], |
| 48 | + headers: ['jj'], |
| 49 | + currentPage: 2, |
| 50 | + }, |
| 51 | + }) |
| 52 | + |
| 53 | + result = wrapper.vm.previousButtonDisabled; |
| 54 | + |
| 55 | + expect(result).toEqual(false); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('#nextButtonDisabled', () => { |
| 60 | + it('returns true if the current page is the last one', () => { |
| 61 | + const wrapper = mount(ComplexTable, { |
| 62 | + propsData: { |
| 63 | + totalItems: 25, |
| 64 | + items: ['hh'], |
| 65 | + headers: ['jj'], |
| 66 | + currentPage: 3, |
| 67 | + }, |
| 68 | + }) |
| 69 | + |
| 70 | + result = wrapper.vm.nextButtonDisabled; |
| 71 | + |
| 72 | + expect(result).toEqual(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('returns false if the current page is not the last one', () => { |
| 76 | + const wrapper = mount(ComplexTable, { |
| 77 | + propsData: { |
| 78 | + totalItems: 35, |
| 79 | + items: ['hh'], |
| 80 | + headers: ['jj'], |
| 81 | + currentPage: 2, |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + result = wrapper.vm.nextButtonDisabled; |
| 86 | + |
| 87 | + expect(result).toEqual(false); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('methods', () => { |
| 93 | + describe('#previousClicked', () => { |
| 94 | + it('emits previousClicked event', async () => { |
| 95 | + const wrapper = mount(ComplexTable, { |
| 96 | + propsData: { |
| 97 | + totalItems: 35, |
| 98 | + items: [], |
| 99 | + headers: ['jj'], |
| 100 | + currentPage: 2, |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + result = wrapper.vm.previousClicked(); |
| 105 | + |
| 106 | + expect(wrapper.emitted('previousClicked')).toBeTruthy() |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('#nextClicked', () => { |
| 111 | + it('emits nextClicked event', async () => { |
| 112 | + const wrapper = mount(ComplexTable, { |
| 113 | + propsData: { |
| 114 | + totalItems: 35, |
| 115 | + items: [], |
| 116 | + headers: ['jj'], |
| 117 | + currentPage: 2, |
| 118 | + }, |
| 119 | + }) |
| 120 | + |
| 121 | + result = wrapper.vm.nextClicked(); |
| 122 | + |
| 123 | + expect(wrapper.emitted('nextClicked')).toBeTruthy() |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('#prepareItems', () => { |
| 128 | + it('returns the first 10 items', () => { |
| 129 | + const itemsList = ['1','2','3','4','5','6','7','8','9','10','11','12']; |
| 130 | + const wrapper = mount(ComplexTable, { |
| 131 | + propsData: { |
| 132 | + totalItems: 35, |
| 133 | + items: itemsList, |
| 134 | + headers: ['jj'], |
| 135 | + currentPage: 2, |
| 136 | + }, |
| 137 | + }) |
| 138 | + |
| 139 | + result = wrapper.vm.prepareItems(itemsList); |
| 140 | + |
| 141 | + expect(result.length).toEqual(10); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('watch', () => { |
| 147 | + describe('props.items', () => { |
| 148 | + it('trigger the itemsLoaded event', async () => { |
| 149 | + const wrapper = mount(ComplexTable, { |
| 150 | + propsData: { |
| 151 | + totalItems: 35, |
| 152 | + items: [], |
| 153 | + headers: ['jj'], |
| 154 | + }, |
| 155 | + }) |
| 156 | + |
| 157 | + wrapper.setProps({ |
| 158 | + items: ['other'] |
| 159 | + }); |
| 160 | + |
| 161 | + await nextTick() |
| 162 | + |
| 163 | + expect(wrapper.emitted('itemsLoaded')) |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments