|
1 | 1 | import VCodeBlock from '../VCodeBlock.vue';
|
| 2 | +import { h } from 'vue'; |
2 | 3 | import { describe, it, expect } from 'vitest';
|
3 | 4 | import { mount } from '@vue/test-utils';
|
4 | 5 | import { pluginName } from '../utils/globals';
|
5 |
| - |
| 6 | +import { codeBlockOptions } from '../'; |
6 | 7 |
|
7 | 8 | describe('VCodeBlock Component', () => {
|
8 | 9 |
|
9 |
| - it('should mount the component', () => { |
10 |
| - const wrapper = mount(VCodeBlock, { |
11 |
| - props: { |
12 |
| - prismjs: true, |
13 |
| - } |
| 10 | + // -------------------------------------------------- Component // |
| 11 | + describe('Component', () => { |
| 12 | + it('should mount the component', () => { |
| 13 | + const wrapper = mount(VCodeBlock, { |
| 14 | + props: { highlightjs: true } |
| 15 | + }); |
| 16 | + |
| 17 | + const returnedProps = wrapper.getComponent(VCodeBlock).props(); |
| 18 | + |
| 19 | + expect(returnedProps).toMatchSnapshot(); |
14 | 20 | });
|
15 | 21 |
|
16 |
| - const returnedProps = wrapper.getComponent(VCodeBlock).props(); |
| 22 | + it('should mount the component using global options', () => { |
| 23 | + const wrapper = mount(VCodeBlock, { |
| 24 | + global: { |
| 25 | + provide: { |
| 26 | + [codeBlockOptions]: { |
| 27 | + browserWindow: true, |
| 28 | + }, |
| 29 | + } |
| 30 | + }, |
| 31 | + props: { highlightjs: true } |
| 32 | + }); |
17 | 33 |
|
18 |
| - expect(returnedProps).toMatchSnapshot(); |
| 34 | + expect(wrapper.html()).toContain('v-code-block--code-browser'); |
| 35 | + }); |
19 | 36 | });
|
20 | 37 |
|
21 |
| - it('should emit run event', () => { |
22 |
| - const wrapper = mount(VCodeBlock, { |
23 |
| - props: { |
24 |
| - prismjs: true, |
25 |
| - runTab: true, |
26 |
| - tabs: true, |
27 |
| - } |
28 |
| - }); |
29 | 38 |
|
30 |
| - wrapper.find(`.${pluginName}--tab-run`).trigger('click'); |
| 39 | + // -------------------------------------------------- Slots // |
| 40 | + describe('Slots', () => { |
| 41 | + it('should use label slot', () => { |
| 42 | + const wrapper = mount(VCodeBlock, { |
| 43 | + props: { highlightjs: true }, |
| 44 | + slots: { |
| 45 | + label: h('div', { class: 'using-label-slot' }, 'Hello World'), |
| 46 | + } |
| 47 | + }); |
31 | 48 |
|
32 |
| - expect(wrapper.emitted()).toHaveProperty('run'); |
33 |
| - }); |
| 49 | + const labelHtml = wrapper.find('.using-label-slot').html(); |
34 | 50 |
|
| 51 | + expect(labelHtml).toMatchSnapshot(); |
| 52 | + }); |
35 | 53 |
|
36 |
| - // -------------------------------------------------- Errors // |
37 |
| - const throwErrors = { |
38 |
| - bothSet: '[vue-code-block]: You cannot have both prismjs and highlightjs props set at the same time.', |
39 |
| - neitherSet: '[vue-code-block]: You must set either the prismjs or highlightjs props.', |
40 |
| - }; |
41 |
| - |
42 |
| - it('should throw error if both prismjs and highlightjs props are true', () => { |
43 |
| - expect(() => { |
44 |
| - mount(VCodeBlock, { |
45 |
| - props: { |
46 |
| - prismjs: true, |
47 |
| - highlightjs: true, |
| 54 | + it('should use tabs slot', () => { |
| 55 | + const wrapper = mount(VCodeBlock, { |
| 56 | + props: { highlightjs: true }, |
| 57 | + slots: { |
| 58 | + tabs: h('div', { class: 'using-tabs-slot' }, 'This is the tabs slot'), |
48 | 59 | }
|
49 | 60 | });
|
50 |
| - }).toThrowError(throwErrors.bothSet); |
51 |
| - }); |
52 | 61 |
|
53 |
| - it('should throw error if both prismjs and highlightjs props are not set', () => { |
54 |
| - expect(() => { |
55 |
| - mount(VCodeBlock, { |
56 |
| - props: { |
| 62 | + const tabsHtml = wrapper.find('.using-tabs-slot').html(); |
| 63 | + |
| 64 | + expect(tabsHtml).toMatchSnapshot(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should use copyButton slot', () => { |
| 68 | + const wrapper = mount(VCodeBlock, { |
| 69 | + props: { highlightjs: true }, |
| 70 | + slots: { |
| 71 | + copyButton: h('div', { class: 'using-copy-button-slot' }, 'This is the copy button slot'), |
57 | 72 | }
|
58 | 73 | });
|
59 |
| - }).toThrowError(throwErrors.neitherSet); |
| 74 | + |
| 75 | + const copyButtonSlotHtml = wrapper.find('.using-copy-button-slot').html(); |
| 76 | + |
| 77 | + expect(copyButtonSlotHtml).toMatchSnapshot(); |
| 78 | + }); |
60 | 79 | });
|
61 | 80 |
|
62 |
| - it('should throw error if neither prismjs or highlightjs props are true', () => { |
63 |
| - expect(() => { |
64 |
| - mount(VCodeBlock, { |
| 81 | + |
| 82 | + // -------------------------------------------------- Events // |
| 83 | + describe('Events', () => { |
| 84 | + it('should emit run event', () => { |
| 85 | + const wrapper = mount(VCodeBlock, { |
65 | 86 | props: {
|
66 |
| - prismjs: false, |
67 |
| - highlightjs: false, |
| 87 | + prismjs: true, |
| 88 | + runTab: true, |
| 89 | + tabs: true, |
68 | 90 | }
|
69 | 91 | });
|
70 |
| - }).toThrowError(throwErrors.neitherSet); |
| 92 | + |
| 93 | + wrapper.find(`.${pluginName}--tab-run`).trigger('click'); |
| 94 | + |
| 95 | + expect(wrapper.emitted()).toHaveProperty('run'); |
| 96 | + }); |
71 | 97 | });
|
72 | 98 |
|
73 |
| - it('should throw error if highlightjs used with prismPlugin prop', () => { |
74 |
| - expect(() => { |
75 |
| - mount(VCodeBlock, { |
76 |
| - props: { |
77 |
| - highlightjs: true, |
78 |
| - prismPlugin: true, |
79 |
| - } |
| 99 | + |
| 100 | + // -------------------------------------------------- Attributes // |
| 101 | + describe('Attributes', () => { |
| 102 | + it('should add class hljs class to code tag when highlightjs prop is true', () => { |
| 103 | + const wrapper = mount(VCodeBlock, { |
| 104 | + props: { highlightjs: true } |
80 | 105 | });
|
81 |
| - }).toThrowError('[vue-code-block]: Highlight.js does not support PrismJS plugins. Unexpected results may occur. Remove the `prism-plugin` prop from the vue-code-block component.'); |
| 106 | + |
| 107 | + const codeTag = wrapper.getComponent(VCodeBlock).find('code').attributes(); |
| 108 | + |
| 109 | + expect(codeTag.class).toContain('hljs'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should add class attribute line-numbers to pre tag', () => { |
| 113 | + const wrapper = mount(VCodeBlock, { |
| 114 | + attrs: { class: 'line-numbers' }, |
| 115 | + props: { highlightjs: true } |
| 116 | + }); |
| 117 | + |
| 118 | + const preTag = wrapper.getComponent(VCodeBlock).find('pre').attributes(); |
| 119 | + |
| 120 | + expect(preTag.class).toContain('line-numbers'); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + |
| 125 | + // -------------------------------------------------- Errors // |
| 126 | + describe('Errors', () => { |
| 127 | + const throwErrors = { |
| 128 | + bothSet: '[vue-code-block]: You cannot have both prismjs and highlightjs props set at the same time.', |
| 129 | + neitherSet: '[vue-code-block]: You must set either the prismjs or highlightjs props.', |
| 130 | + }; |
| 131 | + |
| 132 | + it('should throw error if both prismjs and highlightjs props are true', () => { |
| 133 | + expect(() => { |
| 134 | + mount(VCodeBlock, { |
| 135 | + props: { |
| 136 | + prismjs: true, |
| 137 | + highlightjs: true, |
| 138 | + } |
| 139 | + }); |
| 140 | + }).toThrowError(throwErrors.bothSet); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should throw error if both prismjs and highlightjs props are not set', () => { |
| 144 | + expect(() => { |
| 145 | + mount(VCodeBlock); |
| 146 | + }).toThrowError(throwErrors.neitherSet); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should throw error if neither prismjs or highlightjs props are true', () => { |
| 150 | + expect(() => { |
| 151 | + mount(VCodeBlock, { |
| 152 | + props: { |
| 153 | + prismjs: false, |
| 154 | + highlightjs: false, |
| 155 | + } |
| 156 | + }); |
| 157 | + }).toThrowError(throwErrors.neitherSet); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should throw error if highlightjs used with prismPlugin prop', () => { |
| 161 | + expect(() => { |
| 162 | + mount(VCodeBlock, { |
| 163 | + props: { |
| 164 | + highlightjs: true, |
| 165 | + prismPlugin: true, |
| 166 | + } |
| 167 | + }); |
| 168 | + }).toThrowError('[vue-code-block]: Highlight.js does not support PrismJS plugins. Unexpected results may occur. Remove the `prism-plugin` prop from the vue-code-block component.'); |
| 169 | + }); |
82 | 170 | });
|
83 | 171 | });
|
0 commit comments