Skip to content

Commit 106991f

Browse files
committed
Add additional async/await tests: literals, new, destructure :or
1 parent dfdf091 commit 106991f

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

src/test/cljs/cljs/async_await_test.cljs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(ns cljs.async-await-test
2+
(:refer-global :only [Date Promise])
23
(:require [clojure.test :refer [deftest is async]]
3-
[cljs.core :as cc :refer [await] :rename {await aw}])
4+
[cljs.core :as cc :refer [await] :rename {await aw}]
5+
[goog.object :as gobj])
46
(:require-macros [cljs.macro-test.macros :refer [await!] :as macros]))
57

68
(defn ^:async foo [n]
@@ -192,9 +194,9 @@
192194
(let [x (await (js/Promise.resolve 1))] x)))
193195
b3 (case :foo :foo (case :foo :foo (await (js/Promise.resolve 1))))
194196
b4 (int ;; wrapped in int to avoid false positive warning:
195-
;; all arguments must be numbers, got [number
196-
;; ignore] instead
197-
(try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 )))
197+
;; all arguments must be numbers, got [number
198+
;; ignore] instead
199+
(try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 )))
198200
a (atom 0)
199201
b5 (do (swap! a inc) (swap! a inc)
200202
;; do with single expr, wrapped in identity to avoid merging with upper do
@@ -229,3 +231,46 @@
229231
(is (= 1 f))
230232
(done))
231233
(catch :default _ (is false)))))
234+
235+
(deftest await-with-ctor
236+
(async done
237+
(let [f (^:async fn [] (Date. (await (Promise/resolve 0))))]
238+
(is (= (Date. 0) (await (f)))))
239+
(done)))
240+
241+
(deftest await-with-literals
242+
(async done
243+
(let [objf (^:async fn [] #js {:foo (await (Promise/resolve "bar"))})]
244+
(is (gobj/equals #js {:foo "bar"} (await (objf)))))
245+
(let [arrayf (^:async fn [] #js [0 (await (Promise/resolve 1 )) 2])]
246+
(is (= [0 1 2] (seq (await (arrayf))))))
247+
(let [listf (^:async fn [] (list 0 1 2))]
248+
(is (= '(0 1 2) (await (listf)))))
249+
(let [vectorf (^:async fn [] [0 (await (Promise/resolve 1 )) 2])]
250+
(is (= [0 1 2] (await (vectorf)))))
251+
(let [mapf (^:async fn [] {:foo (await (Promise/resolve :bar))})]
252+
(is (= {:foo :bar} (await (mapf)))))
253+
(let [setf (^:async fn [] #{:foo (await (Promise/resolve :bar)) :baz})]
254+
(is (= #{:foo :bar :baz} (await (setf)))))
255+
(done)))
256+
257+
(deftest await-with-if-test
258+
(async done
259+
(let [f (^:async fn [] (if (await (Promise/resolve true)) :success :fail))]
260+
(is (= :success (await (f)))))
261+
(done)))
262+
263+
(defn ^:async async-destructure
264+
[{:keys [foo bar]
265+
:or {bar (await (Promise/resolve "hello!"))}}]
266+
[foo bar])
267+
268+
(deftest await-in-async-destructure
269+
(async done
270+
(let [res (await (async-destructure {:foo 1}))]
271+
(is (= [1 "hello!"] res)))
272+
(done)))
273+
274+
(comment
275+
(clojure.test/run-tests)
276+
)

0 commit comments

Comments
 (0)