76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
|
|
module('Unit | Service | page-lifecycle', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
test('it exists', function (assert) {
|
|
assert.ok(this.owner.lookup('service:page-lifecycle'));
|
|
});
|
|
|
|
test('it marks the page as leaving on a real unload', function (assert) {
|
|
const service = this.owner.lookup('service:page-lifecycle');
|
|
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pagehide', { persisted: false })
|
|
);
|
|
|
|
assert.true(service.leaving);
|
|
});
|
|
|
|
test('a bfcache freeze is not treated as leaving', function (assert) {
|
|
const service = this.owner.lookup('service:page-lifecycle');
|
|
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pagehide', { persisted: true })
|
|
);
|
|
assert.false(service.leaving);
|
|
});
|
|
|
|
test('it notifies restore handlers on a persisted pageshow', function (assert) {
|
|
const service = this.owner.lookup('service:page-lifecycle');
|
|
|
|
let calls = 0;
|
|
service.onRestore(() => calls++);
|
|
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pageshow', { persisted: true })
|
|
);
|
|
assert.strictEqual(calls, 1);
|
|
|
|
// A fresh load is not a restore.
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pageshow', { persisted: false })
|
|
);
|
|
assert.strictEqual(calls, 1);
|
|
});
|
|
|
|
test('onRestore returns an unsubscribe function', function (assert) {
|
|
const service = this.owner.lookup('service:page-lifecycle');
|
|
|
|
let calls = 0;
|
|
const unsubscribe = service.onRestore(() => calls++);
|
|
|
|
unsubscribe();
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pageshow', { persisted: true })
|
|
);
|
|
|
|
assert.strictEqual(calls, 0);
|
|
});
|
|
|
|
test('it clears the leaving flag after a pageshow', function (assert) {
|
|
const service = this.owner.lookup('service:page-lifecycle');
|
|
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pagehide', { persisted: false })
|
|
);
|
|
assert.true(service.leaving);
|
|
|
|
window.dispatchEvent(
|
|
new PageTransitionEvent('pageshow', { persisted: false })
|
|
);
|
|
assert.false(service.leaving);
|
|
});
|
|
});
|