Skip to content

Commit 14493b7

Browse files
🚨 Update
1 parent 8391c69 commit 14493b7

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

test/HTMLCodeBlockElement.test.ts

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,102 @@
1-
import { HTMLCodeBlockElement } from '../src/index.common';
1+
import HTMLCodeBlockElement from '../src/class/HTMLCodeBlockElement';
22

3-
test('Basic usege', () => {
4-
expect(!!HTMLCodeBlockElement).toBe(true);
3+
HTMLCodeBlockElement.highlight = function hoge() {
4+
return {
5+
markup: 'Debug Text',
6+
}
7+
};
8+
9+
customElements.define('code-block', HTMLCodeBlockElement);
10+
11+
describe('Props', () => {
12+
test('Default', () => {
13+
const cbElm = new HTMLCodeBlockElement();
14+
15+
expect(cbElm.controls).toBe(false);
16+
expect(cbElm.label).toBe('');
17+
expect(cbElm.language).toBe('');
18+
expect(cbElm.value).toBe('');
19+
});
20+
21+
test('With controls attributes', () => {
22+
const cbElm = new HTMLCodeBlockElement();
23+
24+
expect(cbElm.getAttribute('controls')).toBeNull();
25+
cbElm.controls = true;
26+
expect(cbElm.getAttribute('controls')).toBe('');
27+
cbElm.controls = false;
28+
expect(cbElm.getAttribute('controls')).toBeNull();
29+
});
30+
31+
test('With label attributes', () => {
32+
const cbElm = new HTMLCodeBlockElement();
33+
34+
expect(cbElm.getAttribute('label')).toBeNull();
35+
cbElm.label = 'label text';
36+
expect(cbElm.getAttribute('label')).toBe('label text');
37+
cbElm.label = '';
38+
expect(cbElm.getAttribute('label')).toBe('');
39+
cbElm.label = null;
40+
expect(cbElm.getAttribute('label')).toBeNull();
41+
});
42+
43+
test('With language attributes', () => {
44+
const cbElm = new HTMLCodeBlockElement();
45+
46+
expect(cbElm.getAttribute('language')).toBeNull();
47+
cbElm.language = 'js';
48+
expect(cbElm.getAttribute('language')).toBe('js');
49+
cbElm.language = '';
50+
expect(cbElm.getAttribute('language')).toBe('');
51+
cbElm.language = null;
52+
expect(cbElm.getAttribute('language')).toBeNull();
53+
});
54+
55+
test('value prop with blank line', () => {
56+
const cbElm = new HTMLCodeBlockElement();
57+
const value = `
58+
a
59+
b
60+
c
61+
`;
62+
63+
expect(cbElm.value).toBe('');
64+
cbElm.value = value;
65+
expect(cbElm.value).toBe(value);
66+
});
67+
});
68+
69+
describe('Render', () => {
70+
document.body.innerHTML = `<code-block>abc</code-block>`;
71+
72+
const cb = document.body.firstElementChild! as HTMLCodeBlockElement;
73+
74+
test('connectedCallback', () => {
75+
expect(cb.children.length).toBe(3);
76+
});
77+
test('Attributes', () => {
78+
expect(cb.children[0].tagName.toLowerCase()).toBe('span');
79+
expect(cb.children[1].tagName.toLowerCase()).toBe('button');
80+
expect(cb.children[2].tagName.toLowerCase()).toBe('pre');
81+
expect(cb.children[2].children[0].tagName.toLowerCase()).toBe('code');
82+
});
83+
test('value', () => {
84+
const value = `
85+
a
86+
b
87+
c
88+
`;
89+
90+
expect(cb.value).toBe('abc');
91+
cb.value = 'abcd';
92+
expect(cb.value).toBe('abcd');
93+
cb.value = value;
94+
expect(cb.value).toBe(value);
95+
});
96+
// test('Clipboard', () => {
97+
// document.body.innerHTML = `<code-block>abc</code-block>`;
98+
// document.body.firstElementChild?.querySelector('button')?.click();
99+
100+
// expect(navigator.clipboard.readText()).toBe('abc');
101+
// });
5102
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import HTMLCodeBlockElement from '../src/class/HTMLCodeBlockElement';
2+
3+
describe('Error', () => {
4+
test('No Endgine', () => {
5+
expect(() => HTMLCodeBlockElement.highlight('sample')).toThrowError('The syntax highlighting engine is not set to `HTMLCodeBlockElement.highlight`.');
6+
});
7+
});

0 commit comments

Comments
 (0)