Skip to content

Commit

Permalink
bugfix: Waited for dynamic import to resolve (#2100)
Browse files Browse the repository at this point in the history
* refactor: Added failing tests

* bugfix: Waited for dynamic import to resolve

* chore: Added lockfile

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Aug 6, 2024
1 parent 2df2cc4 commit 6aafbf4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ember-flatpickr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
},
"dependencies": {
"@ember/render-modifiers": "^2.0.3",
"@ember/test-helpers": "~3.3.0",
"@ember/test-helpers": "^3.3.0",
"@ember/test-waiters": "^3.1.0",
"@embroider/addon-shim": "^1.8.7"
},
"devDependencies": {
Expand Down
7 changes: 5 additions & 2 deletions ember-flatpickr/src/components/ember-flatpickr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Component from '@glimmer/component';
import { action } from '@ember/object';
import { assert } from '@ember/debug';
import { scheduleOnce } from '@ember/runloop';
import { waitForPromise } from '@ember/test-waiters';
import type { Instance as FlatpickrInstance } from 'flatpickr/dist/types/instance';
import type { BaseOptions as FlatpickrOptions } from 'flatpickr/dist/types/options';
import flatpickr from 'flatpickr';
Expand Down Expand Up @@ -133,8 +134,10 @@ export default class EmberFlatpickr extends Component<EmberFlatpickrArgs> {
Object.entries(rest).filter((entry) => entry[1] !== undefined),
);

if (typeof this.args.locale === 'string' && this.args.locale !== "en") {
await import(`flatpickr/dist/l10n/${this.args.locale}.js`);
if (typeof this.args.locale === 'string' && this.args.locale !== 'en') {
await waitForPromise(
import(`flatpickr/dist/l10n/${this.args.locale}.js`),
);
}

this.flatpickrRef = flatpickr(element, {
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 35 additions & 6 deletions test-app/tests/integration/components/ember-flatpickr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
render,
find,
findAll,
settled,
triggerEvent,
waitFor,
} from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import {
Expand Down Expand Up @@ -431,7 +431,7 @@ module('Integration | Component | ember flatpickr', function (hooks) {
assert.strictEqual(enabledDays[1].textContent, '25');
});

test('locale works correctly', async function (assert) {
test('locale as object works correctly', async function (assert) {
assert.expect(1);

this.set('dateValue', '2080-12-01T16:16:22.585Z');
Expand Down Expand Up @@ -463,7 +463,6 @@ module('Integration | Component | ember flatpickr', function (hooks) {
this.set('dateValue', '2080-12-01T16:16:22.585Z');
this.set('maxDate', '2080-12-31T16:16:22.585Z');
this.set('minDate', '2080-12-01T16:16:22.585Z');

this.set('locale', 'fr');

await render(hbs`<EmberFlatpickr
Expand All @@ -475,8 +474,6 @@ module('Integration | Component | ember flatpickr', function (hooks) {
placeholder="Pick date"
/>`);

await waitFor('.flatpickr-current-month .flatpickr-monthDropdown-month');

assert.strictEqual(
find(
'.flatpickr-current-month .flatpickr-monthDropdown-month',
Expand All @@ -486,7 +483,7 @@ module('Integration | Component | ember flatpickr', function (hooks) {
);
});

test('onLocaleUpdated fired', async function (assert) {
test('onLocaleUpdated (locale as object)', async function (assert) {
assert.expect(1);

this.set('dateValue', '2080-12-01T16:16:22.585Z');
Expand All @@ -504,6 +501,38 @@ module('Integration | Component | ember flatpickr', function (hooks) {
/>`);

this.set('locale', langs.ru);
await settled();

await openFlatpickr();

assert.strictEqual(
find(
'.flatpickr-current-month .flatpickr-monthDropdown-month',
).textContent.trim(),
'Декабрь',
'Russian locale applied successfully',
);
});

test('onLocaleUpdated (locale as string)', async function (assert) {
assert.expect(1);

this.set('dateValue', '2080-12-01T16:16:22.585Z');
this.set('maxDate', '2080-12-31T16:16:22.585Z');
this.set('minDate', '2080-12-01T16:16:22.585Z');
this.set('locale', 'fr');

await render(hbs`<EmberFlatpickr
@date={{this.dateValue}}
@locale={{this.locale}}
@maxDate={{this.maxDate}}
@minDate={{this.minDate}}
@onChange={{null}}
placeholder="Pick date"
/>`);

this.set('locale', 'ru');
await settled();

await openFlatpickr();

Expand Down

0 comments on commit 6aafbf4

Please sign in to comment.