From c80ab08e48f01669dd360105cada13fa6547ef03 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 28 May 2019 10:40:27 +0800 Subject: [PATCH] DEV: Fix pretenders leaking across acceptance test. --- .../acceptance/tag-hashtag-test.js.es6 | 4 ---- .../acceptance/topic-edit-timer-test.js.es6 | 7 ------ test/javascripts/acceptance/topic-test.js.es6 | 12 ---------- .../helpers/create-pretender.js.es6 | 23 +++++++++++++++++++ test/javascripts/helpers/qunit-helpers.js.es6 | 9 ++++---- test/javascripts/test_helper.js | 17 ++++++++------ 6 files changed, 38 insertions(+), 34 deletions(-) diff --git a/test/javascripts/acceptance/tag-hashtag-test.js.es6 b/test/javascripts/acceptance/tag-hashtag-test.js.es6 index 29b0b40085a..1e3e468277b 100644 --- a/test/javascripts/acceptance/tag-hashtag-test.js.es6 +++ b/test/javascripts/acceptance/tag-hashtag-test.js.es6 @@ -4,10 +4,6 @@ acceptance("Tag Hashtag", { loggedIn: true, settings: { tagging_enabled: true }, pretend(server, helper) { - server.get("/tags/filter/search", () => { - return helper.response({ results: [{ text: "monkey", count: 1 }] }); - }); - server.get("/tags/check", () => { return helper.response({ valid: [{ value: "monkey", url: "/tags/monkey" }] diff --git a/test/javascripts/acceptance/topic-edit-timer-test.js.es6 b/test/javascripts/acceptance/topic-edit-timer-test.js.es6 index 45219755065..af8ab7e774b 100644 --- a/test/javascripts/acceptance/topic-edit-timer-test.js.es6 +++ b/test/javascripts/acceptance/topic-edit-timer-test.js.es6 @@ -14,13 +14,6 @@ acceptance("Topic - Edit timer", { category_id: null }) ); - - server.put("/t/internationalization-localization/280/status", () => - helper.response({ - success: "OK", - topic_status_update: null - }) - ); } }); diff --git a/test/javascripts/acceptance/topic-test.js.es6 b/test/javascripts/acceptance/topic-test.js.es6 index 23dbf05ab11..67f4cae3e42 100644 --- a/test/javascripts/acceptance/topic-test.js.es6 +++ b/test/javascripts/acceptance/topic-test.js.es6 @@ -7,18 +7,6 @@ acceptance("Topic", { server.put("/posts/398/wiki", () => { return helper.response({}); }); - - server.get("/topics/feature_stats.json", () => { - return helper.response({ - pinned_in_category_count: 0, - pinned_globally_count: 0, - banner_count: 0 - }); - }); - - server.put("/t/280/make-banner", () => { - return helper.response({}); - }); } }); diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index 9837a53aff9..64a70ccdb92 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -85,6 +85,10 @@ export default function() { }); }); + this.get("/tags/filter/search", () => { + return response({ results: [{ text: "monkey", count: 1 }] }); + }); + this.get(`/u/:username/emails.json`, () => { return response({ email: "eviltrout@example.com" }); }); @@ -138,6 +142,25 @@ export default function() { return response({ topic_list: { topics: [] } }); }); + this.get("/topics/feature_stats.json", () => { + return response({ + pinned_in_category_count: 0, + pinned_globally_count: 0, + banner_count: 0 + }); + }); + + this.put("/t/280/make-banner", () => { + return response({}); + }); + + this.put("/t/internationalization-localization/280/status", () => { + return response({ + success: "OK", + topic_status_update: null + }); + }); + this.post("/clicks/track", success); this.get("/search", request => { diff --git a/test/javascripts/helpers/qunit-helpers.js.es6 b/test/javascripts/helpers/qunit-helpers.js.es6 index 7799e087bc4..652d3d1da6e 100644 --- a/test/javascripts/helpers/qunit-helpers.js.es6 +++ b/test/javascripts/helpers/qunit-helpers.js.es6 @@ -57,17 +57,18 @@ function AcceptanceModal(option, _relatedTarget) { window.bootbox.$body = $("#ember-testing"); $.fn.modal = AcceptanceModal; -let _pretenderCallbacks = []; +let _pretenderCallbacks = {}; -export function applyPretender(server, helper) { - _pretenderCallbacks.forEach(cb => cb(server, helper)); +export function applyPretender(name, server, helper) { + const cb = _pretenderCallbacks[name]; + if (cb) cb(server, helper); } export function acceptance(name, options) { options = options || {}; if (options.pretend) { - _pretenderCallbacks.push(options.pretend); + _pretenderCallbacks[name] = options.pretend; } QUnit.module("Acceptance: " + name, { diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js index d94a88f2fb0..bbfaac9d90d 100644 --- a/test/javascripts/test_helper.js +++ b/test/javascripts/test_helper.js @@ -88,7 +88,8 @@ var origDebounce = Ember.run.debounce, _DiscourseURL = require("discourse/lib/url", null, null, false).default, applyPretender = require("helpers/qunit-helpers", null, null, false) .applyPretender, - server; + server, + acceptanceModulePrefix = "Acceptance: "; function dup(obj) { return jQuery.extend(true, {}, obj); @@ -105,13 +106,15 @@ function resetSite(siteSettings, extras) { QUnit.testStart(function(ctx) { server = pretender.default(); - var helper = { - parsePostData: pretender.parsePostData, - response: pretender.response, - success: pretender.success - }; + if (ctx.module.startsWith(acceptanceModulePrefix)) { + var helper = { + parsePostData: pretender.parsePostData, + response: pretender.response, + success: pretender.success + }; - applyPretender(server, helper); + applyPretender(ctx.module.replace(acceptanceModulePrefix, ""), server, helper); + } // Allow our tests to change site settings and have them reset before the next test Discourse.SiteSettings = dup(Discourse.SiteSettingsOriginal);