Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(localize,form-core): wait for localize before updating feedback #1447

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,62 @@ 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 updateLabel() {
await localize.loadingComplete;
this.label = localize.msg('test-default-label:label');
}

async setDefaultLabel() {
localize.loadNamespace({
'test-default-label': /** @param {string} locale */ async locale => {
switch (locale) {
case 'nl-NL':
return { label: 'Tekst' };
default:
return { label: 'Text' };
}
},
});
this.boundUpdateLabel = this.updateLabel.bind(this);
this.boundUpdateLabel();
localize.addEventListener('localeChanged', this.boundUpdateLabel);
}
}
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.');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was failing because "Tekst" was still "Text" from the old locale. Now that it awaits localize.loadingComplete, it will get the right localized label in the feedback message


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