Debian Codegraph @

tests/jest/codemirror.panel.test.js

raw
1 const CodeMirrorPanel = require( '../../resources/codemirror.panel.js' );
2
3 // CodeMirrorPanel is tagged as abstract, but being JavaScript it isn't a
4 // "real" abstract class, so we can instantiate it directly for testing purposes.
5 const cmPanel = new CodeMirrorPanel();
6
7 describe( 'CodeMirrorPanel', () => {
8 it( 'should create a Codex TextInput', () => {
9 const [ inputWrapper, input ] = cmPanel.getTextInput( 'foo', 'bar', 'codemirror-find' );
10 expect( inputWrapper.className ).toBe( 'cdx-text-input cm-mw-panel--text-input' );
11 expect( input.className ).toBe( 'cdx-text-input__input' );
12 expect( input.type ).toBe( 'text' );
13 expect( input.name ).toBe( 'foo' );
14 // No i18n in unit tests, so we only check for the key.
15 expect( input.placeholder ).toBe( 'codemirror-find' );
16 expect( input.value ).toBe( 'bar' );
17 } );
18
19 it( 'should create a Codex Button with no icon', () => {
20 const buttonNoIcon = cmPanel.getButton( 'foo' );
21 expect( buttonNoIcon.tagName ).toBe( 'BUTTON' );
22 expect( buttonNoIcon.className ).toBe( 'cdx-button cm-mw-panel--button' );
23 expect( buttonNoIcon.type ).toBe( 'button' );
24 expect( buttonNoIcon.children.length ).toBe( 0 );
25 } );
26
27 it( 'should create a Codex button with an icon and a label', () => {
28 const buttonWithIcon = cmPanel.getButton( 'foo', 'bar' );
29 expect( buttonWithIcon.tagName ).toBe( 'BUTTON' );
30 expect( buttonWithIcon.className ).toBe( 'cdx-button cm-mw-panel--button' );
31 expect( buttonWithIcon.type ).toBe( 'button' );
32 expect( buttonWithIcon.children.length ).toBe( 1 );
33 const iconSpan = buttonWithIcon.children[ 0 ];
34 expect( iconSpan.tagName ).toBe( 'SPAN' );
35 expect( iconSpan.className ).toBe( 'cdx-button__icon cm-mw-icon--bar' );
36 expect( iconSpan.getAttribute( 'aria-hidden' ) ).toBe( 'true' );
37 } );
38
39 it( 'should create an icon-only Codex button', () => {
40 const buttonIconOnly = cmPanel.getButton( 'foo', 'bar', true );
41 expect( buttonIconOnly.tagName ).toBe( 'BUTTON' );
42 expect( buttonIconOnly.className ).toBe(
43 'cdx-button cm-mw-panel--button cdx-button--icon-only'
44 );
45 expect( buttonIconOnly.type ).toBe( 'button' );
46 expect( buttonIconOnly.children.length ).toBe( 1 );
47 expect( buttonIconOnly.getAttribute( 'aria-label' ) ).toBe( 'foo' );
48 expect( buttonIconOnly.title ).toBe( 'foo' );
49 const iconSpan = buttonIconOnly.children[ 0 ];
50 expect( iconSpan.tagName ).toBe( 'SPAN' );
51 expect( iconSpan.className ).toBe( 'cdx-button__icon cm-mw-icon--bar' );
52 expect( iconSpan.getAttribute( 'aria-hidden' ) ).toBeNull();
53 } );
54
55 it( 'should create a Codex Checkbox', () => {
56 const [ checkboxWrapper, checkbox ] = cmPanel.getCheckbox( 'foo', 'bar', true );
57 expect( checkboxWrapper.className ).toBe( 'cdx-checkbox cdx-checkbox--inline cm-mw-panel--checkbox' );
58 expect( checkboxWrapper.children.length ).toBe( 3 );
59 const labelWrapper = checkboxWrapper.children[ 2 ];
60 expect( labelWrapper.tagName ).toBe( 'DIV' );
61 expect( labelWrapper.className ).toBe( 'cdx-checkbox__label cdx-label' );
62 const label = labelWrapper.children[ 0 ];
63 expect( label.tagName ).toBe( 'LABEL' );
64 expect( label.className ).toBe( 'cdx-label__label' );
65 expect( label.textContent ).toBe( 'bar' );
66 expect( checkbox.className ).toBe( 'cdx-checkbox__input' );
67 expect( checkbox.type ).toBe( 'checkbox' );
68 expect( checkbox.name ).toBe( 'foo' );
69 expect( checkbox.checked ).toBe( true );
70 } );
71
72 it( 'should create a Codex ToggleButton', () => {
73 const toggleButtonOn = cmPanel.getToggleButton( 'foo', 'bar', 'baz', true );
74 expect( toggleButtonOn.tagName ).toBe( 'BUTTON' );
75 expect( toggleButtonOn.className ).toBe(
76 'cdx-toggle-button cdx-toggle-button--framed cdx-toggle-button--toggled-on cm-mw-panel--toggle-button'
77 );
78 expect( toggleButtonOn.dataset.checked ).toBe( 'true' );
79 expect( toggleButtonOn.getAttribute( 'aria-pressed' ) ).toBe( 'true' );
80 expect( toggleButtonOn.title ).toBe( 'bar' );
81 expect( toggleButtonOn.getAttribute( 'aria-label' ) ).toBe( 'bar' );
82 expect( toggleButtonOn.children.length ).toBe( 1 );
83 const iconSpan = toggleButtonOn.children[ 0 ];
84 expect( iconSpan.tagName ).toBe( 'SPAN' );
85 expect( iconSpan.className ).toBe( 'cdx-icon cdx-icon--medium cm-mw-icon--baz' );
86
87 const toggleButtonOff = cmPanel.getToggleButton( 'foo', 'bar', 'baz', false );
88 expect( toggleButtonOff.className ).toBe(
89 'cdx-toggle-button cdx-toggle-button--framed cdx-toggle-button--toggled-off cm-mw-panel--toggle-button'
90 );
91 expect( toggleButtonOff.dataset.checked ).toBe( 'false' );
92 expect( toggleButtonOff.getAttribute( 'aria-pressed' ) ).toBe( 'false' );
93 } );
94 } );