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

bugfix: Waited for dynamic import to resolve #2100

Merged
merged 3 commits into from
Aug 6, 2024
Merged

bugfix: Waited for dynamic import to resolve #2100

merged 3 commits into from
Aug 6, 2024

Conversation

ijlee2
Copy link
Contributor

@ijlee2 ijlee2 commented Aug 6, 2024

Background

After updating ember-flatpickr from version 4.0.0 to 8.0.0, several rendering and application tests, which assert that the flatpickr 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". Since ember-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 for ember-flatpickr.

Solution

Wrap the dynamic import() with waitForPromise() from @ember/test-waiters, so that tests know when they can resume with the next step.

@ijlee2 ijlee2 marked this pull request as ready for review August 6, 2024 08:02
@@ -475,8 +474,6 @@ module('Integration | Component | ember flatpickr', function (hooks) {
placeholder="Pick date"
/>`);

await waitFor('.flatpickr-current-month .flatpickr-monthDropdown-month');
Copy link
Contributor Author

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();
Copy link
Contributor Author

@ijlee2 ijlee2 Aug 6, 2024

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(
Copy link
Owner

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.

Copy link
Contributor Author

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');
  });
});

Copy link
Owner

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!

@RobbieTheWagner RobbieTheWagner merged commit 6aafbf4 into RobbieTheWagner:main Aug 6, 2024
10 checks passed
@github-actions github-actions bot mentioned this pull request Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants