[helpText]
[feedback]
~~~`, async () => {
const el = /** @type {LionField} */ (
await fixture(html`<${tag}>
${inputSlot}
Enter your Name
No name entered
${tag}>
`)
);
const nativeInput = getSlot(el, 'input');
// @ts-ignore allow protected accessors in tests
const inputId = el._inputId;
expect(nativeInput.getAttribute('aria-labelledby')).to.equal(`label-${inputId}`);
expect(nativeInput.getAttribute('aria-describedby')).to.contain(`help-text-${inputId}`);
expect(nativeInput.getAttribute('aria-describedby')).to.contain(`feedback-${inputId}`);
});
it(`allows additional slots (prefix, suffix, before, after) to be included in labelledby
(via attribute data-label) and in describedby (via attribute data-description)`, async () => {
const el = /** @type {LionField} */ (
await fixture(html`<${tag}>
${inputSlot}
[before]
[after]
[prefix]
[suffix]
${tag}>
`)
);
const nativeInput = getSlot(el, 'input');
// @ts-ignore allow protected accessors in tests
const inputId = el._inputId;
expect(nativeInput.getAttribute('aria-labelledby')).to.contain(
`before-${inputId} after-${inputId}`,
);
expect(nativeInput.getAttribute('aria-describedby')).to.contain(
`prefix-${inputId} suffix-${inputId}`,
);
});
});
describe(`Validation`, () => {
beforeEach(() => {
// Reset and preload validation translations
localizeTearDown();
localize.addData('en-GB', 'lion-validate', {
error: {
hasX: 'This is error message for hasX',
},
});
});
it('should conditionally show error', async () => {
const HasX = class extends Validator {
static get validatorName() {
return 'HasX';
}
/**
* @param {string} value
*/
execute(value) {
const result = value.indexOf('x') === -1;
return result;
}
};
const el = /** @type {LionField} */ (
await fixture(html`
<${tag}
.validators=${[new HasX()]}
.modelValue=${'a@b.nl'}
>
${inputSlot}
${tag}>
`)
);
/**
* @param {import("../index.js").LionField} _sceneEl
* @param {{ index?: number; el: any; wantedShowsFeedbackFor: any; }} scenario
*/
const executeScenario = async (_sceneEl, scenario) => {
const sceneEl = _sceneEl;
sceneEl.resetInteractionState();
sceneEl.touched = scenario.el.touched;
sceneEl.dirty = scenario.el.dirty;
sceneEl.prefilled = scenario.el.prefilled;
sceneEl.submitted = scenario.el.submitted;
await sceneEl.updateComplete;
await sceneEl.feedbackComplete;
expect(sceneEl.showsFeedbackFor).to.deep.equal(scenario.wantedShowsFeedbackFor);
};
await executeScenario(el, {
index: 0,
el: { touched: true, dirty: true, prefilled: false, submitted: false },
wantedShowsFeedbackFor: ['error'],
});
await executeScenario(el, {
index: 1,
el: { touched: false, dirty: false, prefilled: true, submitted: false },
wantedShowsFeedbackFor: ['error'],
});
await executeScenario(el, {
index: 2,
el: { touched: false, dirty: false, prefilled: false, submitted: true },
wantedShowsFeedbackFor: ['error'],
});
await executeScenario(el, {
index: 3,
el: { touched: false, dirty: true, prefilled: false, submitted: false },
wantedShowsFeedbackFor: [],
});
await executeScenario(el, {
index: 4,
el: { touched: true, dirty: false, prefilled: false, submitted: false },
wantedShowsFeedbackFor: [],
});
});
it('should not run validation when disabled', async () => {
const HasX = class extends Validator {
static get validatorName() {
return 'HasX';
}
/**
* @param {string} value
*/
execute(value) {
const result = value.indexOf('x') === -1;
return result;
}
};
const disabledEl = /** @type {LionField} */ (
await fixture(html`
<${tag}
disabled
.validators=${[new HasX()]}
.modelValue=${'a@b.nl'}
>
${inputSlot}
${tag}>
`)
);
const el = /** @type {LionField} */ (
await fixture(html`
<${tag}
.validators=${[new HasX()]}
.modelValue=${'a@b.nl'}
>
${inputSlot}
${tag}>
`)
);
expect(el.hasFeedbackFor).to.deep.equal(['error']);
expect(el.validationStates.error.HasX).to.exist;
expect(disabledEl.hasFeedbackFor).to.deep.equal([]);
expect(disabledEl.validationStates.error).to.deep.equal({});
});
it('can be required', async () => {
const el = /** @type {LionField} */ (
await fixture(html`
<${tag}
.validators=${[new Required()]}
>${inputSlot}${tag}>
`)
);
expect(el.hasFeedbackFor).to.deep.equal(['error']);
expect(el.validationStates.error.Required).to.exist;
el.modelValue = 'cat';
expect(el.hasFeedbackFor).to.deep.equal([]);
expect(el.validationStates.error.Required).to.not.exist;
});
it('will only update formattedValue when valid on `user-input-changed`', async () => {
const formatterSpy = sinon.spy(value => `foo: ${value}`);
const Bar = class extends Validator {
static get validatorName() {
return 'Bar';
}
/**
* @param {string} value
*/
execute(value) {
const hasError = value !== 'bar';
return hasError;
}
};
const el = /** @type {LionField} */ (
await fixture(html`
<${tag}
.modelValue=${'init-string'}
.formatter=${formatterSpy}
.validators=${[new Bar()]}
>${inputSlot}${tag}>
`)
);
expect(formatterSpy.callCount).to.equal(0);
expect(el.formattedValue).to.equal('init-string');
el.modelValue = 'bar';
expect(formatterSpy.callCount).to.equal(1);
expect(el.formattedValue).to.equal('foo: bar');
mimicUserInput(el, 'foo');
expect(formatterSpy.callCount).to.equal(1);
expect(el.value).to.equal('foo');
});
});
describe(`Content projection`, () => {
it('renders correctly all slot elements in light DOM', async () => {
const el = /** @type {LionField} */ (
await fixture(html`
<${tag}>
${inputSlot}
[help-text]
[before]
[after]
[prefix]
[suffix]
[feedback]
${tag}>
`)
);
const names = [
'label',
'input',
'help-text',
'before',
'after',
'prefix',
'suffix',
'feedback',
];
names.forEach(slotName => {
const slotLight = /** @type {HTMLElement} */ (el.querySelector(`[slot="${slotName}"]`));
slotLight.setAttribute('test-me', 'ok');
const slot = /** @type {ShadowHTMLElement} */ (
/** @type {ShadowRoot} */ (el.shadowRoot).querySelector(`slot[name="${slotName}"]`)
);
const assignedNodes = slot.assignedNodes();
expect(assignedNodes.length).to.equal(1);
expect(assignedNodes[0].getAttribute('test-me')).to.equal('ok');
});
});
});
});