|
1 |
| -import Vue from 'vue'; |
2 | 1 | import { mount } from '@vue/test-utils';
|
3 | 2 | import { compileToFunctions } from 'vue-template-compiler';
|
4 | 3 | import TextHighlight from 'vue-text-highlighter/index';
|
@@ -128,4 +127,70 @@ describe('<text-higlight>', () => {
|
128 | 127 | expect(nodes[1].textContent).toEqual('text');
|
129 | 128 | expect(nodes[2].textContent).toEqual('ault');
|
130 | 129 | });
|
| 130 | + |
| 131 | + test('should render with given custom component', () => { |
| 132 | + const text = 'using custom component'; |
| 133 | + const queries = 'om'; |
| 134 | + const CustomComponent = compileToFunctions(` |
| 135 | + <mark class="foo"> |
| 136 | + <slot></slot> |
| 137 | + </mark> |
| 138 | + `); |
| 139 | + |
| 140 | + const wrapper = mount(TextHighlight, { |
| 141 | + propsData: { |
| 142 | + queries, |
| 143 | + highlightComponent: CustomComponent, |
| 144 | + }, |
| 145 | + slots: { |
| 146 | + default: text, |
| 147 | + }, |
| 148 | + }); |
| 149 | + |
| 150 | + const nodes = wrapper.vm.$el.querySelectorAll('.foo'); |
| 151 | + |
| 152 | + expect(nodes[0].textContent).toEqual('om'); |
| 153 | + expect(nodes[1].textContent).toEqual('om'); |
| 154 | + }); |
| 155 | + |
| 156 | + test('should forward non-listed props and listeners to custom component', () => { |
| 157 | + const text = 'forward custom component'; |
| 158 | + const queries = ['ward', 'comp']; |
| 159 | + const CustomComponent = compileToFunctions(` |
| 160 | + <button :class="foo" @click="$emit('bar', 'baz')"> |
| 161 | + <slot></slot> |
| 162 | + </button> |
| 163 | + `); |
| 164 | + CustomComponent.props = { |
| 165 | + index: Number, |
| 166 | + text: String, |
| 167 | + foo: String, |
| 168 | + }; |
| 169 | + |
| 170 | + const fn = jest.fn(); |
| 171 | + |
| 172 | + const wrapper = mount(TextHighlight, { |
| 173 | + propsData: { |
| 174 | + queries, |
| 175 | + highlightComponent: CustomComponent, |
| 176 | + }, |
| 177 | + attrs: { |
| 178 | + foo: 'foofoo', |
| 179 | + }, |
| 180 | + listeners: { |
| 181 | + bar: fn, |
| 182 | + }, |
| 183 | + slots: { |
| 184 | + default: text, |
| 185 | + }, |
| 186 | + }); |
| 187 | + |
| 188 | + const button = wrapper.find('button'); |
| 189 | + expect(button.classes().includes('foofoo')).toBe(true); |
| 190 | + expect(button.text()).toEqual('ward'); |
| 191 | + |
| 192 | + button.trigger('click'); |
| 193 | + expect(fn).toHaveBeenCalledTimes(1); |
| 194 | + expect(fn).toHaveBeenCalledWith('baz'); |
| 195 | + }); |
131 | 196 | });
|
0 commit comments