Skip to content

Commit a3d764a

Browse files
[Testing] update for custom component
1 parent 366960f commit a3d764a

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

src/components/index.vue

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,17 @@ export default {
3535
}) => (
3636
!isHighlighted
3737
? text
38-
: h(
39-
this.highlightComponent,
40-
{
41-
on: this.$listeners,
42-
class: ['text__highlight', this.highlightClass],
43-
style: this.highlightStyle,
44-
key: highlightIndex,
45-
props: {
46-
index: highlightIndex,
47-
text,
48-
...this.$attrs,
49-
},
38+
: h(this.highlightComponent, {
39+
on: this.$listeners,
40+
class: ['text__highlight', this.highlightClass],
41+
style: this.highlightStyle,
42+
key: highlightIndex,
43+
props: {
44+
index: highlightIndex,
45+
text,
46+
...this.$attrs,
5047
},
51-
text,
52-
)))}
48+
}, text)))}
5349
</span>;
5450
},
5551
computed: {

test/unit/specs/TextHighlight.spec.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Vue from 'vue';
21
import { mount } from '@vue/test-utils';
32
import { compileToFunctions } from 'vue-template-compiler';
43
import TextHighlight from 'vue-text-highlighter/index';
@@ -128,4 +127,70 @@ describe('<text-higlight>', () => {
128127
expect(nodes[1].textContent).toEqual('text');
129128
expect(nodes[2].textContent).toEqual('ault');
130129
});
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+
});
131196
});

0 commit comments

Comments
 (0)