PHP Classes

File: tests/js/themebuilder.test.js

Recommend this page to a friend!
  Packages of Boss Ibrahim Mussa   Caelrik   tests/js/themebuilder.test.js   Download  
File: tests/js/themebuilder.test.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Caelrik
Build Web user interface from PHP components
Author: By
Last change:
Date: 8 days ago
Size: 6,333 bytes
 

 

Contents

Class file image Download
import { initThemeBuilder, scopeCss } from '../../resources/js/themebuilder.js'; import { readOptions } from '../../resources/js/core.js'; import { loadFixture } from './helpers.js'; describe('scopeCss', () => { test('applies a theme stylesheet inside the preview only', () => { const css = '/* Theme */\n:root,\n[data-bs-theme=light] {\n --a: 1;\n}\n[data-bs-theme=dark] {\n --a: 2;\n}\n' + ':root {\n --r: 1rem;\n}\n.form-control,\n.form-select:focus {\n color: red;\n}\n'; expect(scopeCss(css, '#b .c')).toBe('#b .c,\n#b .c[data-bs-theme=light] {\n --a: 1;\n}\n#b .c[data-bs-theme=dark] {\n --a: 2;\n}\n' + '#b .c {\n --r: 1rem;\n}\n#b .c .form-control,\n#b .c .form-select:focus {\n color: red;\n}'); }); }); describe('theme builder (fixture: bootstrap preset, purple primary, light error color at 4.49:1)', () => { let element; let builder; beforeEach(() => { element = loadFixture('themebuilder'); builder = initThemeBuilder(element, readOptions(element)); }); afterEach(() => { document.querySelectorAll('style[data-caelrik-theme-preview]').forEach((style) => style.remove()); }); const $ = (selector) => element.querySelector(selector); const text = (id) => document.getElementById(id); const type = (input, value, { commit = true } = {}) => { input.value = value; input.dispatchEvent(new Event('input', { bubbles: true })); if (commit) { input.dispatchEvent(new Event('change', { bubbles: true })); } }; const previewCss = () => document.querySelector('style[data-caelrik-theme-preview="builder"]').textContent; const failures = () => [...element.querySelectorAll('.caelrik-theme-builder__pair.is-fail')].map((li) => li.dataset.caelrikPair); test('reveals the JavaScript controls and previews the theme in the preview area only', () => { for (const part of ['__history', '__export', '__modes']) { expect($(`.caelrik-theme-builder${part}`).hidden).toBe(false); } expect(previewCss()).toContain('#builder .caelrik-theme-builder__canvas,\n#builder .caelrik-theme-builder__canvas[data-bs-theme=light] {'); expect(previewCss()).toContain(' --bs-primary: #6750a4;'); expect(previewCss()).not.toMatch(/(^|\n):root/); expect(failures()).toEqual(['light.danger.bodyBg']); expect($('.caelrik-theme-builder__summary').textContent).toBe('1 pair is below its minimum.'); }); test('a hex field updates its picker, the preview and the report as you type', () => { const changes = []; builder.onChange((theme) => changes.push(theme.light.danger)); type(text('builder-light-danger'), '#B3261E', { commit: false }); expect(text('builder-light-danger-picker').value).toBe('#b3261e'); expect(previewCss()).toContain(' --bs-form-invalid-color: #b3261e;'); expect(failures()).toEqual([]); expect($('.caelrik-theme-builder__summary').textContent).toBe('Every pair meets its minimum.'); expect(changes).toEqual(['#b3261e']); }); test('the picker updates its hex field', () => { type(text('builder-dark-primary-picker'), '#bb86fc'); expect(text('builder-dark-primary').value).toBe('#bb86fc'); expect(builder.getTheme().dark.primary).toBe('#bb86fc'); }); test('an invalid color is reported on its field and not applied', () => { const input = text('builder-light-primary'); type(input, '#12'); expect(input.getAttribute('aria-invalid')).toBe('true'); expect(text('builder-light-primary-error').textContent).toBe('Use a color like #0d6efd.'); expect(input.getAttribute('aria-describedby')).toBe('builder-light-primary-help builder-light-primary-error'); expect(builder.getTheme().light.primary).toBe('#6750a4'); type(input, '#123456'); expect(input.hasAttribute('aria-invalid')).toBe(false); expect(text('builder-light-primary-error')).toBeNull(); expect(input.getAttribute('aria-describedby')).toBe('builder-light-primary-help'); }); test('radius, font and shadow change the theme', () => { type($('[name="theme[radius]"]'), '1.25'); type($('[name="theme[fontFamily]"]'), 'Inter, sans-serif'); type($('[name="theme[shadow]"]'), ''); const theme = builder.getTheme(); expect([theme.radius, theme.fontFamily, theme.shadow]).toEqual([1.25, 'Inter, sans-serif', undefined]); expect(text('builder-radius-value').textContent).toBe('1.25'); expect(previewCss()).toContain(' --caelrik-radius: 1.25rem;'); type($('[name="theme[fontFamily]"]'), 'Inter; } body {'); expect($('[name="theme[fontFamily]"]').getAttribute('aria-invalid')).toBe('true'); expect(builder.getTheme().fontFamily).toBe('Inter, sans-serif'); }); test('undo steps back one finished change at a time; reset returns to the start', () => { const undo = $('[data-caelrik-action="undo"]'); expect(undo.disabled).toBe(true); const primary = text('builder-light-primary'); type(primary, '#111111', { commit: false }); type(primary, '#222222'); // one change event: one undo step type($('[name="theme[radius]"]'), '1'); expect(undo.disabled).toBe(false); undo.click(); expect(builder.getTheme().radius).toBe(0.375); expect(builder.getTheme().light.primary).toBe('#222222'); expect($('[name="theme[radius]"]').value).toBe('0.375'); undo.click(); expect(builder.getTheme().light.primary).toBe('#6750a4'); expect(primary.value).toBe('#6750a4'); expect(undo.disabled).toBe(true); expect(document.activeElement).toBe($('[name="theme[name]"]')); type(primary, '#333333'); $('[data-caelrik-action="reset"]').click(); expect(builder.getTheme().light).toEqual(readOptions(element).theme.light); }); test('the preview mode switches the preview between light and dark', () => { const canvas = $('.caelrik-theme-builder__canvas'); const dark = text('builder-preview-dark'); dark.checked = true; dark.dispatchEvent(new Event('change', { bubbles: true })); expect(canvas.dataset.bsTheme).toBe('dark'); }); test('setTheme validates like Theme::fromArray()', () => { expect(() => builder.setTheme({ ...builder.getTheme(), base: 'bulma' })).toThrow('The theme base can only be "bootstrap".'); expect(() => builder.setTheme({ ...builder.getTheme(), shadow: 'glow' })).toThrow('Unknown shadow'); }); });