From ef37186be30fecef14670114fc4a2d9f18df6716 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 10 Jan 2022 15:45:44 +0000 Subject: [PATCH] DEV: Allow click-interceptor in tests and add navigation test (#15499) The app's wrapper element ID is different in tests. `app.rootElement` allows us to consistently obtain the selector in the initializer, so it works correctly regardless of the app's configuration. --- .../app/initializers/click-interceptor.js | 7 +++--- .../tests/acceptance/topic-discovery-test.js | 24 ++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/app/initializers/click-interceptor.js b/app/assets/javascripts/discourse/app/initializers/click-interceptor.js index 35eaa87be15..3219664f35b 100644 --- a/app/assets/javascripts/discourse/app/initializers/click-interceptor.js +++ b/app/assets/javascripts/discourse/app/initializers/click-interceptor.js @@ -3,8 +3,9 @@ import interceptClick from "discourse/lib/intercept-click"; export default { name: "click-interceptor", - initialize() { - $("#main").on("click.discourse", "a", interceptClick); + initialize(container, app) { + this.selector = app.rootElement; + $(this.selector).on("click.discourse", "a", interceptClick); window.addEventListener("hashchange", this.hashChanged); }, @@ -13,7 +14,7 @@ export default { }, teardown() { - $("#main").off("click.discourse", "a", interceptClick); + $(this.selector).off("click.discourse", "a", interceptClick); window.removeEventListener("hashchange", this.hashChanged); }, }; diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js index 0f7ad82e52e..539b90fbbf8 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js @@ -2,13 +2,14 @@ import { acceptance, exists, publishToMessageBus, + query, queryAll, } from "discourse/tests/helpers/qunit-helpers"; import DiscourseURL from "discourse/lib/url"; import selectKit from "discourse/tests/helpers/select-kit-helper"; import sinon from "sinon"; import { test } from "qunit"; -import { visit } from "@ember/test-helpers"; +import { click, currentURL, visit } from "@ember/test-helpers"; acceptance("Topic Discovery", function (needs) { needs.settings({ @@ -134,4 +135,25 @@ acceptance("Topic Discovery", function (needs) { "it keeps the query params" ); }); + + test("switching between tabs", async function (assert) { + await visit("/latest"); + assert.strictEqual( + query(".topic-list-body .topic-list-item:first-of-type").dataset.topicId, + "11557", + "shows the correct latest topics" + ); + + await click(".navigation-container a[href='/top']"); + assert.strictEqual(currentURL(), "/top", "switches to top"); + + assert.deepEqual( + query(".topic-list-body .topic-list-item:first-of-type").dataset.topicId, + "13088", + "shows the correct top topics" + ); + + await click(".navigation-container a[href='/categories']"); + assert.strictEqual(currentURL(), "/categories", "switches to categories"); + }); });