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

Tests for hard reload use case #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ async function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string

if (!hash) return;

await waitForPromise(mutationsSettled(owner));

await waitForPromise(uiSettled(owner));

if (isDestroyed(owner) || isDestroying(owner)) {
Expand All @@ -136,11 +138,15 @@ async function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string
scrollToHash(hash);
}

export async function uiSettled(owner: ApplicationInstance) {
// TODO: make sure that backburner queues are empty
}

const TIME_SINCE_LAST_MUTATION = 500; // ms
const MAX_TIMEOUT = 2000; // ms

// exported for testing
export async function uiSettled(owner: ApplicationInstance) {
export async function mutationsSettled(owner: ApplicationInstance) {
let timeStarted = new Date().getTime();
let lastMutationAt = Infinity;
let totalTimeWaited = 0;
Expand Down
36 changes: 32 additions & 4 deletions tests/integration/hash-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';

import { scrollToHash, uiSettled } from 'ember-url-hash-polyfill';
import { mutationsSettled, scrollToHash } from 'ember-url-hash-polyfill';

import { setupRouter } from './-helpers';

import type RouterService from '@ember/routing/router-service';
import { currentURL } from '@ember/test-helpers';

module('Hash', function (hooks) {
setupApplicationTest(hooks);
Expand Down Expand Up @@ -38,6 +39,33 @@ module('Hash', function (hooks) {
);
}

module('initial page load', function (_hooks) {
test('waits for app to settle', async function (assert) {
this.owner.register(
'template:application',
hbs`
<h1 id="first">first!</h1>
<div style="height: 100vh;"></div>

<h1 id="second">second!</h1>
<div style="height: 100vh;"></div>
`
);

await visit('/#second');

let container = document.querySelector('#ember-testing-container');
let first = find('#first');
let second = find('#second');

debugAssert(`Expected all test elements to exist`, container && first && second);

assert.false(isVisible(first, container), 'first header is not visible');
assert.true(isVisible(second, container), 'second header is visible');
assert.equal(currentURL(), '/#second', 'initially, preserves hash');
});
});

module('linking with hashes', function (_hooks) {
test('in-page-links can be scrolled to with native anchors', async function (assert) {
this.owner.register(
Expand Down Expand Up @@ -223,22 +251,22 @@ module('Hash', function (hooks) {
debugAssert(`Expected all test elements to exist`, container);

router.transitionTo('/foo');
await uiSettled(this.owner);
await mutationsSettled(this.owner);

assert.true(isVisible(find('#foo-first'), container), 'first header is visible');
assert.false(isVisible(find('#foo-second'), container), 'second header is not visible');
assert.equal(location.hash, '', 'initially, has no hash');

router.transitionTo('/bar#bar-second');
await uiSettled(this.owner);
await mutationsSettled(this.owner);
await scrollSettled();

assert.false(isVisible(find('#bar-first'), container), 'first header is not visible');
assert.true(isVisible(find('#bar-second'), container), 'second header is visible');
assert.equal(location.hash, '#bar-second', 'clicked hash appears in URL');

router.transitionTo('/foo#foo-second');
await uiSettled(this.owner);
await mutationsSettled(this.owner);
await scrollSettled();

assert.false(isVisible(find('#foo-first'), container), 'first header is not visible');
Expand Down