From 9a3a5b19e9d8768d2d7e294db01939f4dd4025cb Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 8 Oct 2021 17:59:44 +0100 Subject: [PATCH] DEV: Move duplicate `selectText` helper into qunit-helpers --- .../acceptance/topic-quote-button-test.js | 11 +-------- .../discourse/tests/acceptance/topic-test.js | 20 ++-------------- .../discourse/tests/helpers/qunit-helpers.js | 24 ++++++++++++++++++- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js index 7ddeca57d4b..d553538b457 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js @@ -3,20 +3,11 @@ import { chromeTest, exists, queryAll, + selectText, } from "discourse/tests/helpers/qunit-helpers"; import I18n from "I18n"; import { settled, visit } from "@ember/test-helpers"; -async function selectText(selector) { - const range = document.createRange(); - const node = document.querySelector(selector); - range.selectNodeContents(node); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - await settled(); -} // This tests are flaky on Firefox. Fails with `calling set on destroyed object` acceptance("Topic - Quote button - logged in", function (needs) { diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-test.js index 584989f3a94..a4f53804ba9 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-test.js @@ -5,31 +5,15 @@ import { exists, query, queryAll, + selectText, visible, } from "discourse/tests/helpers/qunit-helpers"; -import { - click, - fillIn, - settled, - triggerKeyEvent, - visit, -} from "@ember/test-helpers"; +import { click, fillIn, triggerKeyEvent, visit } from "@ember/test-helpers"; import I18n from "I18n"; import selectKit from "discourse/tests/helpers/select-kit-helper"; import { test } from "qunit"; import { withPluginApi } from "discourse/lib/plugin-api"; -async function selectText(selector) { - const range = document.createRange(); - const node = document.querySelector(selector); - range.selectNodeContents(node); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - await settled(); -} - acceptance("Topic", function (needs) { needs.user(); needs.pretender((server, helper) => { diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index 8841b86e6c1..76a0889bace 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -10,7 +10,7 @@ import { mergeSettings, } from "discourse/tests/helpers/site-settings"; import { forceMobile, resetMobile } from "discourse/lib/mobile"; -import { getApplication, getContext } from "@ember/test-helpers"; +import { getApplication, getContext, settled } from "@ember/test-helpers"; import { getOwner, setDefaultOwner } from "discourse-common/lib/get-owner"; import { later, run } from "@ember/runloop"; import { moduleFor, setupApplicationTest } from "ember-qunit"; @@ -479,6 +479,28 @@ export function publishToMessageBus(channelPath, ...args) { .map((c) => c.func(...args)); } +export async function selectText(selector, endOffset = null) { + const range = document.createRange(); + let node; + + if (typeof selector === "string") { + node = document.querySelector(selector); + } else { + node = selector; + } + + range.selectNodeContents(node); + + if (endOffset) { + range.setEnd(node, endOffset); + } + + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(range); + await settled(); +} + export function conditionalTest(name, condition, testCase) { if (condition) { test(name, testCase);