-
Notifications
You must be signed in to change notification settings - Fork 56
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
bugfix: Waited for dynamic import to resolve #2100
bugfix: Waited for dynamic import to resolve #2100
Conversation
@@ -475,8 +474,6 @@ module('Integration | Component | ember flatpickr', function (hooks) { | |||
placeholder="Pick date" | |||
/>`); | |||
|
|||
await waitFor('.flatpickr-current-month .flatpickr-monthDropdown-month'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed waitFor()
to see if the test can pass without it, and to document how consuming projects may have to write their tests.
@@ -504,6 +501,38 @@ module('Integration | Component | ember flatpickr', function (hooks) { | |||
/>`); | |||
|
|||
this.set('locale', langs.ru); | |||
await settled(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to call settled()
after updating the value of @locale
using this.set()
.
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( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ijlee2 can you please explain what this is doing? I would have thought awaiting the import would be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await import
is an asynchronous task that is pure JavaScript, so Ember tests can't know when translations are available (when the component is in a settled state), without using waitFor()
or waitUntil()
from @ember/test-helpers
, for example.
I think the tests in this repo might have been passing, because the time to dynamically load the French translations has been short. On the other hand, tests in my production project have sometimes failed, because it can take a while to render a page.
By wrapping the dynamic import with waitForPromise()
, projects that consume ember-flatpickr
don't need to write additional code such as waitFor()
or waitUntil()
in their tests.
module('Integration | Component | appointment-form/date', function (hooks) {
setupRenderingTest(hooks);
test('We can show the current date', async function (assert) {
await render(hbs`
<AppointmentForm::Date
@date={{this.date}}
/>
`);
- await waitUntilDateRenders(); // a test helper that uses waitUntil()
assert.dom('[data-test-field="Date"]').hasValue('06.08.2024');
});
test('We can update the date', async function (assert) {
await render(hbs`
<AppointmentForm::Date
@date={{this.date}}
/>
`);
await click('[data-test-field="Date"]');
- await waitUntilCalendarAppears(); // a test helper that uses waitUntil()
const dates = getAvailableDates();
await click(dates[3]);
assert.dom('[data-test-field="Date"]').hasValue('09.08.2024');
});
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for that explanation!
Background
After updating
ember-flatpickr
from version4.0.0
to8.0.0
, several rendering and application tests, which assert that theflatpickr
calendar works (e.g. the user can see the correct date initially, they can click the input to open the calendar and select a date), became flaky.I believe the reason is, we need translations for Germany to exist (for these assertions to pass) and have been passing the argument
@locale="de"
. Sinceember-flatpickr@>=7.1.0
uses dynamic import to load the translations, tests can fail when loading takes a while. We can see this issue happen by throttling the network when running the tests forember-flatpickr
.Solution
Wrap the dynamic
import()
withwaitForPromise()
from@ember/test-waiters
, so that tests know when they can resume with the next step.