penpot/frontend/test/frontend_tests/ui/routes_test.cljs
Alonso Torres 4ac14cfd08
Add component synchronization to waitForLayoutUpdate (#10964)
*  Add component synchronization to waitForLayoutUpdate

* 🐛 Fix async mock leak in workspace-reflow-test

Use mock/with-mocks instead of with-redefs for http/send! mock in
failed-google-font-css-does-not-abort-shared-consumers test.

with-redefs restores bindings when the block exits synchronously,
but the RxJS subscription fires asynchronously. This caused the mock
to leak into subsequent tests (workspace-media-test), producing 3
spurious failures.

AI-assisted-by: mimo-v2.5-pro

* ♻️ Replace async with-redefs with mock/with-mocks in frontend tests

with-redefs restores bindings when the block exits synchronously,
which is too early for async code (t/async, rx/subs!, promises).
mock/with-mocks uses set! and restores in the done callback, keeping
mocks alive across async boundaries.

Converted 15 with-redefs usages across 4 test files:
- workspace_reflow_test.cljs: 2 genuinely async tests (P1)
- routes_test.cljs: 3 SSO caching tests (P2)
- main_errors_test.cljs: 8 expired-org SSO tests (P2)
- comments_test.cljs: 2 comment thread tests (P2)

36 purely sync with-redefs usages left unchanged — with-redefs
is correct for synchronous code.

AI-assisted-by: mimo-v2.5-pro

* 📎 Fix fmt issues

---------

Co-authored-by: Andrey Antukh <niwi@niwi.nz>
2026-08-18 17:45:42 +02:00

98 lines
3.6 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC Sucursal en España SL
(ns frontend-tests.ui.routes-test
(:require
[app.common.time :as ct]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.main.repo :as rp]
[app.main.store :as st]
[app.main.ui.routes :as routes]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.mock :as mock]))
(defn- workspace-match
[team-id]
{:data {:name :workspace}
:params {:path {}}
:query-params {:team-id (str team-id)}})
(t/deftest sso-check-is-cached-for-five-minutes
(t/async done
(let [team-id (uuid/next)
match (workspace-match team-id)
now (atom (ct/inst "2026-08-11T10:00:00Z"))
rpc-calls (atom 0)
events (atom [])]
(mock/with-mocks
{cf/flags (conj cf/flags :admin-console)
ct/now (mock/stub (fn [] @now))
rp/cmd! (mock/stub
(fn [command params]
(t/is (= :check-nitrate-sso command))
(t/is (= team-id (:team-id params)))
(swap! rpc-calls inc)
(rx/of {:authorized true})))
st/emit! (mock/stub
(fn [& emitted]
(swap! events into emitted)))}
(fn [done']
(#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace")
(reset! now (ct/plus @now #js {:minutes 4 :seconds 59}))
(#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace")
(t/is (= 1 @rpc-calls))
(t/is (= 2 (count @events)))
(done'))
done))))
(t/deftest sso-check-is-refreshed-after-five-minutes
(t/async done
(let [team-id (uuid/next)
match (workspace-match team-id)
now (atom (ct/inst "2026-08-11T10:00:00Z"))
rpc-calls (atom 0)]
(mock/with-mocks
{cf/flags (conj cf/flags :admin-console)
ct/now (mock/stub (fn [] @now))
rp/cmd! (mock/stub
(fn [_ _]
(swap! rpc-calls inc)
(rx/of {:authorized true})))
st/emit! mock/noop}
(fn [done']
(#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace")
(reset! now (ct/plus @now #js {:minutes 5}))
(#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace")
(t/is (= 2 @rpc-calls))
(done'))
done))))
(t/deftest sso-redirect-result-is-not-cached
(t/async done
(let [team-id (uuid/next)
match (workspace-match team-id)
rpc-calls (atom 0)
events (atom [])]
(mock/with-mocks
{cf/flags (conj cf/flags :admin-console)
rp/cmd! (mock/stub
(fn [_ _]
(swap! rpc-calls inc)
(rx/of {:authorized false
:redirect-uri "https://idp.example.com/authorize"})))
st/emit! (mock/stub
(fn [& emitted]
(swap! events into emitted)))}
(fn [done']
(#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace")
(#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace")
(t/is (= 2 @rpc-calls))
(t/is (= 2 (count @events)))
(done'))
done))))