|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import TailwindPagination from '@/TailwindPagination.vue'; |
| 3 | + |
| 4 | +var exampleData = { |
| 5 | + current_page: 1, |
| 6 | + data: [ |
| 7 | + { id: 1 }, |
| 8 | + { id: 2 }, |
| 9 | + { id: 3 }, |
| 10 | + { id: 4 }, |
| 11 | + { id: 5 }, |
| 12 | + { id: 6 }, |
| 13 | + { id: 7 }, |
| 14 | + { id: 8 }, |
| 15 | + { id: 9 }, |
| 16 | + { id: 10 }, |
| 17 | + { id: 11 }, |
| 18 | + ], |
| 19 | + from: 1, |
| 20 | + last_page: 6, |
| 21 | + next_page_url: 'http://example.com/page/2', |
| 22 | + per_page: 2, |
| 23 | + prev_page_url: null, |
| 24 | + to: 2, |
| 25 | + total: 11, |
| 26 | +}; |
| 27 | + |
| 28 | +var exampleResourceData = { |
| 29 | + data: [ |
| 30 | + { id: 1 }, |
| 31 | + { id: 2 }, |
| 32 | + { id: 3 }, |
| 33 | + { id: 4 }, |
| 34 | + { id: 5 }, |
| 35 | + { id: 6 }, |
| 36 | + { id: 7 }, |
| 37 | + { id: 8 }, |
| 38 | + { id: 9 }, |
| 39 | + { id: 10 }, |
| 40 | + { id: 11 }, |
| 41 | + ], |
| 42 | + links: { |
| 43 | + first: 'http://example.com/page/1', |
| 44 | + last: 'http://example.com/page/6', |
| 45 | + prev: null, |
| 46 | + next: 'http://example.com/page/2', |
| 47 | + }, |
| 48 | + meta: { |
| 49 | + current_page: 1, |
| 50 | + from: 1, |
| 51 | + last_page: 6, |
| 52 | + path: 'http://example.com/page', |
| 53 | + per_page: 2, |
| 54 | + to: 2, |
| 55 | + total: 11, |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +test('has correct DOM structure', function () { |
| 60 | + const wrapper = mount(TailwindPagination, { |
| 61 | + props: { |
| 62 | + data: exampleData, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(wrapper.find('nav').exists()).toBe(true); |
| 67 | + expect(wrapper.findAll('button').length).toBe(8); |
| 68 | +}); |
| 69 | + |
| 70 | +test('has correct DOM structure with -1 limit on page 2', function () { |
| 71 | + exampleData.current_page = 2; |
| 72 | + exampleData.next_page_url = 'http://example.com/page/3'; |
| 73 | + exampleData.prev_page_url = 'http://example.com/page/1'; |
| 74 | + |
| 75 | + const wrapper = mount(TailwindPagination, { |
| 76 | + props: { |
| 77 | + data: exampleData, |
| 78 | + limit: -1, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(wrapper.find('nav').exists()).toBe(true); |
| 83 | + expect(wrapper.findAll('button').length).toBe(2); |
| 84 | +}); |
| 85 | + |
| 86 | +test('has correct DOM structure with 1 link limit on page 5', function () { |
| 87 | + exampleData.current_page = 5; |
| 88 | + exampleData.last_page = 11; |
| 89 | + exampleData.per_page = 1; |
| 90 | + exampleData.next_page_url = 'http://example.com/page/6'; |
| 91 | + exampleData.prev_page_url = 'http://example.com/page/4'; |
| 92 | + |
| 93 | + const wrapper = mount(TailwindPagination, { |
| 94 | + props: { |
| 95 | + data: exampleData, |
| 96 | + limit: 1, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(wrapper.find('nav').exists()).toBe(true); |
| 101 | + expect(wrapper.findAll('button').length).toBe(9); |
| 102 | +}); |
| 103 | + |
| 104 | +test('has correct DOM structure when on page 2', function () { |
| 105 | + exampleData.current_page = 2; |
| 106 | + exampleData.last_page = 6; |
| 107 | + exampleData.per_page = 2; |
| 108 | + exampleData.next_page_url = 'http://example.com/page/3'; |
| 109 | + exampleData.prev_page_url = 'http://example.com/page/1'; |
| 110 | + |
| 111 | + const wrapper = mount(TailwindPagination, { |
| 112 | + props: { |
| 113 | + data: exampleData, |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(wrapper.findAll('button').length).toBe(8); |
| 118 | +}); |
| 119 | + |
| 120 | +test('emits correct event', function () { |
| 121 | + const wrapper = mount(TailwindPagination, { |
| 122 | + props: { |
| 123 | + data: exampleData, |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + wrapper.findAll('button').at(2).trigger('click'); |
| 128 | + |
| 129 | + const event = wrapper.emitted('pagination-change-page'); |
| 130 | + expect(event).toHaveLength(1); |
| 131 | + expect(event[0]).toEqual([2]); |
| 132 | +}); |
| 133 | + |
| 134 | +test('has correct DOM structure when using slots', function () { |
| 135 | + const wrapper = mount(TailwindPagination, { |
| 136 | + props: { data: exampleData }, |
| 137 | + slots: { |
| 138 | + 'prev-nav': '<span class="custom-prev-nav">Previous</span>', |
| 139 | + 'next-nav': '<span>Next</span>', |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + expect(wrapper.html()).toContain( |
| 144 | + '<span class="custom-prev-nav">Previous</span>' |
| 145 | + ); |
| 146 | + expect(wrapper.html()).toContain('<span>Next</span>'); |
| 147 | +}); |
| 148 | + |
| 149 | +test('has correct DOM structure for Laravel API Resource responses', function () { |
| 150 | + const wrapper = mount(TailwindPagination, { |
| 151 | + props: { |
| 152 | + data: exampleResourceData, |
| 153 | + }, |
| 154 | + }); |
| 155 | + |
| 156 | + expect(wrapper.find('nav').exists()).toBe(true); |
| 157 | + expect(wrapper.findAll('button').length).toBe(8); |
| 158 | +}); |
0 commit comments