forked from fulcrologic/fulcro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives_spec.cljc
1870 lines (1690 loc) · 84.4 KB
/
primitives_spec.cljc
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns fulcro.client.primitives-spec
(:require [fulcro-spec.core :refer [specification behavior assertions provided component when-mocking]]
[fulcro.client.primitives :as prim :refer [defui defsc]]
[fulcro.history :as hist]
[fulcro.client.dom :as dom]
[fulcro-css.css]
[clojure.spec.alpha :as s]
[clojure.core.async :as async]
fulcro.client
[clojure.spec.gen.alpha :as gen]
[clojure.spec.test.alpha :as check]
[clojure.test.check.properties :as prop]
[fulcro.client.mutations :as m :refer [defmutation]]
[fulcro.client.data-fetch :as df]
[clojure.test.check :as tc]
[clojure.spec.test.alpha :as check]
[clojure.test :refer [is are]]
#?@(:cljs [[goog.object :as gobj]])
[fulcro.client.impl.protocols :as p]
[fulcro.util :as util]
[clojure.walk :as walk])
#?(:clj
(:import (clojure.lang ExceptionInfo))))
(defn read-times [m]
(walk/postwalk
(fn [x]
(if-let [time (some-> x meta ::prim/time)]
(-> (into {} (filter (fn [[_ v]] (or (vector? v)
(and (map? v) (contains? v ::time))))) x)
(assoc ::time time))
x))
m))
(specification "Add basis time"
(assertions
"Add time information recursively"
(-> (prim/add-basis-time {:hello {:world {:data 2}
:foo :bar
:multi [{:x 1} {:x 2}]}} 10)
(read-times))
=> {:hello {:world {::time 10}
:multi [{::time 10} {::time 10}]
::time 10}
::time 10}
"Limit the reach when query is provided"
(-> (prim/add-basis-time [{:hello [{:world [:item]}
{:multi [:x]}]}]
{:hello {:world {:data 2
:item [{:nested {:deep "bar"}}]}
:so {:long {:and "more"}}
:foo :bar
:multi [{:x 1} {:x 2}]}} 10)
(read-times))
=> {:hello {:world {:item [{::time 10}]
::time 10}
:multi [{::time 10} {::time 10}]
::time 10}
::time 10}
"Out of query data is kept as is"
(prim/add-basis-time [{:hello [{:world [:item]}
:query-only
{:multi [:x]}]}]
{:hello {:world {:data 2
:item [{:nested {:deep "bar"}}]}
:so {:long {:and "more"}}
:foo :bar
:multi [{:x 1} {:x 2}]}} 10)
=> {:hello {:world {:data 2
:item [{:nested {:deep "bar"}}]}
:so {:long {:and "more"}}
:foo :bar
:multi [{:x 1} {:x 2}]}}
"Handle unions"
(-> (prim/add-basis-time [{:hello [{:union {:ua [:x]
:ub [:y]}}]}]
{:hello {:union {:x {:content "bar"}
:type :ua}}} 10)
(read-times))
=> {:hello {:union {:x {::time 10}
::time 10}
::time 10}
::time 10}
"Handle recursive queries"
(-> (prim/add-basis-time [{:hello [{:parent '...}
:name]}]
{:hello {:name "bla"
:parent {:name "meh"
:parent {:name "other"}}}} 10)
(read-times))
=> {:hello {:parent {:parent {::time 10}
::time 10}
::time 10}
::time 10}
"Handle recursive queries with limit"
(-> (prim/add-basis-time [{:hello [{:parent 1}
:name]}]
{:hello {:name "bla"
:parent {:name "meh"
:parent {:name "other"
:parent {:name "one more"}}}}} 10)
(read-times))
=> {:hello {:parent {:parent {::time 10}
::time 10}
::time 10}
::time 10}
"Meta data is preserved"
(-> (prim/add-basis-time [{:hello [{:world [:item]}
{:multi [:x]}]}]
^::info {:hello {:world {:data 2
:item [{:nested {:deep "bar"}}]}
:so {:long {:and "more"}}
:foo :bar
:multi [{:x 1} {:x 2}]}} 10)
(meta))
=> {::prim/time 10 ::info true}))
(defui A)
(specification "Query IDs"
(assertions
"Start with the fully-qualifier class name"
(prim/query-id A nil) => "fulcro$client$primitives_spec$A"
"Include the optional qualifier"
(prim/query-id A :x) => "fulcro$client$primitives_spec$A$:x"))
(specification "UI Factory"
(assertions
"Adds react-class to the metadata of the generated factory"
(some-> (prim/factory A) meta :class) => A
"Adds an optional qualifier to the metadata of the generated factory"
(some-> (prim/factory A) meta :qualifier) => nil
(some-> (prim/factory A {:qualifier :x}) meta :qualifier) => :x)
(behavior "Adds an internal query id to the props passed by the factory"
#?(:cljs
(when-mocking
(prim/query-id c q) => :ID
(prim/create-element class props children) => (do
(assertions
(gobj/get props "fulcro$queryid") => :ID))
((prim/factory A) {}))
:clj
(let [class (fn [_ _ props _] (assertions (:fulcro$queryid props) => :ID))]
(when-mocking
(prim/query-id c q) => :ID
(prim/init-local-state c) => nil
((prim/factory class) {}))))))
(defui Q
static prim/IQuery
(query [this] [:a :b]))
(def ui-q (prim/factory Q))
(defui UnionChildA
static prim/Ident
(ident [this props] [:union-child/by-id (:L props)])
static prim/IQuery
(query [this] [:L]))
(def ui-a (prim/factory UnionChildA))
(defui UnionChildB
static prim/IQuery
(query [this] [:M]))
(def ui-b (prim/factory UnionChildB))
(defui Union
static prim/IQuery
(query [this] {:u1 (prim/get-query ui-a)
:u2 (prim/get-query ui-b)}))
(def ui-union (prim/factory Union))
(defui Child
static prim/IQuery
(query [this] [:x]))
(def ui-child (prim/factory Child))
(defui Root
static prim/IQuery
(query [this] [:a
{:join (prim/get-query ui-child)}
{:union (prim/get-query ui-union)}]))
(def ui-root (prim/factory Root))
(defui UnionChildAP
static prim/IQuery
(query [this] '[(:L {:child-params 1})]))
(def ui-ap (prim/factory UnionChildAP))
(defui UnionP
static prim/IQuery
(query [this] {:u1 (prim/get-query ui-ap)
:u2 (prim/get-query ui-b)}))
(def ui-unionp (prim/factory UnionP))
(defui RootP
static prim/IQuery
(query [this] `[:a
({:join ~(prim/get-query ui-child)} {:join-params 2})
({:union ~(prim/get-query ui-unionp)} {:union-params 3})]))
(def ui-rootp (prim/factory RootP))
(specification "link-query"
(assertions
"Replaces nested queries with their string ID"
(prim/link-query (prim/get-query ui-root {})) => [:a {:join (prim/query-id Child nil)} {:union (prim/query-id Union nil)}]))
(specification "normalize-query"
(let [union-child-a-id (prim/query-id UnionChildA nil)
union-child-b-id (prim/query-id UnionChildB nil)
child-id (prim/query-id Child nil)
root-id (prim/query-id Root nil)
union-id (prim/query-id Union nil)
existing-query {:id union-child-a-id
:query [:OTHER]}]
(assertions
"Adds simple single-level queries into app state under a reserved key"
(prim/normalize-query {} (prim/get-query ui-a {})) => {::prim/queries {union-child-a-id
{:id union-child-a-id
:query [:L]}}}
"Single-level queries are not added if a query is already set in state"
(prim/normalize-query {::prim/queries {union-child-a-id existing-query}} (prim/get-query ui-a {})) => {::prim/queries {union-child-a-id existing-query}}
"More complicated queries normalize correctly"
(prim/normalize-query {} (prim/get-query ui-root {}))
=> {::prim/queries {root-id {:id root-id
:query [:a {:join child-id} {:union union-id}]}
union-id {:id union-id
:query {:u1 union-child-a-id :u2 union-child-b-id}}
union-child-b-id {:id union-child-b-id
:query [:M]}
union-child-a-id {:id union-child-a-id
:query [:L]}
child-id {:query [:x]
:id child-id}}})))
(specification "get-query*"
(assertions
"Obtains the static query from a given class"
(prim/get-query Q) => [:a :b]
"Obtains the static query when the state has no stored queries"
(prim/get-query ui-q {}) => [:a :b])
(let [query (prim/get-query ui-root {})
top-level query
join-target (get-in query [1 :join])
union-target (get-in query [2 :union])
union-left (get union-target :u1)
union-right (get union-target :u2)]
(assertions
"Places a query ID on the metadata of each component's portion of the query"
(-> top-level meta :queryid) => "fulcro$client$primitives_spec$Root"
(-> join-target meta :queryid) => "fulcro$client$primitives_spec$Child"
(-> union-target meta :queryid) => "fulcro$client$primitives_spec$Union"
(-> union-left meta :queryid) => "fulcro$client$primitives_spec$UnionChildA"
(-> union-right meta :queryid) => "fulcro$client$primitives_spec$UnionChildB"))
(let [app-state (prim/normalize-query {} (prim/get-query ui-root {}))
app-state (assoc-in app-state [::prim/queries (prim/query-id UnionChildA nil) :query] [:UPDATED])]
(behavior "Pulls a denormalized query from app state if one exists."
(assertions
(prim/get-query ui-root app-state) => [:a {:join [:x]} {:union {:u1 [:UPDATED] :u2 [:M]}}]))
(behavior "Allows a class instead of a factory"
(assertions
"with state"
(prim/get-query Root app-state) => [:a {:join [:x]} {:union {:u1 [:UPDATED] :u2 [:M]}}]
"without state (raw static query)"
(prim/get-query Root) => [:a {:join [:x]} {:union {:u1 [:L] :u2 [:M]}}]))))
(specification "Normalization preserves query"
(let [query (prim/get-query ui-root {})
parameterized-query (prim/get-query ui-rootp {})
state (prim/normalize-query {} query)
state-parameterized (prim/normalize-query {} parameterized-query)]
(assertions
"When parameters are not present"
(prim/get-query ui-root state) => query
"When parameters are present"
(prim/get-query ui-rootp state) => parameterized-query)))
; TODO: This would be a great property-based check (marshalling/unmarshalling) if we had generators that would work...
(specification "Setting a query"
(let [query (prim/get-query ui-root {})
parameterized-query (prim/get-query ui-rootp {})
state (prim/normalize-query {} query)
state-modified (prim/set-query* state ui-b {:query [:MODIFIED]})
expected-query (assoc-in query [2 :union :u2 0] :MODIFIED)
state-modified-root (prim/set-query* state Root {:query [:b
{:join (prim/get-query ui-child state)}
{:union (prim/get-query ui-union state)}]})
queryid (prim/query-id Root nil)
state-queryid-modified-root (prim/set-query* state queryid {:query [:b
{:join (prim/get-query ui-child state)}
{:union (prim/get-query ui-union state)}]})
expected-root-query (assoc query 0 :b)
state-parameterized (prim/normalize-query {} parameterized-query)]
(assertions
"Can update a node by factory"
(prim/get-query ui-root state-modified) => expected-query
"Can update a node by class"
(prim/get-query ui-root state-modified-root) => expected-root-query
"Can be done directly by ID"
(prim/get-query ui-root state-queryid-modified-root) => expected-root-query)))
(specification "Indexing"
(component "Gathering keys for a query"
(assertions
"finds the correct prop keys (without parameters)"
(prim/gather-keys (prim/get-query ui-root {})) => #{:a :join :union}
(prim/gather-keys (prim/get-query ui-union {})) => #{:u1 :u2}
(prim/gather-keys (prim/get-query ui-a {})) => #{:L}
(prim/gather-keys (prim/get-query ui-b {})) => #{:M}
(prim/gather-keys (prim/get-query ui-child {})) => #{:x}
"finds the correct prop keys (with parameters)"
(prim/gather-keys (prim/get-query ui-rootp {})) => #{:a :join :union}
(prim/gather-keys (prim/get-query ui-ap {})) => #{:L}
(prim/gather-keys (prim/get-query ui-unionp {})) => #{:u1 :u2}))
(component "Indexer"
(let [indexer (prim/map->Indexer {:indexes (atom {})})
indexer (assoc indexer :state {})]
(p/index-root indexer Root)
(assertions
"Properly indexes components"
(-> indexer :indexes deref :prop->classes :L) => #{UnionChildA}
(-> indexer :indexes deref :prop->classes :M) => #{UnionChildB}
(-> indexer :indexes deref :prop->classes :a) => #{Root}
(-> indexer :indexes deref :prop->classes :join) => #{Root}
(-> indexer :indexes deref :prop->classes :union) => #{Root}))
(let [indexer (prim/map->Indexer {:indexes (atom {})})
indexer (assoc indexer :state {})]
(p/index-root indexer RootP)
(assertions
"Properly indexes components that have parameterized queries"
(-> indexer :indexes deref :prop->classes :L) => #{UnionChildAP}
(-> indexer :indexes deref :prop->classes :M) => #{UnionChildB}
(-> indexer :indexes deref :prop->classes :a) => #{RootP}
(-> indexer :indexes deref :prop->classes :join) => #{RootP}
(-> indexer :indexes deref :prop->classes :union) => #{RootP}))
(let [indexer (prim/map->Indexer {:indexes (atom {})})
id [:union-child/by-id 1]
id-2 [:union-child/by-id 2]
element (ui-a {:L 1})
element-2 (ui-a {:L 2})]
(p/index-component! indexer element)
(p/index-component! indexer element-2)
(let [ident-elements (-> indexer :indexes deref :ref->components (get id))
class-elements (-> indexer :indexes deref :class->components (get UnionChildA))
expected-ident #{element}
expected-by-class #{element element-2}]
(assertions
;"Adds component idents to the ref->components index when indexing the component"
;; FIXME: Not sure I can...Shows up as broken on cljs, but this may be due to the fact that the component isn't mounted
;(count ident-elements) => 1 ; js objects that cannot be printed...crash spec
"Adds the component to the index of :class->components"
(count class-elements) => 2)))))
(specification "gather-sends"
(behavior "Runs the parser against the given remotes and:"
(let [q [:some-query]
saw-remotes (atom [])
parser (fn [env query remote]
(swap! saw-remotes conj remote)
(assertions
"passes the environment to the parser"
(contains? env :parser) => true
"passes the query to the parser"
query => q)
(get {:a '[(do-a-thing)] :b '[(do-b-thing)]} remote))
env {:parser parser}
result (prim/gather-sends env q [:a :b] 1000)]
(assertions
"Returns a map with keys for each remote's actions"
result => {:a '[(do-a-thing)] :b '[(do-b-thing)]}
"Includes the history timestamp on each entry"
(-> result :a meta ::hist/tx-time) => 1000
(-> result :b meta ::hist/tx-time) => 1000))))
(specification "gather-keys"
(assertions
"Can gather the correct keys from simple props"
(prim/gather-keys [:a :b :c]) => #{:a :b :c}
"Gather keys for joins, but does not recurse."
(prim/gather-keys [:a {:b [:d]} :c]) => #{:a :b :c}
"Allow parameterized props"
(prim/gather-keys '[(:a {:x 1}) {:b [:d]} :c]) => #{:a :b :c}
"Includes keywords from link queries"
(prim/gather-keys [:a [:x '_]]) => #{:a :x}
(prim/gather-keys [:a {[:x '_] [:prop]}]) => #{:a :x}))
(specification "building prop->classes index"
(let [index-atom (atom {})]
(prim/build-prop->class-index! index-atom (prim/get-query Root))
(assertions
"Includes entries the correct keys"
(set (keys @index-atom)) => #{:a :join :union :x :u1 :u2 :L :M}
"The component class count is correct"
(count (get @index-atom :a)) => 1
(count (get @index-atom :join)) => 1
(count (get @index-atom :u1)) => 1
(count (get @index-atom :u2)) => 1
(count (get @index-atom :L)) => 1
(count (get @index-atom :M)) => 1
(count (get @index-atom :union)) => 1
)))
(specification "remove-loads-and-fallbacks"
(behavior "Removes top-level mutations that use the fulcro/load or tx/fallback symbols"
(are [q q2] (= (prim/remove-loads-and-fallbacks q) q2)
'[:a {:j [:a]} (f) (fulcro/load {:x 1}) (app/l) (tx/fallback {:a 3})] '[:a {:j [:a]} (f) (app/l)]
'[(fulcro/load {:x 1}) (app/l) (tx/fallback {:a 3})] '[(app/l)]
'[(fulcro/load {:x 1}) (tx/fallback {:a 3})] '[]
'[(boo {:x 1}) (fulcro.client.data-fetch/fallback {:a 3})] '[(boo {:x 1})]
'[:a {:j [:a]}] '[:a {:j [:a]}])))
(specification "fallback-query"
(behavior "extracts the fallback expressions of a query, adds execute flags, and includes errors in params"
(are [q q2] (= (prim/fallback-query q {:error 42}) q2)
'[:a :b] nil
'[:a {:j [:a]} (f) (fulcro/load {:x 1}) (app/l) (tx/fallback {:a 3})]
'[(tx/fallback {:a 3 :execute true :error {:error 42}})]
'[:a {:j [:a]} (tx/fallback {:b 4}) (f) (fulcro/load {:x 1}) (app/l) (fulcro.client.data-fetch/fallback {:a 3})]
'[(tx/fallback {:b 4 :execute true :error {:error 42}}) (fulcro.client.data-fetch/fallback {:a 3 :execute true :error {:error 42}})])))
(specification "tempid handling"
(behavior "rewrites all tempids used in pending requests in the request queue"
(let [queue (async/chan 10000)
tid1 (prim/tempid)
tid2 (prim/tempid)
tid3 (prim/tempid)
rid1 4
rid2 2
rid3 42
tid->rid {tid1 rid1
tid2 rid2
tid3 rid3}
q (fn [id] {:query `[(app/thing {:id ~id})]})
expected-result [(q rid1) (q rid2) (q rid3)]
results (atom [])]
(async/offer! queue (q tid1))
(async/offer! queue (q tid2))
(async/offer! queue (q tid3))
(prim/rewrite-tempids-in-request-queue queue tid->rid)
(swap! results conj (async/poll! queue))
(swap! results conj (async/poll! queue))
(swap! results conj (async/poll! queue))
(is (nil? (async/poll! queue)))
(is (= expected-result @results))))
(let [tid (prim/tempid)
tid2 (prim/tempid)
rid 1
state {:thing {tid {:id tid}
tid2 {:id tid2}} ; this one isn't in the remap, and should not be touched
:things [[:thing tid]]}
expected-state {:thing {rid {:id rid}
tid2 {:id tid2}}
:things [[:thing rid]]}
reconciler (prim/reconciler {:state state :parser {:read (constantly nil)} :migrate prim/resolve-tempids})]
(assertions
"rewrites all tempids in the app state (leaving unmapped ones alone)"
((-> reconciler :config :migrate) @reconciler {tid rid}) => expected-state)))
(specification "strip-ui"
(let [q1 [:username :password :ui/login-dropdown-showing {:forgot-password [:email :ui/forgot-button-showing]}]
q2 [:username :password :ui.login/dropdown-showing {:forgot-password [:email :ui.forgot/button-showing]}]
result [:username :password {:forgot-password [:email]}]]
(assertions
"removes keywords with a ui namespace"
(prim/strip-ui q1) => result
"removes keywords with a ui.{something} namespace"
(prim/strip-ui q2) => result))
(let [query '[(app/x {:ui/boo 23})]]
(assertions
"does not remove ui prefixed data from parameters"
(prim/strip-ui query) => query)))
(specification "mark-missing"
(behavior "correctly marks missing properties"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[:a :b]
{:a 1}
{:a 1 :b prim/nf}))
(behavior "joins -> one"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[:a {:b [:c]}]
{:a 1}
{:a 1 :b prim/nf}
[{:b [:c]}]
{:b {}}
{:b {:c prim/nf}}
[{:b [:c]}]
{:b {:c 0}}
{:b {:c 0}}
[{:b [:c :d]}]
{:b {:c 1}}
{:b {:c 1 :d prim/nf}}))
(behavior "join -> many"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[{:a [:b :c]}]
{:a [{:b 1 :c 2} {:b 1}]}
{:a [{:b 1 :c 2} {:b 1 :c prim/nf}]}))
(behavior "idents and ident joins"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[{[:a 1] [:x]}]
{[:a 1] {}}
{[:a 1] {:x prim/nf}}
[{[:b 1] [:x]}]
{[:b 1] {:x 2}}
{[:b 1] {:x 2}}
[{[:c 1] [:x]}]
{}
{[:c 1] {:ui/fetch-state {:fulcro.client.impl.data-fetch/type :not-found}
:x prim/nf}}
[{[:e 1] [:x :y :z]}]
{}
{[:e 1] {:ui/fetch-state {:fulcro.client.impl.data-fetch/type :not-found}
:x prim/nf
:y prim/nf
:z prim/nf}}
[[:d 1]]
{}
{[:d 1] {:ui/fetch-state {:fulcro.client.impl.data-fetch/type :not-found}}}))
(behavior "parameterized"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
'[:z (:y {})]
{:z 1}
{:z 1 :y prim/nf}
'[:z (:y {})]
{:z 1 :y 0}
{:z 1 :y 0}
'[:z ({:y [:x]} {})]
{:z 1 :y {}}
{:z 1 :y {:x prim/nf}}))
(behavior "nested"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[{:b [:c {:d [:e]}]}]
{:b {:c 1}}
{:b {:c 1 :d prim/nf}}
[{:b [:c {:d [:e]}]}]
{:b {:c 1 :d {}}}
{:b {:c 1 :d {:e prim/nf}}}))
(behavior "upgrades value to maps if necessary"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[{:l [:m]}]
{:l 0}
{:l {:m prim/nf}}
[{:b [:c]}]
{:b nil}
{:b {:c prim/nf}}))
(behavior "unions"
(assertions
"singletons"
(prim/mark-missing {:j {:c {}}} [{:j {:a [:c] :b [:d]}}]) => {:j {:c {} :d prim/nf}}
"singleton with no result"
(prim/mark-missing {} [{:j {:a [:c] :b [:d]}}]) => {:j prim/nf}
"list to-many with 1"
(prim/mark-missing {:j [{:c "c"}]} [{:j {:a [:c] :b [:d]}}]) => {:j [{:c "c" :d prim/nf}]}
"list to-many with 2"
(prim/mark-missing {:items [{:id 0 :image "img1"} {:id 1 :text "text1"}]} [{:items {:photo [:id :image] :text [:id :text]}}]) => {:items [{:id 0 :image "img1" :text prim/nf} {:id 1 :image prim/nf :text "text1"}]}
"list to-many with no results"
(prim/mark-missing {:j []} [{:j {:a [:c] :b [:d]}}]) => {:j []}))
(behavior "if the query has a ui.*/ attribute, it should not be marked as missing"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[:a :ui/b :c]
{:a {}
:c {}}
{:a {}
:c {}}
[{:j [:ui/b :c]}]
{:j {:c 5}}
{:j {:c 5}}
[{:j [{:ui/b [:d]} :c]}]
{:j {:c 5}}
{:j {:c 5}}))
(behavior "mutations!"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
'[(f) {:j [:a]}]
{'f {}
:j {}}
{'f {}
:j {:a prim/nf}}
'[(app/add-q {:p 1}) {:j1 [:p1]} {:j2 [:p2]}]
{'app/add-q {:tempids {}}
:j1 {}
:j2 [{:p2 2} {}]}
{'app/add-q {:tempids {}}
:j1 {:p1 prim/nf}
:j2 [{:p2 2} {:p2 prim/nf}]}))
(behavior "correctly walks recursive queries to mark missing data"
(behavior "when the recursive target is a singleton"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[:a {:b '...}]
{:a 1 :b {:a 2}}
{:a 1 :b {:a 2 :b prim/nf}}
[:a {:b '...}]
{:a 1 :b {:a 2 :b {:a 3}}}
{:a 1 :b {:a 2 :b {:a 3 :b prim/nf}}}
[:a {:b 9}]
{:a 1 :b {:a 2 :b {:a 3 :b {:a 4}}}}
{:a 1 :b {:a 2 :b {:a 3 :b {:a 4 :b prim/nf}}}}))
(behavior "when the recursive target is to-many"
(are [query ?missing-result exp]
(= exp (prim/mark-missing ?missing-result query))
[:a {:b '...}]
{:a 1 :b [{:a 2 :b [{:a 3}]}
{:a 4}]}
{:a 1 :b [{:a 2 :b [{:a 3 :b prim/nf}]}
{:a 4 :b prim/nf}]})))
(behavior "marks leaf data based on the query where"
(letfn [(has-leaves [leaf-paths] (fn [result] (every? #(prim/leaf? (get-in result %)) leaf-paths)))]
(assertions
"plain data is always a leaf"
(prim/mark-missing {:a 1 :b {:x 5}} [:a {:b [:x]}]) =fn=> (has-leaves [[:b :x] [:a] [:missing]])
"data structures are properly marked in singleton results"
(prim/mark-missing {:b {:x {:data 1}}} [{:b [:x :y]}]) =fn=> (has-leaves [[:b :x]])
"data structures are properly marked in to-many results"
(prim/mark-missing {:b [{:x {:data 1}} {:x {:data 2}}]} [{:b [:x]}]) =fn=> (has-leaves [[:b 0 :x] [:b 1 :x]])
(prim/mark-missing {:b []} [:a {:b [:x]}]) =fn=> (has-leaves [[:b]])
"unions are followed"
(prim/mark-missing {:a [{:x {:data 1}} {:y {:data 2}}]} [{:a {:b [:x] :c [:y]}}]) =fn=> (has-leaves [[:a 0 :x] [:a 1 :y]])
"unions leaves data in place when the result is empty"
(prim/mark-missing {:a 1} [:a {:z {:b [:x] :c [:y]}}]) =fn=> (has-leaves [[:a]])))))
(specification "Sweep one"
(assertions
"removes not-found values from maps"
(prim/sweep-one {:a 1 :b ::prim/not-found}) => {:a 1}
"removes tempids from maps"
(prim/sweep-one {::prim/tempids {1 2} :tempids {3 4}}) => {}
"is not recursive"
(prim/sweep-one {:a 1 :b {:c ::prim/not-found}}) => {:a 1 :b {:c ::prim/not-found}}
"maps over vectors not recursive"
(prim/sweep-one [{:a 1 :b ::prim/not-found}]) => [{:a 1}]
"retains metadata"
(-> (prim/sweep-one (with-meta {:a 1 :b ::prim/not-found} {:meta :data}))
meta) => {:meta :data}
(-> (prim/sweep-one [(with-meta {:a 1 :b ::prim/not-found} {:meta :data})])
first meta) => {:meta :data}
(-> (prim/sweep-one (with-meta [{:a 1 :b ::prim/not-found}] {:meta :data}))
meta) => {:meta :data}))
(specification "Sweep merge"
(assertions
"recursively merges maps"
(prim/sweep-merge {:a 1 :c {:b 2}} {:a 2 :c 5}) => {:a 2 :c 5}
(prim/sweep-merge {:a 1 :c {:b 2}} {:a 2 :c {:x 1}}) => {:a 2 :c {:b 2 :x 1}}
"stops recursive merging if the source element is marked as a leaf"
(prim/sweep-merge {:a 1 :c {:d {:x 2} :e 4}} {:a 2 :c (prim/as-leaf {:d {:x 1}})}) => {:a 2 :c {:d {:x 1}}}
"sweeps tempids from maps"
(prim/sweep-merge {:a 1 :c {:b 2}} {:a 2 :tempids {} :c {::prim/tempids {} :b ::prim/not-found}}) => {:a 2 :c {}}
"sweeps values that are marked as not found"
(prim/sweep-merge {:a 1 :c {:b 2}} {:a 2 :c {:b ::prim/not-found}}) => {:a 2 :c {}}
(prim/sweep-merge {:a 1 :c 2} {:a 2 :c {:b ::prim/not-found}}) => {:a 2 :c {}}
(prim/sweep-merge {:a 1 :c 2} {:a 2 :c [{:x 1 :b ::prim/not-found}]}) => {:a 2 :c [{:x 1}]}
(prim/sweep-merge {:a 1 :c {:data-fetch :loading}} {:a 2 :c [{:x 1 :b ::prim/not-found}]}) => {:a 2 :c [{:x 1}]}
(prim/sweep-merge {:a 1 :c nil} {:a 2 :c [{:x 1 :b ::prim/not-found}]}) => {:a 2 :c [{:x 1}]}
(prim/sweep-merge {:a 1 :b {:c {:ui/fetch-state {:post-mutation 's}}}} {:a 2 :b {:c [{:x 1 :b ::prim/not-found}]}}) => {:a 2 :b {:c [{:x 1}]}}
"clears normalized table entries that has an id of not found"
(prim/sweep-merge {:table {1 {:a 2}}} {:table {::prim/not-found {:db/id ::prim/not-found}}}) => {:table {1 {:a 2}}}
"clears idents whose ids were not found"
(prim/sweep-merge {} {:table {1 {:db/id 1 :the-thing [:table-1 ::prim/not-found]}}
:thing [:table-2 ::prim/not-found]}) => {:table {1 {:db/id 1}}}
"sweeps not-found values from normalized table merges"
(prim/sweep-merge {:subpanel [:dashboard :panel]
:dashboard {:panel {:view-mode :detail :surveys {:ui/fetch-state {:post-mutation 's}}}}
}
{:subpanel [:dashboard :panel]
:dashboard {:panel {:view-mode :detail :surveys [[:s 1] [:s 2]]}}
:s {
1 {:db/id 1, :survey/launch-date ::prim/not-found}
2 {:db/id 2, :survey/launch-date "2012-12-22"}
}}) => {:subpanel [:dashboard :panel]
:dashboard {:panel {:view-mode :detail :surveys [[:s 1] [:s 2]]}}
:s {
1 {:db/id 1}
2 {:db/id 2 :survey/launch-date "2012-12-22"}
}}
"overwrites target (non-map) value if incoming value is a map"
(prim/sweep-merge {:a 1 :c 2} {:a 2 :c {:b 1}}) => {:a 2 :c {:b 1}}))
(specification "merge*"
(when-mocking
(prim/merge-novelty! r s res q) => {:next true}
(let [result `{:data 33 f {:tempids {1 2}} g {::prim/tempids {3 4}}}
{:keys [keys next ::prim/tempids]} (prim/merge* :reconciler (atom {}) result [])]
(assertions
"Finds all of the tempid remappings"
tempids => {1 2 3 4}
"gives back the next state for the app"
next => {:next true}
"Finds all of the data keys on the response"
keys => [:data]))))
(specification "Merge handler"
(let [swept-state {:state 1}
data-response {:v 1}
mutation-response {'f {:x 1 ::prim/tempids {1 2}} 'g {:tempids {3 4} :y 2}}
mutation-response-without-tempids (-> mutation-response
(update 'f dissoc ::prim/tempids)
(update 'g dissoc :tempids))
response (merge data-response mutation-response)
rh (fn [state k v]
(assertions
"return handler is passed the swept state as a map"
state => swept-state
"tempids are stripped from return value before calling handler"
(contains? v :tempids) => false
(contains? v ::prim/tempids) => false)
(vary-meta state assoc k v))]
(when-mocking
(prim/sweep-merge t s) => (do
(assertions
"Passes source, cleaned of symbols, to sweep-merge"
s => {:v 1})
swept-state)
(let [actual (prim/merge-handler rh {} response)]
(assertions
"Returns the swept state reduced over the return handlers"
;; Function under test:
actual => swept-state
(meta actual) => mutation-response-without-tempids)))))
(defsc Item [this {:keys [db/id item/value]}]
{:query [:db/id :item/value]
:ident [:item/by-id :db/id]}
(dom/li nil value))
(defsc ItemList [this {:keys [db/id list/title list/items] :as props}]
{:query [:db/id :list/title {:list/items (prim/get-query Item)}]
:ident [:list/by-id :db/id]}
(dom/div nil (dom/h3 nil title)))
(specification "Mutation joins"
(let [q [{'(f {:p 1}) (prim/get-query ItemList)}]
d {'f {:db/id 1 :list/title "A" :list/items [{:db/id 1 :item/value "1"}]}}
result (prim/merge-mutation-joins {:top-key 1} q d)
existing-db {:item/by-id {1 {:db/id 1 :item/value "1"}}}
missing-data {'f {:db/id 1 :list/title "A" :list/items [{:db/id 1}]}}
result-2 (prim/merge-mutation-joins existing-db q missing-data)]
(assertions
"mutation responses are merged"
result => {:top-key 1
:list/by-id {1 {:db/id 1 :list/title "A" :list/items [[:item/by-id 1]]}}
:item/by-id {1 {:db/id 1 :item/value "1"}}}
"mutation responses do proper sweep merge"
result-2 => {:list/by-id {1 {:db/id 1 :list/title "A" :list/items [[:item/by-id 1]]}}
:item/by-id {1 {:db/id 1}}}))
(let [mj {'(f {:p 1}) [:a]}]
(assertions
"Detects mutation joins as joins"
(util/join? mj) => true
"give the correct join key for mutation joins"
(util/join-key mj) => 'f
"give the correct join value for mutation joins"
(util/join-value mj) => [:a]))
(let [q [{'(f {:p 1}) (vary-meta (prim/get-query ItemList) assoc :fulcro.client.impl.data-fetch/target [:ui/root])}]
d {'f {:db/id 1 :list/title "A" :list/items [{:db/id 1 :item/value "1"}]}}
result (prim/merge-mutation-joins {:top-key 1} q d)]
(assertions
"mutation responses are merged and placed on target"
result => {:top-key 1
:ui/root [:list/by-id 1]
:list/by-id {1 {:db/id 1 :list/title "A" :list/items [[:item/by-id 1]]}}
:item/by-id {1 {:db/id 1 :item/value "1"}}}))
(let [q [{'(f {:p 1}) ^{:fulcro.client.impl.data-fetch/target [:ui/root]} ['*]}]
d {'f {:response "data"}}
result (prim/merge-mutation-joins {:top-key 1} q d)
q-2 [{'(f {:p 1}) ^{:fulcro.client.impl.data-fetch/target
(df/multiple-targets [:ui/new-root]
(df/append-to [:ui/list])
(df/prepend-to [:ui/list2]))} ['*]}]
result-2 (prim/merge-mutation-joins {:top-key 1
:ui/list [:x :y]
:ui/list2 [:a :b]} q-2 d)]
(assertions
"result is placed on target"
result => {:top-key 1
:ui/root {:response "data"}}
"uses the process target algorithm"
result-2 => {:top-key 1
:ui/new-root {:response "data"}
:ui/list [:x :y {:response "data"}]
:ui/list2 [{:response "data"} :a :b]})))
(defmutation f [params]
(action [env] true)
(remote [env] true))
(defmutation g [params]
(action [env] true)
(rest-remote [env] true))
(defmutation h [params]
(action [env] true))
(defmethod m/mutate `unhappy-mutation [env _ params]
(throw (ex-info "Boo!" {})))
(specification "pessimistic-transaction->transaction"
(assertions
"Returns the transaction if it only contains a single call"
(prim/pessimistic-transaction->transaction nil `[(f {:x 1})]) => `[(f {:x 1})]
"Includes the follow-on reads in a single-call"
(prim/pessimistic-transaction->transaction nil `[(f {:y 2}) :read]) => `[(f {:y 2}) :read]
"Converts a sequence of calls to the proper nested structure, deferring against the correct remotes"
(prim/pessimistic-transaction->transaction nil `[(f) (g) (h)])
=> `[(f)
(df/deferred-transaction {:remote :remote
:ref nil
:tx [(g) (df/deferred-transaction
{:remote :rest-remote
:ref nil
:tx [(h)]})]})]
"Spread the ref across the calls"
(prim/pessimistic-transaction->transaction [:id 123] `[(f) (g) (h)])
=> `[(f)
(df/deferred-transaction {:remote :remote
:ref [:id 123]
:tx [(g) (df/deferred-transaction
{:remote :rest-remote
:ref [:id 123]
:tx [(h)]})]})]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; query spec, with generators for property-based tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(comment
(defn anything? [x] true)
(def om-keyword? (s/with-gen
keyword?
#(s/gen #{:a :b :c :d :e :f :g :h})))
(def anything-gen #(s/gen #{"hello" 45 99.99 'abc -1 0 1 -1.0 0.0 1.0}))
(s/def ::ident-expr (s/tuple om-keyword? (s/with-gen anything? anything-gen)))
(def recur-gen #(s/gen #{'... 4 9 11}))
(def om-ident? (s/with-gen
::ident-expr
#(s/gen #{[:x 1] [:y "hello"] [:boo/other 22] [:poo nil]})))
(def keyword-or-ident?
(s/with-gen
(s/or :keyword om-keyword? :ident om-ident?)
#(s/gen #{[:x 1] [:poo nil] [:b 44] :a :b :c :d})))
(s/def ::union-expr (s/with-gen (s/map-of om-keyword? ::query-root :gen-max 1 :count 1)
#(gen/fmap (fn [v] (with-meta v {:queryid (futil/unique-key)})) (s/gen (s/map-of om-keyword? ::query-root :gen-max 1 :count 1)))))
(s/def ::recur-expr (s/with-gen
(s/or :infinite #{'...} :to-depth (s/and number? pos? integer?))
recur-gen))
(s/def ::join-expr (s/map-of keyword-or-ident?
(s/or :root ::query-root :union ::union-expr :recur ::recur-expr)
:gen-max 1 :count 1))
(s/def ::param-map-expr (s/map-of om-keyword? (s/with-gen anything? anything-gen) :gen-max 1 :count 1))
(s/def ::param-expr (s/cat :first ::plain-query-expr :second ::param-map-expr))
(s/def ::plain-query-expr (s/or :keyword om-keyword? :ident om-ident? :join ::join-expr))
(s/def ::query-expr (s/or :query ::plain-query-expr :parameterized ::param-expr))
(s/def ::query-root (s/with-gen (s/and vector?
(s/every ::query-expr :min-count 1))
#(gen/fmap (fn [v] (with-meta v {:queryid (futil/unique-key)})) (gen/vector (s/gen ::query-expr)))))
(prim/defui A)
(def f (factory A))
; This property isn't holding, but I don't know if it is the spec generators not adding metadata correctly, or what
(def prop-marshall
(prop/for-all [v (s/gen ::query-root)]
(try
(= v (get-query* (normalize-query {} (gen/generate (s/gen ::query-root))) v))
(catch #?(:cljs :default :clj StackOverflowError) e
true)))))
(comment
(tc/quick-check 10 prop-marshall)
(check/check (normalize-query {} (gen/generate (s/gen ::query-root))))
(let [expr (gen/generate (s/gen ::union-expr))]
(and (-> expr meta :queryid)
(every? (fn [[k v]]
(-> v meta :queryid)
) expr)))
(gen/sample (s/gen ::query-root))
(gen/generate (s/gen ::join-expr))
(s/valid? ::query-root '[(:a {:x 1})])
(gen/sample (s/gen (s/cat :k om-keyword? :v (s/or :root ::query-root :union ::union-expr :recur ::recur-expr))))
(gen/sample (s/gen (s/or :n number? :s string?)))
(s/valid? ::param-expr '({:x [:a]} {:f 1})))
(specification "Static Queries"
(behavior "Maintain their backward-compatible functionality" :manual-test))
#?(:clj
(specification "defsc helpers" :focused
(component "legal-keys"
(assertions
"Finds all of the top-level props in a query"
(#'prim/legal-keys [:a :b]) => #{:a :b}
"Finds all of the top-level join keys"
(#'prim/legal-keys [{:a [:x]} {:b [:y]}]) => #{:a :b}
"Finds all of the unique ident (links to root) when used as props"
(#'prim/legal-keys [[:x ''_] [:y ''_]]) => #{:x :y}
"Finds all of the unique ident (links to root) when used as anchor of joins"
(#'prim/legal-keys [{[:x ''_] [:a]} {[:y ''_] [:b]}]) => #{:x :y}
"Finds keys that are parameterized"
(#'prim/legal-keys '[(:a {:n 1})]) => #{:a}
(#'prim/legal-keys '[({:a [:x]} {:n 1})]) => #{:a}
(#'prim/legal-keys '[([:x '_] {:n 1})]) => #{:x}
(#'prim/legal-keys '[({[:x '_] [:a]} {:n 1})]) => #{:x}
))
(component "build-query-forms"
(assertions
"Support a method form"
(#'prim/build-query-forms 'X 'this 'props {:method '(fn [] [:db/id])})
=> `(~'static fulcro.client.primitives/IQuery (~'query [~'this] [:db/id]))
"Uses symbol from external-looking scope in output"
(#'prim/build-query-forms 'X 'that 'props {:method '(query [] [:db/id])})
=> `(~'static fulcro.client.primitives/IQuery (~'query [~'that] [:db/id]))
"Honors the symbol for this that is defined by defsc"