forked from bootstrap-vue-next/bootstrap-vue-next
/
table.spec.ts
200 lines (178 loc) · 6.35 KB
/
table.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {enableAutoUnmount, mount} from '@vue/test-utils'
import type {LiteralUnion, TableField, TableItem} from 'src/types'
import {afterEach, describe, expect, it} from 'vitest'
import BTable from './BTable.vue'
interface SimplePerson {
first_name: string
age: number
}
const simpleItems: TableItem<SimplePerson>[] = [
{age: 27, first_name: 'Havij'},
{age: 9, first_name: 'Cyndi'},
{age: 42, first_name: 'Robert'},
]
const simpleFields: Exclude<TableField<SimplePerson>, string>[] = [
{key: 'first_name', label: 'First Name', sortable: true},
{key: 'age', label: 'Age', sortable: true},
]
const formattedFields: Exclude<TableField<SimplePerson>, string>[] = [
{
key: 'is_adult',
label: 'Adult?',
sortable: true,
sortByFormatted: true,
formatter: (_value: unknown, _key?: LiteralUnion<keyof SimplePerson>, item?: SimplePerson) =>
item ? (item.age >= 18 ? 'Yes' : 'No') : 'Something went wrong',
},
{key: 'first_name', label: 'First Name', sortable: true},
{key: 'age', label: 'Age', sortable: true},
]
describe('tbody', () => {
enableAutoUnmount(afterEach)
it('has table class by default', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems},
})
expect(wrapper.get('table').classes()).toContain('table')
})
it('has b-table class by default', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems},
})
expect(wrapper.get('table').classes()).toContain('b-table')
})
it('creates #columns === #fields', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: formattedFields},
})
const heads = wrapper.get('table').findAll('th')
expect(heads.length).toBe(3)
})
it('uses label from fields', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields},
})
const heads = wrapper.get('table').findAll('th')
expect(heads[0].text()).toBe('First Name')
expect(heads[1].text()).toBe('Age')
})
it('shows sortable columns when sortable === true', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields},
})
const heads = wrapper.get('table').findAll('th')
expect(heads[0].classes()).toContain('b-table-sortable-column')
expect(heads[1].classes()).toContain('b-table-sortable-column')
})
it('does not show sortable columns when sortable undefined', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems},
})
const heads = wrapper.get('table').findAll('th')
expect(heads[0].classes()).not.toContain('b-table-sortable-column')
expect(heads[1].classes()).not.toContain('b-table-sortable-column')
})
it('has aria-sort="none" by default', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields},
})
const heads = wrapper.get('table').findAll('th')
expect(heads[0].attributes('aria-sort')).toBe('none')
})
it('has aria-sort="ascending" on first click', async () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields},
})
const [names] = wrapper.get('table').findAll('th')
await names.trigger('click')
expect(names.attributes('aria-sort')).toBe('ascending')
})
it('has aria-sort="descending" on second click', async () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields},
})
const [names] = wrapper.get('table').findAll('th')
await names.trigger('click')
await names.trigger('click')
expect(names.attributes('aria-sort')).toBe('descending')
})
it('sorts text ascending', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields, sortBy: 'first_name'},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.find('td').text())
expect(text).toStrictEqual(['Cyndi', 'Havij', 'Robert'])
})
it('sorts text descending', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields, sortBy: 'first_name', sortDesc: true},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.find('td').text())
expect(text).toStrictEqual(['Robert', 'Havij', 'Cyndi'])
})
it('sorts numbers ascending', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields, sortBy: 'age'},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.findAll('td')[1].text())
expect(text).toStrictEqual(['9', '27', '42'])
})
it('sorts numbers descending', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: simpleFields, sortBy: 'age', sortDesc: true},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.findAll('td')[1].text())
expect(text).toStrictEqual(['42', '27', '9'])
})
it('sorts by formatted when sortByFormatted === true && formatter exists', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: formattedFields, sortBy: 'is_adult'},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.find('td').text())
expect(text).toStrictEqual(['No', 'Yes', 'Yes'])
})
it('sorts by formatted when sortByFormatted === true && formatter exists and respects sorDesc', () => {
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: formattedFields, sortBy: 'is_adult', sortDesc: true},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.find('td').text())
expect(text).toStrictEqual(['Yes', 'Yes', 'No'])
})
it('sorts by custom formatter when sortByFormatted === function', () => {
const customFormatterFields: Exclude<TableField<SimplePerson>, string>[] = [
{
key: 'first_name',
label: 'First Name',
sortable: true,
sortByFormatted: (value: unknown) => (value as string).slice(1),
},
{key: 'age', label: 'Age', sortable: true},
]
const wrapper = mount(BTable, {
props: {items: simpleItems, fields: customFormatterFields, sortBy: 'first_name'},
})
const text = wrapper
.get('tbody')
.findAll('tr')
.map((row) => row.find('td').text())
expect(text).toStrictEqual(['Havij', 'Robert', 'Cyndi'])
})
})