A platform for community discussion. Free, open, simple.
DEV: Restore topic discovery footer test (#39462)
Jarek Radosz72c300a1510f
diff --git a/frontend/discourse/tests/acceptance/topic-discovery-test.js b/frontend/discourse/tests/acceptance/topic-discovery-test.jsindex 3de0039ad2f..77f9430361b 100644--- a/frontend/discourse/tests/acceptance/topic-discovery-test.js+++ b/frontend/discourse/tests/acceptance/topic-discovery-test.js@@ -1,5 +1,5 @@-import { click, currentURL, find, settled, visit } from "@ember/test-helpers";-import { skip, test } from "qunit";+import { click, currentURL, find, visit } from "@ember/test-helpers";+import { test } from "qunit";import {disableLoadMoreObserver,enableLoadMoreObserver,@@ -12,6 +12,7 @@ import {publishToMessageBus,} from "discourse/tests/helpers/qunit-helpers";import selectKit from "discourse/tests/helpers/select-kit-helper";+import stubIntersectionObserver from "discourse/tests/helpers/stub-intersection-observer";acceptance("Topic Discovery", function (needs) {needs.settings({@@ -181,31 +182,33 @@ acceptance("Topic Discovery", function (needs) {});acceptance("Topic Discovery | Footer", function (needs) {- needs.hooks.beforeEach(function () {- enableLoadMoreObserver();- });-- needs.hooks.afterEach(function () {- disableLoadMoreObserver();- });-needs.pretender((server, helper) => {server.get("/c/dev/7/l/latest.json", (request) => {const json = cloneJSON(discoveryFixtures["/c/dev/7/l/latest.json"]);- if (!request.queryParams.page) {+ if (request.queryParams.page) {+ delete json.topic_list.more_topics_url;+ } else {json.topic_list.more_topics_url = "/c/dev/7/l/latest.json?page=2";}return helper.response(json);});});- // TODO: Needs scroll support in tests- skip("No footer, then shows footer when all loaded", async function (assert) {- await visit("/c/dev");- assert.dom(".custom-footer-content").doesNotExist();+ test("No footer, then shows footer when all loaded", async function (assert) {+ enableLoadMoreObserver();+ const observations = stubIntersectionObserver();++ try {+ await visit("/c/dev");+ assert.dom(".custom-footer-content").doesNotExist();++ await observations+ .find(({ element }) => element.classList.contains("load-more-sentinel"))+ .trigger();- document.querySelector("#ember-testing-container").scrollTop = 100000; // scroll to bottom- await settled();- assert.dom(".custom-footer-content").exists();+ assert.dom(".custom-footer-content").exists();+ } finally {+ disableLoadMoreObserver();+ }});});diff --git a/frontend/discourse/tests/helpers/stub-intersection-observer.js b/frontend/discourse/tests/helpers/stub-intersection-observer.jsnew file mode 100644index 00000000000..0c763fd8744--- /dev/null+++ b/frontend/discourse/tests/helpers/stub-intersection-observer.js@@ -0,0 +1,40 @@+import { settled } from "@ember/test-helpers";+import sinon from "sinon";++/**+ * Stubs `IntersectionObserver` so tests can simulate intersection events.+ * The real one needs viewport scroll state, which the Ember testing container+ * can't reliably provide.+ *+ * @returns {Array<{element, trigger: (overrides?: object) => Promise<void>}>}+ * Entries appended on each `observe()`.+ * `await entry.trigger()` fires an intersection event.+ */+export default function stubIntersectionObserver() {+ const observations = [];++ sinon.stub(window, "IntersectionObserver").value(+ class {+ constructor(callback) {+ this.callback = callback;+ }++ observe(element) {+ observations.push({+ element,+ trigger: async (overrides = {}) => {+ this.callback([+ { target: element, isIntersecting: true, ...overrides },+ ]);+ await settled();+ },+ });+ }++ unobserve() {}+ disconnect() {}+ }+ );++ return observations;+}diff --git a/frontend/discourse/tests/integration/components/load-more-test.gjs b/frontend/discourse/tests/integration/components/load-more-test.gjsindex 4f23abc5bff..35815133073 100644--- a/frontend/discourse/tests/integration/components/load-more-test.gjs+++ b/frontend/discourse/tests/integration/components/load-more-test.gjs@@ -1,46 +1,21 @@-import { render, waitUntil } from "@ember/test-helpers";+import { render } from "@ember/test-helpers";import { module, test } from "qunit";import LoadMore, {disableLoadMoreObserver,enableLoadMoreObserver,} from "discourse/components/load-more";import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import stubIntersectionObserver from "discourse/tests/helpers/stub-intersection-observer";module("Integration | Component | load-more", function (hooks) {setupRenderingTest(hooks);hooks.beforeEach(function () {enableLoadMoreObserver();- this.originalIntersectionObserver = window.IntersectionObserver;-- window.IntersectionObserver = class MockIntersectionObserver {- callback;-- constructor(callback) {- this.callback = callback;- }-- observe(element) {- if (element) {- // Simulate intersection in next run loop- setTimeout(() => {- this.callback([- {- target: element,- isIntersecting: true,- },- ]);- }, 0);- }- }-- unobserve() {}- disconnect() {}- };+ this.observations = stubIntersectionObserver();});hooks.afterEach(function () {- window.IntersectionObserver = this.originalIntersectionObserver;disableLoadMoreObserver();});@@ -52,7 +27,7 @@ module("Integration | Component | load-more", function (hooks) {await render(<template>- <LoadMore @action={{performLoadMore}} @root="#ember-testing">+ <LoadMore @action={{performLoadMore}}><table class="numbers"><tbody><tr />@@ -62,13 +37,9 @@ module("Integration | Component | load-more", function (hooks) {</template>);- await waitUntil(() => actionCalled === 1);+ await this.observations[0]?.trigger();- assert.strictEqual(- actionCalled,- 1,- "loadMore action should be called once"- );+ assert.strictEqual(actionCalled, 1, "calls the loadMore action once");});test("does not call loadMore action if intersection occurs and this is not enabled", async function (assert) {@@ -79,11 +50,7 @@ module("Integration | Component | load-more", function (hooks) {await render(<template>- <LoadMore- @action={{performLoadMore}}- @root="#ember-testing"- @enabled={{false}}- >+ <LoadMore @action={{performLoadMore}} @enabled={{false}}><table class="numbers"><tbody><tr />@@ -93,11 +60,9 @@ module("Integration | Component | load-more", function (hooks) {</template>);- assert.strictEqual(- actionCalled,- 0,- "loadMore action should be called never"- );+ await this.observations[0]?.trigger();++ assert.strictEqual(actionCalled, 0, "does not call the loadMore action");});test("does not call loadMore action when isLoading is true", async function (assert) {@@ -106,11 +71,7 @@ module("Integration | Component | load-more", function (hooks) {await render(<template>- <LoadMore- @action={{loadMore}}- @root="#ember-testing"- @isLoading={{true}}- >+ <LoadMore @action={{loadMore}} @isLoading={{true}}><table class="numbers"><tbody><tr />@@ -120,13 +81,11 @@ module("Integration | Component | load-more", function (hooks) {</template>);- // Wait to ensure the observer callback would have fired if it was created- await new Promise((resolve) => setTimeout(resolve, 50));-assert.strictEqual(- actionCalled,+ this.observations.length,0,- "loadMore action should not be called when isLoading is true"+ "does not create an IntersectionObserver while loading");+ assert.strictEqual(actionCalled, 0, "does not call the loadMore action");});});