Skip to content

Commit

Permalink
fix: Create instance own localize method (#209)
Browse files Browse the repository at this point in the history
* Create instance own localize method

* Add tests
  • Loading branch information
bearfriend authored Nov 14, 2024
1 parent 2d7962d commit 37eae40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export const Localize = class extends getLocalizeClass() {
constructor(config) {
super();
super.constructor.setLocalizeMarkup(localizeMarkup);
this.localize = (...args) => super.localize(...args);
this.config = config;
this.connect();
}
Expand Down
23 changes: 19 additions & 4 deletions test/localize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ const resources = {

describe('Localize', () => {

let localizer, runCount, updatePromise;
let config, localizer, runCount, updatePromise;
beforeEach(async() => {
await fixture('<div></div>');
runCount = 0;

let resolve;
updatePromise = new Promise(r => resolve = r);

localizer = {};
localizer = new Localize({
config = {
importFunc: async lang => await new Promise(r => setTimeout(() => r(resources[lang]), 1)),
onResourcesChange: () => {
if (runCount) resolve();
runCount++;
}
});
};
localizer = new Localize(config);
expect(runCount).to.equal(0);
});

Expand All @@ -56,6 +56,21 @@ describe('Localize', () => {
expect(localizer.pristine).to.be.false;
});

it('should have its own localize method', async() => {
await localizer.ready;
expect(Object.hasOwn(localizer, 'localize')).to.be.true;
});

it('should not share resources between instances', async() => {
await localizer.ready;
const localizer2 = new Localize({ importFunc: config.importFunc });
expect(localizer2.localize.resources).to.be.undefined;
await localizer2.ready;
expect(localizer2.localize.resources).to.be.an('object');
expect(localizer.localize.resources).to.not.equal(localizer2.localize.resources);
expect(localizer.localize.resources).to.deep.equal(localizer2.localize.resources);
});

describe('onResourcesChange', () => {

it('runs when the document locale changes', async() => {
Expand Down

0 comments on commit 37eae40

Please sign in to comment.