mirror of
https://github.com/discourse/discourse.git
synced 2024-11-21 08:34:17 -06:00
DEV: Allow afterFramePaint to be used in tests (#27231)
We need to register a waiter so that `settled()` will wait for `runAfterFramePaint()` callbacks to be run before proceeding.
Re-lands 63b7b598cb
, but wrapped with `isTesting()` to avoid production errors.
This commit is contained in:
parent
9b11e9a8d1
commit
4dbd072eae
@ -1,8 +1,18 @@
|
||||
import DEBUG from "@glimmer/env";
|
||||
import { registerWaiter } from "@ember/test";
|
||||
import { isTesting } from "discourse-common/config/environment";
|
||||
|
||||
/**
|
||||
* Runs `callback` shortly after the next browser Frame is produced.
|
||||
* ref: https://webperf.tips/tip/measuring-paint-time
|
||||
*/
|
||||
export default function runAfterFramePaint(callback) {
|
||||
let done = false;
|
||||
|
||||
if (DEBUG && isTesting()) {
|
||||
registerWaiter(() => done);
|
||||
}
|
||||
|
||||
// Queue a "before Render Steps" callback via requestAnimationFrame.
|
||||
requestAnimationFrame(() => {
|
||||
// MessageChannel is one of the highest priority task queues
|
||||
@ -10,7 +20,10 @@ export default function runAfterFramePaint(callback) {
|
||||
const messageChannel = new MessageChannel();
|
||||
|
||||
// Setup the callback to run in a Task
|
||||
messageChannel.port1.onmessage = callback;
|
||||
messageChannel.port1.onmessage = () => {
|
||||
done = true;
|
||||
callback();
|
||||
};
|
||||
|
||||
// Queue the Task on the Task Queue
|
||||
messageChannel.port2.postMessage(undefined);
|
||||
|
@ -0,0 +1,16 @@
|
||||
import { settled } from "@ember/test-helpers";
|
||||
import { setupTest } from "ember-qunit";
|
||||
import { module, test } from "qunit";
|
||||
import runAfterFramePaint from "discourse/lib/after-frame-paint";
|
||||
|
||||
module("Unit | Lib | afterFramePaint", function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test("should run callback correctly", async function (assert) {
|
||||
let callbackDone = false;
|
||||
runAfterFramePaint(() => (callbackDone = true));
|
||||
assert.false(callbackDone, "callback was not run immediately");
|
||||
await settled();
|
||||
assert.true(callbackDone, "callback was run before settled resolved");
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user