|
1 | | -import { HTMLCodeBlockElement } from '../src/index.common'; |
| 1 | +import HTMLCodeBlockElement from '../src/class/HTMLCodeBlockElement'; |
2 | 2 |
|
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 | + // }); |
5 | 102 | }); |
0 commit comments