From 9389bf295cf0fbe7fd04a4c37bf8c869d6b5dc3e Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 3 Dec 2024 13:32:37 +0100 Subject: [PATCH] DEV: Make history-store work in non-accceptance tests (#30059) --- .../discourse/app/services/history-store.js | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/discourse/app/services/history-store.js b/app/assets/javascripts/discourse/app/services/history-store.js index a19051f215f..fce92bfa6eb 100644 --- a/app/assets/javascripts/discourse/app/services/history-store.js +++ b/app/assets/javascripts/discourse/app/services/history-store.js @@ -21,14 +21,10 @@ export default class HistoryStore extends Service { #routeData = new TrackedMap(); #uuid; - #pendingStore; + #pendingStore = DEBUG && isTesting() ? new TrackedMap() : null; get #currentStore() { - if (this.#pendingStore) { - return this.#pendingStore; - } - - return this.#dataFor(this.#uuid); + return this.#pendingStore || this.#dataFor(this.#uuid); } /** @@ -84,10 +80,7 @@ export default class HistoryStore extends Service { if (key === undefined) { continue; } - if (key === this.#uuid) { - return false; - } - return true; + return key !== this.#uuid; } } @@ -118,7 +111,7 @@ export default class HistoryStore extends Service { * not available as an event on the router service. */ @bind - willResolveModel(transition) { + async willResolveModel(transition) { if (HANDLED_TRANSITIONS.has(transition)) { return; } @@ -126,7 +119,6 @@ export default class HistoryStore extends Service { if (DEBUG && isTesting()) { // Can't use window.history in tests - this.#pendingStore = new TrackedMap(); return; } @@ -149,16 +141,16 @@ export default class HistoryStore extends Service { } this.#pendingStore = pendingStoreForThisTransition; - transition - .then(() => { - this.#uuid = window.history.state?.uuid; - this.#routeData.set(this.#uuid, this.#pendingStore); - this.#pruneOldData(); - }) - .finally(() => { - if (pendingStoreForThisTransition === this.#pendingStore) { - this.#pendingStore = null; - } - }); + + try { + await transition; + this.#uuid = window.history.state?.uuid; + this.#routeData.set(this.#uuid, this.#pendingStore); + this.#pruneOldData(); + } finally { + if (pendingStoreForThisTransition === this.#pendingStore) { + this.#pendingStore = null; + } + } } }