UNPKG

4.01 kBJavaScriptView Raw
1import { shallowMount } from '@vue/test-utils';
2import Intersperse from './intersperse.vue';
3
4describe('Intersperse Component', () => {
5 let wrapper;
6
7 const createComponent = (defaultSlot = [], options = {}) => {
8 wrapper = shallowMount(Intersperse, {
9 slots: {
10 default: defaultSlot,
11 },
12 ...options,
13 });
14 };
15
16 it.each`
17 defaultSlotContent | selectorsToCheck
18 ${'<button>Foo</button>'} | ${['button']}
19 ${'<a><span>Foo</span></a><small>Bar</small>'} | ${['a', 'a span', 'small']}
20 `(
21 'contains all elements passed into the default slot',
22 ({ defaultSlotContent, selectorsToCheck }) => {
23 createComponent(defaultSlotContent);
24
25 selectorsToCheck.forEach((selector) => {
26 expect(wrapper.find(selector).exists()).toBe(true);
27 });
28 }
29 );
30
31 it('renders an empty span if no children are given', () => {
32 createComponent([]);
33
34 expect(wrapper.find('span').exists()).toBe(true);
35 expect(wrapper.findAll('span > *').length).toBe(0);
36 });
37
38 it('uses ", " as a default separator and lastSeparator', () => {
39 const defaultSlotContent = '<i>Foo</i><i>Bar</i><i>Baz</i>';
40 const expectedText = 'Foo, Bar, Baz';
41
42 createComponent(defaultSlotContent);
43
44 expect(wrapper.text()).toBe(expectedText);
45 });
46
47 it('adds class attribute when present', () => {
48 const defaultSlotContent = '<i>Foo</i><i>Bar</i>';
49
50 createComponent(defaultSlotContent, { attrs: { class: 'text-secondary' } });
51
52 expect(wrapper.classes('text-secondary')).toBe(true);
53 });
54
55 it.each`
56 defaultSlotContent | separator | expectedText
57 ${'<i>Baz</i><i>Foo</i><i>Biz</i>'} | ${', '} | ${'Baz, Foo, Biz'}
58 ${'<i>Bar</i><i>Baz</i><i>Biz</i>'} | ${'+'} | ${'Bar+Baz+Biz'}
59 ${'<i>Bar</i><i>Baz</i><i>Biz</i>Quz'} | ${'-'} | ${'Bar-Baz-Biz-Quz'}
60 `(
61 'intersperses each direct child with a given separator',
62 ({ defaultSlotContent, separator, expectedText }) => {
63 createComponent(defaultSlotContent, { propsData: { separator } });
64
65 expect(wrapper.text()).toBe(expectedText);
66 }
67 );
68
69 it.each`
70 defaultSlotContent | separator | lastSeparator | expectedText
71 ${'<i>Foo</i><i>Bar</i>'} | ${','} | ${' and '} | ${'Foo and Bar'}
72 ${'<i>Foo</i><i>Bar</i><i>Baz</i>'} | ${','} | ${'+'} | ${'Foo,Bar,+Baz'}
73 ${'<i>Foo</i><i>Bar</i><i>Baz</i>'} | ${','} | ${' and '} | ${'Foo,Bar, and Baz'}
74 ${'<i>Foo</i><i>Bar</i><i>Baz</i>'} | ${'+'} | ${' and '} | ${'Foo+Bar+ and Baz'}
75 `(
76 'adds a custom lastSeparator',
77 ({ defaultSlotContent, separator, lastSeparator, expectedText }) => {
78 createComponent(defaultSlotContent, { propsData: { separator, lastSeparator } });
79
80 expect(wrapper.text()).toBe(expectedText);
81 }
82 );
83
84 it.each`
85 defaultSlotContent | expectedText
86 ${'<i>Foo</i> <i>Bar</i>'} | ${'Foo, Bar'}
87 ${'<i>Foo</i>\n<i>Bar</i>'} | ${'Foo, Bar'}
88 ${'<i>Foo</i>\t<i>Bar</i>'} | ${'Foo, Bar'}
89 ${'<i>Foo</i> <i>Bar</i>'} | ${'Foo, Bar'}
90 ${'<i>Foo</i>&nbsp;<i>Bar</i>'} | ${'Foo, Bar'}
91 ${'<i>Foo</i><i>Bar</i> '} | ${'Foo, Bar'}
92 ${'<i>Foo</i><i>Bar</i>\n'} | ${'Foo, Bar'}
93 `(
94 'strips whitespace elements that are direct children of the slot',
95 ({ defaultSlotContent, expectedText }) => {
96 createComponent(defaultSlotContent);
97
98 expect(wrapper.text()).toBe(expectedText);
99 }
100 );
101
102 it.each`
103 defaultSlotContent | expectedText
104 ${'<i>Foo Bar</i><i>Baz</i>'} | ${'Foo Bar, Baz'}
105 ${'<i>Foo Bar</i><i>Baz Qaz</i><small>Qiz</small>'} | ${'Foo Bar, Baz Qaz, Qiz'}
106 `(
107 'preserves whitespace elements that are not direct children of the slot',
108 ({ defaultSlotContent, expectedText }) => {
109 createComponent(defaultSlotContent);
110
111 expect(wrapper.text()).toBe(expectedText);
112 }
113 );
114});