|
1 |
| -import { loadFixture, testVM } from '../../../tests/utils' |
| 1 | +import ButtonToolbar from './button-toolbar' |
| 2 | +import ButtonGroup from '../button-group/button-group' |
| 3 | +import Button from '../button/button' |
| 4 | +import { mount } from '@vue/test-utils' |
| 5 | +import Vue from 'vue' |
2 | 6 |
|
3 | 7 | describe('button-toolbar', () => {
|
4 |
| - beforeEach(loadFixture(__dirname, 'button-toolbar')) |
5 |
| - testVM() |
| 8 | + it('toolbar root should be "div"', async () => { |
| 9 | + const wrapper = mount(ButtonToolbar, {}) |
| 10 | + expect(wrapper.is('div')).toBe(true) |
| 11 | + wrapper.destroy() |
| 12 | + }) |
6 | 13 |
|
7 | 14 | it('toolbar should contain base class', async () => {
|
8 |
| - const { |
9 |
| - app: { $refs } |
10 |
| - } = window |
| 15 | + const wrapper = mount(ButtonToolbar, {}) |
| 16 | + expect(wrapper.classes()).toContain('btn-toolbar') |
| 17 | + wrapper.destroy() |
| 18 | + }) |
11 | 19 |
|
12 |
| - expect($refs.toolbar).toHaveClass('btn-toolbar') |
| 20 | + it('toolbar should not have class "justify-content-between"', async () => { |
| 21 | + const wrapper = mount(ButtonToolbar, {}) |
| 22 | + expect(wrapper.classes()).not.toContain('justify-content-between') |
| 23 | + wrapper.destroy() |
13 | 24 | })
|
14 | 25 |
|
15 | 26 | it('toolbar should have role', async () => {
|
16 |
| - const { |
17 |
| - app: { $refs } |
18 |
| - } = window |
| 27 | + const wrapper = mount(ButtonToolbar, {}) |
| 28 | + expect(wrapper.attributes('role')).toBe('toolbar') |
| 29 | + wrapper.destroy() |
| 30 | + }) |
| 31 | + |
| 32 | + it('toolbar should not have tabindex by default', async () => { |
| 33 | + const wrapper = mount(ButtonToolbar, {}) |
| 34 | + expect(wrapper.attributes('tabindex')).not.toBeDefined() |
| 35 | + wrapper.destroy() |
| 36 | + }) |
| 37 | + |
| 38 | + it('toolbar should have class "justify-content-between" when justify set', async () => { |
| 39 | + const wrapper = mount(ButtonToolbar, { |
| 40 | + propsData: { |
| 41 | + justify: true |
| 42 | + } |
| 43 | + }) |
| 44 | + expect(wrapper.classes()).toContain('justify-content-between') |
| 45 | + expect(wrapper.classes()).toContain('btn-toolbar') |
| 46 | + wrapper.destroy() |
| 47 | + }) |
| 48 | + |
| 49 | + it('toolbar should have tabindex when key-nav set', async () => { |
| 50 | + const wrapper = mount(ButtonToolbar, { |
| 51 | + propsData: { |
| 52 | + keyNav: true |
| 53 | + } |
| 54 | + }) |
| 55 | + expect(wrapper.attributes('tabindex')).toBeDefined() |
| 56 | + expect(wrapper.attributes('tabindex')).toBe('0') |
| 57 | + expect(wrapper.element.tabIndex).toBe(0) |
| 58 | + wrapper.destroy() |
| 59 | + }) |
| 60 | + |
| 61 | + // These tests are wrapped in a new describe to limit the scope of the getBCR Mock |
| 62 | + describe('keyboard navigation', () => { |
| 63 | + const origGetBCR = Element.prototype.getBoundingClientRect |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + // Mock getBCR so that the isVisible(el) test returns true |
| 67 | + // In our test below, all pagination buttons would normally be visible |
| 68 | + Element.prototype.getBoundingClientRect = jest.fn(() => { |
| 69 | + return { |
| 70 | + width: 24, |
| 71 | + height: 24, |
| 72 | + top: 0, |
| 73 | + left: 0, |
| 74 | + bottom: 0, |
| 75 | + right: 0 |
| 76 | + } |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + afterEach(() => { |
| 81 | + // Restore prototype |
| 82 | + Element.prototype.getBoundingClientRect = origGetBCR |
| 83 | + }) |
| 84 | + |
| 85 | + // Test App for keynav |
| 86 | + const App = Vue.extend({ |
| 87 | + render(h) { |
| 88 | + return h(ButtonToolbar, { props: { keyNav: true } }, [ |
| 89 | + h(ButtonGroup, {}, [h(Button, {}, 'a'), h(Button, {}, 'b')]), |
| 90 | + h(ButtonGroup, {}, [h(Button, { props: { disabled: true } }, 'c'), h(Button, {}, 'd')]), |
| 91 | + h(ButtonGroup, {}, [h(Button, {}, 'e'), h(Button, {}, 'f')]) |
| 92 | + ]) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + it('has correct structure', async () => { |
| 97 | + const wrapper = mount(App, { |
| 98 | + attachToDocument: true |
| 99 | + }) |
| 100 | + |
| 101 | + await wrapper.vm.$nextTick() |
| 102 | + |
| 103 | + expect(wrapper.is('div.btn-toolbar')).toBe(true) |
| 104 | + expect(wrapper.attributes('tabindex')).toBe('0') |
| 105 | + |
| 106 | + const $groups = wrapper.findAll('.btn-group') |
| 107 | + expect($groups).toBeDefined() |
| 108 | + expect($groups.length).toBe(3) |
| 109 | + expect($groups.is(ButtonGroup)).toBe(true) |
| 110 | + |
| 111 | + const $btns = wrapper.findAll('button') |
| 112 | + expect($btns).toBeDefined() |
| 113 | + expect($btns.length).toBe(6) |
| 114 | + expect($btns.is(Button)).toBe(true) |
| 115 | + expect($btns.at(0).is('button[tabindex="-1"')).toBe(true) |
| 116 | + expect($btns.at(1).is('button[tabindex="-1"')).toBe(true) |
| 117 | + expect($btns.at(2).is('button[tabindex="-1"')).toBe(false) // disabled button |
| 118 | + expect($btns.at(3).is('button[tabindex="-1"')).toBe(true) |
| 119 | + expect($btns.at(4).is('button[tabindex="-1"')).toBe(true) |
| 120 | + expect($btns.at(5).is('button[tabindex="-1"')).toBe(true) |
| 121 | + |
| 122 | + wrapper.destroy() |
| 123 | + }) |
| 124 | + |
| 125 | + it('focuses first button when tabbed into', async () => { |
| 126 | + const wrapper = mount(App, { |
| 127 | + attachToDocument: true |
| 128 | + }) |
| 129 | + |
| 130 | + await wrapper.vm.$nextTick() |
| 131 | + |
| 132 | + expect(wrapper.is('div.btn-toolbar')).toBe(true) |
| 133 | + expect(wrapper.attributes('tabindex')).toBe('0') |
| 134 | + |
| 135 | + const $btns = wrapper.findAll('button') |
| 136 | + expect($btns).toBeDefined() |
| 137 | + expect($btns.length).toBe(6) |
| 138 | + |
| 139 | + expect(document.activeElement).not.toBe(wrapper.element) |
| 140 | + expect(document.activeElement).not.toBe($btns.at(0).element) |
| 141 | + |
| 142 | + wrapper.trigger('focusin') |
| 143 | + await wrapper.vm.$nextTick() |
| 144 | + expect(document.activeElement).toBe($btns.at(0).element) |
| 145 | + |
| 146 | + wrapper.destroy() |
| 147 | + }) |
| 148 | + |
| 149 | + it('keyboard navigation works', async () => { |
| 150 | + const wrapper = mount(App, { |
| 151 | + attachToDocument: true |
| 152 | + }) |
| 153 | + |
| 154 | + await wrapper.vm.$nextTick() |
| 155 | + |
| 156 | + expect(wrapper.is('div.btn-toolbar')).toBe(true) |
| 157 | + expect(wrapper.attributes('tabindex')).toBe('0') |
| 158 | + |
| 159 | + const $btns = wrapper.findAll('button') |
| 160 | + expect($btns).toBeDefined() |
| 161 | + expect($btns.length).toBe(6) |
| 162 | + |
| 163 | + // Focus first button |
| 164 | + $btns.at(0).element.focus() |
| 165 | + expect(document.activeElement).toBe($btns.at(0).element) |
| 166 | + |
| 167 | + // Cursor right |
| 168 | + $btns.at(0).trigger('keydown.right') |
| 169 | + await wrapper.vm.$nextTick() |
| 170 | + expect(document.activeElement).toBe($btns.at(1).element) |
| 171 | + |
| 172 | + // Cursor right (skips disabled button) |
| 173 | + $btns.at(1).trigger('keydown.right') |
| 174 | + await wrapper.vm.$nextTick() |
| 175 | + expect(document.activeElement).toBe($btns.at(3).element) |
| 176 | + |
| 177 | + // Cursor shift-right (focuses last button) |
| 178 | + $btns.at(1).trigger('keydown.right', { shiftKey: true }) |
| 179 | + await wrapper.vm.$nextTick() |
| 180 | + expect(document.activeElement).toBe($btns.at(5).element) |
| 181 | + |
| 182 | + // Cursor left |
| 183 | + $btns.at(5).trigger('keydown.left') |
| 184 | + await wrapper.vm.$nextTick() |
| 185 | + expect(document.activeElement).toBe($btns.at(4).element) |
| 186 | + |
| 187 | + // Cursor shift left (focuses first button) |
| 188 | + $btns.at(5).trigger('keydown.left', { shiftKey: true }) |
| 189 | + await wrapper.vm.$nextTick() |
| 190 | + expect(document.activeElement).toBe($btns.at(0).element) |
19 | 191 |
|
20 |
| - expect($refs.toolbar.$el.getAttribute('role')).toBe('toolbar') |
| 192 | + wrapper.destroy() |
| 193 | + }) |
21 | 194 | })
|
22 | 195 | })
|
0 commit comments