DEV: Move duplicate selectText helper into qunit-helpers

This commit is contained in:
David Taylor 2021-10-08 17:59:44 +01:00
parent 9efc57f0c6
commit 9a3a5b19e9
3 changed files with 26 additions and 29 deletions

View File

@ -3,20 +3,11 @@ import {
chromeTest, chromeTest,
exists, exists,
queryAll, queryAll,
selectText,
} from "discourse/tests/helpers/qunit-helpers"; } from "discourse/tests/helpers/qunit-helpers";
import I18n from "I18n"; import I18n from "I18n";
import { settled, visit } from "@ember/test-helpers"; 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` // This tests are flaky on Firefox. Fails with `calling set on destroyed object`
acceptance("Topic - Quote button - logged in", function (needs) { acceptance("Topic - Quote button - logged in", function (needs) {

View File

@ -5,31 +5,15 @@ import {
exists, exists,
query, query,
queryAll, queryAll,
selectText,
visible, visible,
} from "discourse/tests/helpers/qunit-helpers"; } from "discourse/tests/helpers/qunit-helpers";
import { import { click, fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";
click,
fillIn,
settled,
triggerKeyEvent,
visit,
} from "@ember/test-helpers";
import I18n from "I18n"; import I18n from "I18n";
import selectKit from "discourse/tests/helpers/select-kit-helper"; import selectKit from "discourse/tests/helpers/select-kit-helper";
import { test } from "qunit"; import { test } from "qunit";
import { withPluginApi } from "discourse/lib/plugin-api"; 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) { acceptance("Topic", function (needs) {
needs.user(); needs.user();
needs.pretender((server, helper) => { needs.pretender((server, helper) => {

View File

@ -10,7 +10,7 @@ import {
mergeSettings, mergeSettings,
} from "discourse/tests/helpers/site-settings"; } from "discourse/tests/helpers/site-settings";
import { forceMobile, resetMobile } from "discourse/lib/mobile"; 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 { getOwner, setDefaultOwner } from "discourse-common/lib/get-owner";
import { later, run } from "@ember/runloop"; import { later, run } from "@ember/runloop";
import { moduleFor, setupApplicationTest } from "ember-qunit"; import { moduleFor, setupApplicationTest } from "ember-qunit";
@ -479,6 +479,28 @@ export function publishToMessageBus(channelPath, ...args) {
.map((c) => c.func(...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) { export function conditionalTest(name, condition, testCase) {
if (condition) { if (condition) {
test(name, testCase); test(name, testCase);