1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import { nextTick } from 'vue';
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2';
import { shallowMount } from '@vue/test-utils';
import CodeBlockWrapper from '~/content_editor/components/wrappers/code_block.vue';
import codeBlockLanguageLoader from '~/content_editor/services/code_block_language_loader';
jest.mock('~/content_editor/services/code_block_language_loader');
describe('content/components/wrappers/code_block', () => {
const language = 'yaml';
let wrapper;
let updateAttributesFn;
const createWrapper = async (nodeAttrs = { language }) => {
updateAttributesFn = jest.fn();
wrapper = shallowMount(CodeBlockWrapper, {
propsData: {
node: {
attrs: nodeAttrs,
},
updateAttributes: updateAttributesFn,
},
});
};
beforeEach(() => {
codeBlockLanguageLoader.findLanguageBySyntax.mockReturnValue({ syntax: language });
});
afterEach(() => {
wrapper.destroy();
});
it('renders a node-view-wrapper as a pre element', () => {
createWrapper();
expect(wrapper.findComponent(NodeViewWrapper).props().as).toBe('pre');
expect(wrapper.findComponent(NodeViewWrapper).classes()).toContain('gl-relative');
});
it('adds content-editor-code-block class to the pre element', () => {
createWrapper();
expect(wrapper.findComponent(NodeViewWrapper).classes()).toContain('content-editor-code-block');
});
it('renders a node-view-content as a code element', () => {
createWrapper();
expect(wrapper.findComponent(NodeViewContent).props().as).toBe('code');
});
it('renders label indicating that code block is frontmatter', () => {
createWrapper({ isFrontmatter: true, language });
const label = wrapper.find('[data-testid="frontmatter-label"]');
expect(label.text()).toEqual('frontmatter:yaml');
expect(label.classes()).toEqual(['gl-absolute', 'gl-top-0', 'gl-right-3']);
});
it('loads code block’s syntax highlight language', async () => {
createWrapper();
expect(codeBlockLanguageLoader.loadLanguage).toHaveBeenCalledWith(language);
await nextTick();
expect(updateAttributesFn).toHaveBeenCalledWith({ language });
});
});
|