-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cljs
More file actions
67 lines (56 loc) · 2.75 KB
/
Copy pathtest.cljs
File metadata and controls
67 lines (56 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(ns test
(:require ["./fleck_sdk.mjs" :as sdk]
["@esm-bundle/chai" :refer [expect]]))
(defn- make-container []
(let [el (.createElement js/document "div")]
(set! (.-id el) "fleck-test-container")
(.appendChild (.-body js/document) el)
el))
(defn- dispatch-message! [data origin source]
(let [event (js/MessageEvent. "message" #js {:data data
:origin origin
:source source})]
(.dispatchEvent js/window event)))
(defn- wait-tick []
(js/Promise. (fn [resolve _] (js/setTimeout resolve 0))))
(js/describe "Fleck Partner SDK"
(fn []
(js/it "resolves on fleck:ready and posts auth message"
(fn []
(js/Promise.
(fn [resolve reject]
(try
(let [sdk (.-fleckSdk sdk)
container (make-container)
init-promise (.init sdk #js {:container container
:src "https://fleck.unlockit.io/app"})
iframe (.querySelector container "iframe")]
(.equal (.-to (expect (not (nil? iframe)))) true)
;; Wrong message (ignored)
(dispatch-message! #js {:type "ready"}
"https://fleck.unlockit.io"
(.-contentWindow iframe))
;; Correct ready message
(dispatch-message! #js {:type "fleck:ready"}
"https://fleck.unlockit.io"
(.-contentWindow iframe))
(.then init-promise
(fn [fleck]
(let [last-message (atom nil)
content-window (.-contentWindow iframe)]
(set! (.-postMessage content-window)
(fn [data origin]
(reset! last-message {:data data :origin origin})))
(.auth fleck #js {:jwt "token-123"})
(.then (wait-tick)
(fn []
(try
(.equal (.-to (expect (-> @last-message :data (aget "type")))) "fleck:auth")
(.equal (.-to (expect (-> @last-message :data (aget "token")))) "token-123")
(.equal (.-to (expect (:origin @last-message))) "https://fleck.unlockit.io")
(resolve)
(catch :default e
(reject e)))))))
(fn [e] (reject e))))
(catch :default e
(reject e)))))))))