Skip to content

Commit

Permalink
fix(localize,form-core): wait for localize before updating feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jul 7, 2021
1 parent 5e7e43d commit 36d3c46
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/sour-oranges-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lion/form-core': minor
'@lion/localize': minor
---

Make ValidateMixin feedback message wait for localize loadingComplete, to ensure getting the right fieldName if this refers to a localized label.
2 changes: 1 addition & 1 deletion docs/docs/systems/localize/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Let's look at both cases in depth.
```js
// use the dynamic import to load static assets
localize.loadNamespace({
'my-hello-component': locale => {
'my-hello-component': async locale => {
// resolves to a module with the module.default `{ greeting: 'Hallo {name}!' }`
return import(`./translations/${locale}.js`);
},
Expand Down
1 change: 1 addition & 0 deletions packages/form-core/src/validate/ValidateMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ export const ValidateMixinImplementation = superclass =>
* @private
*/
async __getFeedbackMessages(validators) {
await localize.loadingComplete;
let fieldName = await this.fieldName;
return Promise.all(
validators.map(async validator => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, fixture, defineCE } from '@open-wc/testing';
import { html, unsafeStatic } from 'lit/static-html.js';
import { Required, DefaultSuccess, Validator } from '@lion/form-core';
import { localize } from '@lion/localize';
import { loadDefaultFeedbackMessages } from '@lion/validate-messages';
import { LionInput } from '@lion/input';
import sinon from 'sinon';
Expand Down Expand Up @@ -117,5 +118,63 @@ describe('Form Validation Integrations', () => {
await el.feedbackComplete;
expect(spy.called).to.be.false;
});

it('correctly renders localized fieldName (label), even on locale changes', async () => {
class DefaultLabelInput extends LionInput {
constructor() {
super();
this.setDefaultLabel();
}

async setDefaultLabel() {
async function update() {
await localize.loadingComplete;
this.label = localize.msg('test-default-label:label');
}
const boundUpdate = update.bind(this);
localize.loadNamespace({
'test-default-label': /** @param {string} locale */ async locale => {
switch (locale) {
case 'nl-NL':
return { label: 'Tekst' };
default:
return { label: 'Text' };
}
},
});
boundUpdate();
localize.addEventListener('localeChanged', () => {
boundUpdate();
});
}
}
const elTagString = defineCE(DefaultLabelInput);
const elTag = unsafeStatic(elTagString);
const el = /** @type {LionInput} */ (
await fixture(html`
<${elTag}
.validators=${[new Required()]}
>${lightDom}</${elTag}>
`)
);
el.touched = true;
el.dirty = true;
await el.updateComplete;
await el.feedbackComplete;
const { _feedbackNode } = getFormControlMembers(el);
expect(_feedbackNode.feedbackData?.[0].message).to.equal('Please enter a(n) Text.');

localize.locale = 'nl-NL';
await el.updateComplete;
await el.feedbackComplete;
expect(el.label).to.equal('Tekst');
expect(_feedbackNode.feedbackData?.[0].message).to.equal('Vul een Tekst in.');

localize.locale = 'en-GB';
await el.updateComplete;
await el.feedbackComplete;
expect(el.label).to.equal('Text');
expect(_feedbackNode.feedbackData?.[0].message).to.equal('Please enter a(n) Text.');
});
});
});
7 changes: 5 additions & 2 deletions packages/localize/src/LocalizeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,13 @@ export class LocalizeManager {
}

/**
* @returns {Promise.<Object>}
* @returns {Promise.<Object|void>}
*/
get loadingComplete() {
return Promise.all(Object.values(this.__namespaceLoaderPromisesCache[this.locale]));
if (typeof this.__namespaceLoaderPromisesCache[this.locale] === 'object') {
return Promise.all(Object.values(this.__namespaceLoaderPromisesCache[this.locale]));
}
return Promise.resolve();
}

reset() {
Expand Down

0 comments on commit 36d3c46

Please sign in to comment.