/* eslint-env mocha */ /* eslint-disable class-methods-use-this, no-underscore-dangle */ import { expect, fixture, html, aTimeout, defineCE, unsafeStatic } from '@open-wc/testing'; import sinon from 'sinon'; import { LionLitElement } from '@lion/core/src/LionLitElement.js'; import { Unparseable } from '@lion/validate'; import { FormatMixin } from '../src/FormatMixin.js'; function mimicUserInput(formControl, newViewValue) { formControl.value = newViewValue; // eslint-disable-line no-param-reassign formControl.inputElement.dispatchEvent(new CustomEvent('input', { bubbles: true })); } /* eslint-disable no-unused-expressions */ describe('FormatMixin', () => { let elem; let nonFormat; let fooFormat; before(async () => { const tagString = defineCE( class extends FormatMixin(LionLitElement) { render() { return html` `; } set value(newValue) { this.inputElement.value = newValue; } get value() { return this.inputElement.value; } get inputElement() { return this.querySelector('input'); } }, ); elem = unsafeStatic(tagString); nonFormat = await fixture(html`<${elem}>`); fooFormat = await fixture(html` <${elem} .formatter="${value => `foo: ${value}`}" .parser="${value => value.replace('foo: ', '')}" .serializer="${value => `[foo] ${value}`}" .deserializer="${value => value.replace('[foo] ', '')}" > `); }); it('fires `model-value-changed` for every change on the input', async () => { const formatEl = await fixture(html`<${elem}>`); let counter = 0; formatEl.addEventListener('model-value-changed', () => { counter += 1; }); mimicUserInput(formatEl, 'one'); expect(counter).to.equal(1); // no change means no event mimicUserInput(formatEl, 'one'); expect(counter).to.equal(1); mimicUserInput(formatEl, 'two'); expect(counter).to.equal(2); }); it('fires `model-value-changed` for every modelValue change', async () => { const el = await fixture(html`<${elem}>`); let counter = 0; el.addEventListener('model-value-changed', () => { counter += 1; }); el.modelValue = 'one'; expect(counter).to.equal(1); // no change means no event el.modelValue = 'one'; expect(counter).to.equal(1); el.modelValue = 'two'; expect(counter).to.equal(2); }); it('has modelValue, formattedValue and serializedValue which are computed synchronously', async () => { expect(nonFormat.modelValue).to.equal('', 'modelValue initially'); expect(nonFormat.formattedValue).to.equal('', 'formattedValue initially'); expect(nonFormat.serializedValue).to.equal('', 'serializedValue initially'); nonFormat.modelValue = 'string'; expect(nonFormat.modelValue).to.equal('string', 'modelValue as provided'); expect(nonFormat.formattedValue).to.equal('string', 'formattedValue synchronized'); expect(nonFormat.serializedValue).to.equal('string', 'serializedValue synchronized'); }); it('has an input node (like /