mirror of
https://github.com/penpot/penpot.git
synced 2026-11-19 16:40:38 +00:00
210 lines
10 KiB
Clojure
210 lines
10 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 SUBSIDIARY SL
|
|
|
|
(ns frontend-tests.data.common-test
|
|
"Every team-scoped navigation carries a team.
|
|
|
|
`team-container*` raises `:not-found :invalid-team-id` for a dashboard
|
|
or workspace route without a valid `team-id`, and `:not-found` is not
|
|
reported to the error pipeline, so a navigation that loses the id ends
|
|
on the not-found page without leaving a trace. The helpers resolve the
|
|
id from the argument, the initialized team, the team of the route
|
|
already on screen, and the profile default, in that order."
|
|
(:require
|
|
[app.common.types.team :as ctt]
|
|
[app.common.uuid :as uuid]
|
|
[app.main.data.common :as dcm]
|
|
[app.main.router :as rt]
|
|
[beicon.v2.core :as rx]
|
|
[cljs.test :as t :include-macros true]
|
|
[frontend-tests.helpers.async :as async]
|
|
[potok.v2.core :as ptk]))
|
|
|
|
(def ^:private file-id (uuid/next))
|
|
|
|
(defn- state
|
|
"A slice of the store with the teams a navigation can resolve. The
|
|
route is the one a team screen leaves behind: its query names the team
|
|
the user is looking at."
|
|
[{:keys [current-team-id route-team-id default-team-id]}]
|
|
(cond-> {:profile {:id (uuid/next)}}
|
|
(some? current-team-id)
|
|
(assoc :current-team-id current-team-id)
|
|
|
|
(some? default-team-id)
|
|
(assoc-in [:profile :default-team-id] default-team-id)
|
|
|
|
(some? route-team-id)
|
|
(assoc :route {:data {:name :dashboard-recent}
|
|
:params {:path {} :query {:team-id (str route-team-id)}}
|
|
:query-params {:team-id (str route-team-id)}})))
|
|
|
|
(defn- ^:async emitted
|
|
"Runs `event` against `state` and returns every event it emitted, once
|
|
the event's stream terminates.
|
|
|
|
The stream is a subject the caller of `ptk/watch` never sees: `go-to-
|
|
dashboard-search` keeps listening to the router after navigating and
|
|
completes only when it receives one `::rt/navigated`, which is the event
|
|
it waits for in the app to focus the search box. Pushing it here makes
|
|
that helper terminate like the rest, so every event of the family is
|
|
measured the same way, subscribe and await termination.
|
|
|
|
`observe` forces asynchronous delivery even for a synchronous source and
|
|
rejects when the stream does not terminate in time, so a helper that
|
|
stops closing its stream fails here instead of passing on the first
|
|
event."
|
|
[event state]
|
|
(let [stream (rx/subject)
|
|
events (atom [])
|
|
done (async/observe (ptk/watch event state stream)
|
|
:on-next #(swap! events conj %))]
|
|
;; `observe` subscribes inside its promise executor, so the subject
|
|
;; already has a listener; a subject drops values nobody hears.
|
|
(rx/push! stream (rt/navigated {:data {:name :dashboard-search}} false))
|
|
(await done)
|
|
@events))
|
|
|
|
(defn- nav-params
|
|
"The params of the last navigation to `route-id`, or nil when the event
|
|
emitted none."
|
|
[events route-id]
|
|
(some->> events
|
|
(filter #(and (= ::rt/navigate (ptk/type %))
|
|
(= route-id (:id @%))))
|
|
(last)
|
|
(deref)
|
|
(:params)))
|
|
|
|
(t/deftest ^:async go-to-workspace-takes-the-team-of-the-route-on-screen
|
|
;; The opened file belongs to the team of the route. The personal
|
|
;; default would open it under another team.
|
|
(let [route-team-id (uuid/next)
|
|
default-team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-workspace :file-id file-id)
|
|
(state {:route-team-id route-team-id
|
|
:default-team-id default-team-id})))]
|
|
(t/is (= route-team-id (:team-id (nav-params events :workspace))))))
|
|
|
|
(t/deftest ^:async go-to-workspace-takes-the-team-initialized-in-the-store
|
|
(let [current-team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-workspace :file-id file-id)
|
|
(state {:current-team-id current-team-id
|
|
:route-team-id (uuid/next)
|
|
:default-team-id (uuid/next)})))]
|
|
(t/is (= current-team-id (:team-id (nav-params events :workspace))))))
|
|
|
|
(t/deftest ^:async go-to-workspace-takes-the-profile-default-when-no-team-is-known
|
|
(let [default-team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-workspace :file-id file-id)
|
|
(state {:default-team-id default-team-id})))]
|
|
(t/is (= default-team-id (:team-id (nav-params events :workspace))))))
|
|
|
|
(t/deftest ^:async go-to-workspace-keeps-the-team-the-caller-passes
|
|
(let [team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-workspace :team-id team-id :file-id file-id)
|
|
(state {:current-team-id (uuid/next)
|
|
:route-team-id (uuid/next)
|
|
:default-team-id (uuid/next)})))]
|
|
(t/is (= team-id (:team-id (nav-params events :workspace))))))
|
|
|
|
(t/deftest ^:async go-to-dashboard-recent-keeps-the-default-sentinel
|
|
(let [default-team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-dashboard-recent :team-id :default)
|
|
(state {:current-team-id (uuid/next)
|
|
:route-team-id (uuid/next)
|
|
:default-team-id default-team-id})))]
|
|
(t/is (= default-team-id (:team-id (nav-params events :dashboard-recent))))))
|
|
|
|
(t/deftest ^:async go-to-dashboard-deleted-keeps-the-default-sentinel
|
|
;; The sentinel is a second copy of the branch in `-recent`; without its
|
|
;; own test, a divergence here would navigate to the current team.
|
|
(let [default-team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-dashboard-deleted :team-id :default)
|
|
(state {:current-team-id (uuid/next)
|
|
:route-team-id (uuid/next)
|
|
:default-team-id default-team-id})))]
|
|
(t/is (= default-team-id (:team-id (nav-params events :dashboard-deleted))))))
|
|
|
|
(t/deftest ^:async go-to-dashboard-recent-resolves-a-nil-team-through-the-route
|
|
(let [route-team-id (uuid/next)
|
|
events (await (emitted (dcm/go-to-dashboard-recent)
|
|
(state {:route-team-id route-team-id
|
|
:default-team-id (uuid/next)})))]
|
|
(t/is (= route-team-id (:team-id (nav-params events :dashboard-recent))))))
|
|
|
|
(def ^:private dashboard-helpers
|
|
"The rest of the family, with the route each one navigates to."
|
|
[[dcm/go-to-dashboard-files :dashboard-files]
|
|
[dcm/go-to-dashboard-search :dashboard-search]
|
|
[dcm/go-to-dashboard-libraries :dashboard-libraries]
|
|
[dcm/go-to-dashboard-fonts :dashboard-fonts]
|
|
[dcm/go-to-dashboard-members :dashboard-members]
|
|
[dcm/go-to-dashboard-invitations :dashboard-invitations]
|
|
[dcm/go-to-dashboard-webhooks :dashboard-webhooks]
|
|
[dcm/go-to-dashboard-settings :dashboard-settings]
|
|
[dcm/go-to-dashboard-deleted :dashboard-deleted]])
|
|
|
|
(t/deftest ^:async dashboard-helpers-take-the-team-of-the-route-on-screen
|
|
(doseq [[helper route-id] dashboard-helpers]
|
|
(let [route-team-id (uuid/next)
|
|
events (await (emitted (helper)
|
|
(state {:route-team-id route-team-id
|
|
:default-team-id (uuid/next)})))]
|
|
(t/is (= route-team-id (:team-id (nav-params events route-id)))
|
|
(str "route: " route-id)))))
|
|
|
|
(t/deftest ^:async dashboard-helpers-take-the-profile-default-when-no-team-is-known
|
|
(doseq [[helper route-id] dashboard-helpers]
|
|
(let [default-team-id (uuid/next)
|
|
events (await (emitted (helper)
|
|
(state {:default-team-id default-team-id})))]
|
|
(t/is (= default-team-id (:team-id (nav-params events route-id)))
|
|
(str "route: " route-id)))))
|
|
|
|
(t/deftest ^:async navigations-still-fire-when-no-team-can-be-resolved
|
|
;; Documented last resort: a profile without a default team is a broken
|
|
;; or transient state, and there is nothing to resolve. The navigation
|
|
;; still fires, so the not-found page appears with its exit in the
|
|
;; header instead of the click doing nothing.
|
|
(let [events (await (emitted (dcm/go-to-dashboard-members) (state {})))]
|
|
(t/is (some? (nav-params events :dashboard-members)))
|
|
(t/is (nil? (:team-id (nav-params events :dashboard-members))))))
|
|
|
|
;; --- Team role changes
|
|
|
|
(defn- role-state
|
|
"A session inside `current-team-id` with editor rights in it and in
|
|
`other-team-id`."
|
|
[current-team-id other-team-id]
|
|
(let [editor (get ctt/permissions-for-role :editor)]
|
|
{:current-team-id current-team-id
|
|
:permissions editor
|
|
:team {:id current-team-id :permissions editor}
|
|
:teams {current-team-id {:permissions editor}
|
|
other-team-id {:permissions editor}}}))
|
|
|
|
(t/deftest change-team-role-of-another-team-keeps-the-session-permissions
|
|
(let [current-team-id (uuid/next)
|
|
other-team-id (uuid/next)
|
|
editor (get ctt/permissions-for-role :editor)
|
|
state (ptk/update (dcm/change-team-role {:team-id other-team-id :role :viewer})
|
|
(role-state current-team-id other-team-id))]
|
|
(t/is (= editor (:permissions state)))
|
|
(t/is (= editor (get-in state [:team :permissions])))
|
|
(t/is (= editor (get-in state [:teams current-team-id :permissions])))
|
|
(t/is (false? (get-in state [:teams other-team-id :permissions :can-edit])))))
|
|
|
|
(t/deftest change-team-role-of-the-current-team-limits-the-session
|
|
(let [current-team-id (uuid/next)
|
|
other-team-id (uuid/next)
|
|
state (ptk/update (dcm/change-team-role {:team-id current-team-id :role :viewer})
|
|
(role-state current-team-id other-team-id))]
|
|
(t/is (false? (get-in state [:permissions :can-edit])))
|
|
(t/is (false? (get-in state [:team :permissions :can-edit])))
|
|
(t/is (false? (get-in state [:teams current-team-id :permissions :can-edit])))
|
|
(t/is (true? (get-in state [:teams other-team-id :permissions :can-edit])))))
|