|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { mount } from '@vue/test-utils'; |
| 3 | +import Vue from 'vue'; |
| 4 | +import AutoComplete from '@/components/forms/auto-complete/RuiAutoComplete.vue'; |
| 5 | +import { TeleportPlugin } from '@/components/overlays/teleport-container'; |
| 6 | + |
| 7 | +interface SelectOption { id: string; label: string } |
| 8 | + |
| 9 | +Vue.use(TeleportPlugin); |
| 10 | + |
| 11 | +function createWrapper(options?: any) { |
| 12 | + return mount(AutoComplete, options); |
| 13 | +} |
| 14 | + |
| 15 | +describe('autocomplete', () => { |
| 16 | + const options: SelectOption[] = [ |
| 17 | + { id: '1', label: 'Germany' }, |
| 18 | + { id: '2', label: 'Nigeria' }, |
| 19 | + { id: '3', label: 'Greece' }, |
| 20 | + { id: '4', label: 'Indonesia' }, |
| 21 | + { id: '5', label: 'Spain' }, |
| 22 | + { id: '6', label: 'India' }, |
| 23 | + { id: '7', label: 'France' }, |
| 24 | + { id: '8', label: 'England' }, |
| 25 | + ...[...new Array(50).keys()].map(index => ({ |
| 26 | + id: `${index + 9}`, |
| 27 | + label: `${index + 9}`, |
| 28 | + })), |
| 29 | + ]; |
| 30 | + |
| 31 | + it('renders properly', () => { |
| 32 | + const wrapper = createWrapper({ |
| 33 | + propsData: { |
| 34 | + keyAttr: 'id', |
| 35 | + options, |
| 36 | + textAttr: 'label', |
| 37 | + value: null, |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(wrapper.get('div[data-id="activator"]').classes()).toEqual( |
| 42 | + expect.arrayContaining([expect.stringMatching(/_activator_/)]), |
| 43 | + ); |
| 44 | + expect(wrapper.find('div[data-id="activator"] span[class*=label]').exists()).toBeTruthy(); |
| 45 | + expect(wrapper.find('span > svg').exists()).toBeTruthy(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('passes props correctly', async () => { |
| 49 | + const wrapper = createWrapper({ |
| 50 | + propsData: { |
| 51 | + disabled: true, |
| 52 | + keyAttr: 'id', |
| 53 | + options, |
| 54 | + textAttr: 'label', |
| 55 | + value: options[4].id, |
| 56 | + }, |
| 57 | + }); |
| 58 | + expect(wrapper.find('div[data-id=activator][tabindex=-1]').exists()).toBeTruthy(); |
| 59 | + expect(wrapper.find('div[data-id=activator][tabindex=-1]').text()).toMatch('Spain'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('works with primitive options', () => { |
| 63 | + const wrapper = createWrapper({ |
| 64 | + propsData: { |
| 65 | + keyAttr: 'id', |
| 66 | + options: options.map(item => item.label), |
| 67 | + textAttr: 'label', |
| 68 | + value: options[4].label, |
| 69 | + }, |
| 70 | + }); |
| 71 | + expect(wrapper.find('div[data-id=activator]').text()).toMatch('Spain'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('value passed and emitted properly', async () => { |
| 75 | + const wrapper = createWrapper({ |
| 76 | + propsData: { |
| 77 | + autoSelectFirst: true, |
| 78 | + keyAttr: 'id', |
| 79 | + options, |
| 80 | + textAttr: 'label', |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + // Open Menu Select |
| 85 | + await wrapper.find('[data-id=activator]').trigger('click'); |
| 86 | + await vi.delay(); |
| 87 | + await nextTick(); |
| 88 | + |
| 89 | + expect(document.body.querySelector('div[role=menu]')).toBeTruthy(); |
| 90 | + |
| 91 | + const selectedIndex = 4; |
| 92 | + let highlightedItemButton = document.body.querySelector(`button:first-child`) as HTMLButtonElement; |
| 93 | + expect(highlightedItemButton.classList).toContain('highlighted'); |
| 94 | + |
| 95 | + const buttonToSelect = document.body.querySelector(`button:nth-child(${selectedIndex})`) as HTMLButtonElement; |
| 96 | + buttonToSelect?.click(); |
| 97 | + expect(wrapper.emitted().input!.at(-1)![0]).toBe(selectedIndex.toString()); |
| 98 | + |
| 99 | + await vi.delay(); |
| 100 | + expect(document.body.querySelector('div[role=menu]')).toBeFalsy(); |
| 101 | + |
| 102 | + // Open Menu Select |
| 103 | + await wrapper.find('[data-id=activator]').trigger('click'); |
| 104 | + await vi.delay(); |
| 105 | + await nextTick(); |
| 106 | + |
| 107 | + expect(document.body.querySelector('div[role=menu]')).toBeTruthy(); |
| 108 | + |
| 109 | + await nextTick(); |
| 110 | + |
| 111 | + highlightedItemButton = document.body.querySelector(`button:nth-child(${selectedIndex})`) as HTMLButtonElement; |
| 112 | + expect(highlightedItemButton.classList).toContain('highlighted'); |
| 113 | + |
| 114 | + await wrapper.find('[data-id=activator]').trigger('keydown.down'); |
| 115 | + |
| 116 | + highlightedItemButton = document.body.querySelector(`button:nth-child(${selectedIndex + 1})`) as HTMLButtonElement; |
| 117 | + expect(highlightedItemButton.classList).toContain('highlighted'); |
| 118 | + |
| 119 | + await wrapper.find('[data-id=activator]').trigger('keydown.up'); |
| 120 | + await wrapper.find('[data-id=activator]').trigger('keydown.up'); |
| 121 | + |
| 122 | + const newSelectedIndex = selectedIndex - 1; |
| 123 | + |
| 124 | + highlightedItemButton = document.body.querySelector(`button:nth-child(${newSelectedIndex})`) as HTMLButtonElement; |
| 125 | + expect(highlightedItemButton.classList).toContain('highlighted'); |
| 126 | + |
| 127 | + await wrapper.find('[data-id=activator]').trigger('keydown.enter'); |
| 128 | + expect(wrapper.emitted().input!.at(-1)![0]).toBe(newSelectedIndex.toString()); |
| 129 | + }); |
| 130 | + |
| 131 | + it('multiple value', async () => { |
| 132 | + const wrapper = createWrapper({ |
| 133 | + propsData: { |
| 134 | + autoSelectFirst: true, |
| 135 | + keyAttr: 'id', |
| 136 | + options, |
| 137 | + textAttr: 'label', |
| 138 | + value: ['7', '8'], |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + expect(wrapper.find('div[data-id=activator]').exists()).toBeTruthy(); |
| 143 | + let chips = wrapper.find('div[data-id=activator]').findAll('.rui-chip'); |
| 144 | + expect(chips).toHaveLength(2); |
| 145 | + expect(chips.at(0).text()).toBe('France'); |
| 146 | + expect(chips.at(1).text()).toBe('England'); |
| 147 | + |
| 148 | + // Add India |
| 149 | + await wrapper.find('input').setValue('India'); |
| 150 | + await vi.delay(); |
| 151 | + |
| 152 | + expect(document.body.querySelectorAll('button').length).toBe(1); |
| 153 | + const itemButton = document.body.querySelector('button')!; |
| 154 | + expect(itemButton.innerHTML).toContain('India'); |
| 155 | + itemButton.click(); |
| 156 | + |
| 157 | + let newValue = ['7', '8', '6']; |
| 158 | + expect(wrapper.emitted().input!.at(-1)![0]).toEqual(newValue); |
| 159 | + |
| 160 | + await wrapper.setProps({ |
| 161 | + value: newValue, |
| 162 | + }); |
| 163 | + |
| 164 | + chips = wrapper.find('div[data-id=activator]').findAll('.rui-chip'); |
| 165 | + expect(chips).toHaveLength(3); |
| 166 | + expect(chips.at(0).text()).toBe('India'); |
| 167 | + expect(chips.at(1).text()).toBe('France'); |
| 168 | + expect(chips.at(2).text()).toBe('England'); |
| 169 | + |
| 170 | + // Delete England |
| 171 | + await chips.at(2).find('button[type="button"]').trigger('click'); |
| 172 | + await nextTick(); |
| 173 | + |
| 174 | + newValue = ['6', '7']; |
| 175 | + expect(wrapper.emitted().input!.at(-1)![0]).toEqual(newValue); |
| 176 | + }); |
| 177 | +}); |
0 commit comments