breezy_template 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,161 +1,6 @@
1
1
  require "test_helper"
2
- require "mocha"
3
- require 'delegate'
4
- require "action_view"
5
- require "action_view/testing/resolvers"
6
- require "breezy_template"
7
- require 'byebug'
8
- require 'mocha/test_unit'
9
-
10
- BLOG_POST_PARTIAL = <<-JBUILDER
11
- json.extract! blog_post, :id, :body
12
- json.author do
13
- first_name, last_name = blog_post.author_name.split(nil, 2)
14
- json.first_name first_name
15
- json.last_name last_name
16
- end
17
- JBUILDER
18
-
19
- COLLECTION_PARTIAL = <<-JBUILDER
20
- json.extract! collection, :id, :name
21
- JBUILDER
22
-
23
- PROFILE_PARTIAL = <<-JBUILDER
24
- json.email email
25
- JBUILDER
26
-
27
- RECORD_PARTIAL = <<-JBUILDER
28
- json.email record[:email]
29
- JBUILDER
30
-
31
- FOOTER_PARTIAL = <<-JBUILDER
32
- json.terms "You agree"
33
- JBUILDER
34
-
35
- NESTED_PARTIAL = <<-JBUILDER
36
- json.foo do
37
- json.bar 'goo'
38
- end
39
-
40
- json.nested nil, partial: "footer"
41
- JBUILDER
42
-
43
- FLATTENED_PARTIAL = <<-JBUILDER
44
- json.array! [1,2]
45
- JBUILDER
46
-
47
- BlogPost = Struct.new(:id, :body, :author_name)
48
- Collection = Struct.new(:id, :name)
49
- blog_authors = [ "David Heinemeier Hansson", "Pavel Pravosud" ].cycle
50
- BLOG_POST_COLLECTION = Array.new(10){ |i| BlogPost.new(i+1, "post body #{i+1}", blog_authors.next) }
51
- COLLECTION_COLLECTION = Array.new(5){ |i| Collection.new(i+1, "collection #{i+1}") }
52
-
53
- ActionView::Template.register_template_handler :breezy, BreezyTemplate::Handler
54
-
55
- PARTIALS = {
56
- "_partial.js.breezy" => "foo ||= 'hello'; json.content foo",
57
- "_blog_post.js.breezy" => BLOG_POST_PARTIAL,
58
- "_profile.js.breezy" => PROFILE_PARTIAL,
59
- "_record.js.breezy" => RECORD_PARTIAL,
60
- "_footer.js.breezy" => FOOTER_PARTIAL,
61
- "_nested.js.breezy" => NESTED_PARTIAL,
62
- "_collection.js.breezy" => COLLECTION_PARTIAL,
63
- "_flattened.js.breezy" => FLATTENED_PARTIAL
64
- }
65
-
66
- def strip_format(str)
67
- str.strip_heredoc.gsub(/\n\s*/, "")
68
- end
69
-
70
- class BreezyTemplateTest < ActionView::TestCase
71
- setup do
72
- self.request_forgery = false
73
- BreezyTemplate.configuration.track_sprockets_assets = []
74
- BreezyTemplate.configuration.track_pack_assets = []
75
-
76
- # this is a stub. Normally this would be set by the
77
- # controller locals
78
- self.breezy = {}
79
-
80
- @context = self
81
- Rails.cache.clear
82
- end
83
-
84
- teardown do
85
- # Mocha didn't auto teardown??
86
- Mocha::Mockery.teardown
87
- end
88
-
89
- cattr_accessor :request_forgery, :breezy
90
- self.request_forgery = false
91
-
92
- def breezy_filter
93
- @breezy_filter
94
- end
95
-
96
- def asset_pack_path(asset)
97
- return asset
98
- end
99
-
100
- def request
101
- @request
102
- end
103
-
104
- # Stub out a couple of methods that'll get called from cache_fragment_name
105
- def view_cache_dependencies
106
- []
107
- end
108
-
109
- def jbuild(source, opts={})
110
- @breezy_filter = opts[:breezy_filter]
111
- @request = opts[:request] || action_controller_test_request
112
- @rendered = []
113
- partials = PARTIALS.clone
114
- partials["test.js.breezy"] = source
115
- resolver = ActionView::FixtureResolver.new(partials)
116
- lookup_context.view_paths = [resolver]
117
- lookup_context.formats = [:js]
118
- template = ActionView::Template.new(source, "test", BreezyTemplate::Handler, virtual_path: "test")
119
- template.render(self, {}).strip
120
- end
121
-
122
- def action_controller_test_request
123
- if ::Rails.version.start_with?('5.2')
124
- ::ActionController::TestRequest.create({})
125
- elsif ::Rails.version.start_with?('5.1')
126
- ::ActionController::TestRequest.create({})
127
- elsif ::Rails.version.start_with?('5')
128
- ::ActionController::TestRequest.create
129
- else
130
- ::ActionController::TestRequest.new
131
- end
132
- end
133
-
134
- def cache_keys
135
- major_v = Rails::VERSION::MAJOR
136
- minor_v = Rails::VERSION::MINOR
137
- rails_v = "rails#{major_v}#{minor_v}"
138
- path = File.expand_path("../fixtures/cache_keys.yaml", __FILE__)
139
- keys = YAML.load_file(path)
140
- keys[method_name][rails_v]
141
- end
142
-
143
- def undef_context_methods(*names)
144
- self.class_eval do
145
- names.each do |name|
146
- undef_method name.to_sym if method_defined?(name.to_sym)
147
- end
148
- end
149
- end
150
-
151
- def protect_against_forgery?
152
- self.request_forgery
153
- end
154
-
155
- def form_authenticity_token
156
- "secret"
157
- end
158
2
 
3
+ class ExtensionsTest < BreezyTemplateTestCase
159
4
  test "rendering" do
160
5
  result = jbuild(<<-JBUILDER)
161
6
  json.content "hello"
@@ -245,9 +90,9 @@ class BreezyTemplateTest < ActionView::TestCase
245
90
  assert_equal expected, result
246
91
  end
247
92
 
248
-
249
93
  test "render with csrf token when request forgery is on" do
250
- self.request_forgery = true
94
+ self.stubs(:protect_against_forgery?).returns(true)
95
+
251
96
  # csrf_meta_tags also delegate authenticity tokens to the controller
252
97
  # here we provide a simple mock to the context
253
98
 
@@ -290,1199 +135,6 @@ class BreezyTemplateTest < ActionView::TestCase
290
135
 
291
136
  assert_equal expected, result
292
137
  end
293
-
294
- test "renders partial via the option through set!" do
295
- @post = BLOG_POST_COLLECTION.first
296
- Rails.cache.clear
297
-
298
- result = jbuild(<<-JBUILDER)
299
- json.post @post, partial: ["blog_post", as: :blog_post, fragment_name: :header]
300
- JBUILDER
301
-
302
- expected = strip_format(<<-JS)
303
- (function(){
304
- var fragments={};
305
- var lastFragmentName;
306
- var lastFragmentPath;
307
- var cache={};
308
- var defers=[];
309
- fragments['header'] = fragments['header'] || []; fragments['header'].push('post'); lastFragmentName='header'; lastFragmentPath='post';
310
- return ({"data":{"post":{
311
- "id":1,
312
- "body":"post body 1",
313
- "author":{"firstName":"David","lastName":"Heinemeier Hansson"}
314
- }},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
315
- })()
316
- JS
317
-
318
- assert_equal expected, result
319
- end
320
-
321
- test "renders a partial with explicit fragment" do
322
- result = jbuild(<<-JBUILDER)
323
- json.footer nil, partial: ["footer", fragment_name: 'hello']
324
- JBUILDER
325
-
326
- expected = strip_format(<<-JS)
327
- (function(){
328
- var fragments={};
329
- var lastFragmentName;
330
- var lastFragmentPath;
331
- var cache={};
332
- var defers=[];
333
- fragments['hello'] = fragments['hello'] || []; fragments['hello'].push('footer'); lastFragmentName='hello'; lastFragmentPath='footer';
334
- return ({"data":{"footer":{"terms":"You agree"}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
335
- })()
336
- JS
337
- assert_equal expected, result
338
- end
339
-
340
- test "render array of partials with unique fragments" do
341
- result = jbuild(<<-JBUILDER)
342
- json.array! [1,2], partial: ["footer", fragment_name: ->(x){"somefoo"+x.to_s}]
343
- JBUILDER
344
-
345
- expected = strip_format(<<-JS)
346
- (function(){
347
- var fragments={};
348
- var lastFragmentName;
349
- var lastFragmentPath;
350
- var cache={};
351
- var defers=[];
352
- fragments['somefoo1'] = fragments['somefoo1'] || []; fragments['somefoo1'].push('0'); lastFragmentName='somefoo1'; lastFragmentPath='0';fragments['somefoo2'] = fragments['somefoo2'] || []; fragments['somefoo2'].push('1'); lastFragmentName='somefoo2'; lastFragmentPath='1';
353
- return ({"data":[{"terms":"You agree"},{"terms":"You agree"}],"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
354
- })()
355
- JS
356
-
357
- assert_equal expected, result
358
- end
359
-
360
- test "renders a partial with no locals" do
361
- result = jbuild(<<-JBUILDER)
362
- json.footer nil, partial: "footer"
363
- JBUILDER
364
-
365
- expected = strip_format(<<-JS)
366
- (function(){
367
- var fragments={};
368
- var lastFragmentName;
369
- var lastFragmentPath;
370
- var cache={};
371
- var defers=[];
372
- return ({"data":{"footer":{"terms":"You agree"}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
373
- })()
374
- JS
375
- assert_equal expected, result
376
- end
377
-
378
- test "renders a partial with locals" do
379
- result = jbuild(<<-JBUILDER)
380
- json.profile nil, partial: ["profile", locals: {email: "test@test.com"}]
381
- JBUILDER
382
-
383
- expected = strip_format(<<-JS)
384
- (function(){
385
- var fragments={};
386
- var lastFragmentName;
387
- var lastFragmentPath;
388
- var cache={};
389
- var defers=[];
390
- return ({"data":{"profile":{"email":"test@test.com"}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
391
- })()
392
- JS
393
- assert_equal expected, result
394
- end
395
-
396
- test "renders a partial with locals and caches" do
397
- result = jbuild(<<-JBUILDER)
398
- opts = {
399
- cache: 'cachekey',
400
- partial: ["profile", locals: {email: "test@test.com"}]
401
- }
402
- json.profile 32, opts
403
- JBUILDER
404
-
405
- expected = strip_format(<<-JS)
406
- (function(){
407
- var fragments={};
408
- var lastFragmentName;
409
- var lastFragmentPath;
410
- var cache={};
411
- var defers=[];
412
- cache["#{cache_keys[0]}"]={"email":"test@test.com"};
413
- return ({"data":{"profile":cache["#{cache_keys[0]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
414
- })()
415
- JS
416
-
417
- assert_equal expected, result
418
- end
419
-
420
- test "renders a partial even without a :as to the value, this usage is rare" do
421
- result = jbuild(<<-JBUILDER)
422
- json.profile 32, partial: ["profile", locals: {email: "test@test.com"}]
423
- JBUILDER
424
-
425
- expected = strip_format(<<-JS)
426
- (function(){
427
- var fragments={};
428
- var lastFragmentName;
429
- var lastFragmentPath;
430
- var cache={};
431
- var defers=[];
432
- return ({"data":{"profile":{"email":"test@test.com"}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
433
- })()
434
- JS
435
-
436
- assert_equal expected, result
437
- end
438
-
439
- test "render array of partials without an :as to a member, this usage is very rare" do
440
- result = jbuild(<<-JBUILDER)
441
- json.array! [1,2], partial: "footer"
442
- JBUILDER
443
-
444
- expected = strip_format(<<-JS)
445
- (function(){
446
- var fragments={};
447
- var lastFragmentName;
448
- var lastFragmentPath;
449
- var cache={};
450
- var defers=[];
451
- return ({"data":[{"terms":"You agree"},{"terms":"You agree"}],"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
452
- })()
453
- JS
454
-
455
- assert_equal expected, result
456
- end
457
-
458
- test "render array of partials without an :as to a member and cache" do
459
- result = jbuild(<<-JBUILDER)
460
- json.array! [1,2], partial: "footer", cache: ->(i){ ['a', i] }
461
- JBUILDER
462
-
463
- expected = strip_format(<<-JS)
464
- (function(){
465
- var fragments={};
466
- var lastFragmentName;
467
- var lastFragmentPath;
468
- var cache={};
469
- var defers=[];
470
- cache["#{cache_keys[0]}"]={"terms":"You agree"};
471
- cache["#{cache_keys[1]}"]={"terms":"You agree"};
472
- return ({"data":[cache["#{cache_keys[0]}"],cache["#{cache_keys[1]}"]],"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
473
- })()
474
- JS
475
-
476
- assert_equal expected, result
477
- end
478
-
479
- test "render array of partials" do
480
- result = jbuild(<<-JBUILDER)
481
- json.array! BLOG_POST_COLLECTION, partial: ["blog_post", as: :blog_post]
482
- JBUILDER
483
-
484
- expected = strip_format(<<-JS)
485
- (function(){
486
- var fragments={};
487
- var lastFragmentName;
488
- var lastFragmentPath;
489
- var cache={};
490
- var defers=[];
491
- return ({"data":[
492
- {"id":1,"body":"post body 1","author":{"firstName":"David","lastName":"Heinemeier Hansson"}},
493
- {"id":2,"body":"post body 2","author":{"firstName":"Pavel","lastName":"Pravosud"}},
494
- {"id":3,"body":"post body 3","author":{"firstName":"David","lastName":"Heinemeier Hansson"}},
495
- {"id":4,"body":"post body 4","author":{"firstName":"Pavel","lastName":"Pravosud"}},
496
- {"id":5,"body":"post body 5","author":{"firstName":"David","lastName":"Heinemeier Hansson"}},
497
- {"id":6,"body":"post body 6","author":{"firstName":"Pavel","lastName":"Pravosud"}},
498
- {"id":7,"body":"post body 7","author":{"firstName":"David","lastName":"Heinemeier Hansson"}},
499
- {"id":8,"body":"post body 8","author":{"firstName":"Pavel","lastName":"Pravosud"}},
500
- {"id":9,"body":"post body 9","author":{"firstName":"David","lastName":"Heinemeier Hansson"}},
501
- {"id":10,"body":"post body 10","author":{"firstName":"Pavel","lastName":"Pravosud"}}
502
- ],"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
503
- })()
504
- JS
505
-
506
- assert_equal expected, result
507
- end
508
-
509
- test "renders array of partials as empty array with an empty collection" do
510
- result = jbuild(<<-JBUILDER)
511
- json.array! [], partial: ["blog_post", as: :blog_post]
512
- JBUILDER
513
-
514
- expected = strip_format(<<-JS)
515
- (function(){
516
- var fragments={};
517
- var lastFragmentName;
518
- var lastFragmentPath;
519
- var cache={};
520
- var defers=[];
521
- return ({"data":[],"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
522
- })()
523
- JS
524
-
525
- assert_equal expected, result
526
- end
527
-
528
- test "renders the partial and ignores the value" do
529
- result = jbuild <<-JBUILDER
530
- json.posts nil, partial: "footer"
531
- JBUILDER
532
-
533
- expected = strip_format(<<-JS)
534
- (function(){
535
- var fragments={};
536
- var lastFragmentName;
537
- var lastFragmentPath;
538
- var cache={};
539
- var defers=[];
540
- return ({"data":{"posts":{"terms":"You agree"}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
541
- })()
542
- JS
543
- assert_equal expected, result
544
- end
545
-
546
- test "renders the partial as an array and ignores the value" do
547
- result = jbuild <<-JBUILDER
548
- json.posts nil, partial: "flattened"
549
- JBUILDER
550
-
551
- expected = strip_format(<<-JS)
552
- (function(){
553
- var fragments={};
554
- var lastFragmentName;
555
- var lastFragmentPath;
556
- var cache={};
557
- var defers=[];
558
- return ({"data":{"posts":[1,2]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
559
- })()
560
- JS
561
- assert_equal expected, result
562
- end
563
-
564
- test "caching a value at a node" do
565
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
566
-
567
- result = jbuild(<<-JBUILDER)
568
- opts = {
569
- cache: [['b', 'c']]
570
- }
571
- json.hello 32, opts
572
- JBUILDER
573
-
574
- expected = strip_format(<<-JS)
575
- (function(){
576
- var fragments={};
577
- var lastFragmentName;
578
- var lastFragmentPath;
579
- var cache={};
580
- var defers=[];
581
- cache["#{cache_keys[0]}"]=32;
582
- return ({"data":{"hello":cache["#{cache_keys[0]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
583
- })()
584
- JS
585
-
586
- assert_equal expected, result
587
- end
588
-
589
- test "caching elements in a list" do
590
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
591
-
592
- result = jbuild(<<-JBUILDER)
593
- json.hello do
594
- opts = {
595
- cache: ->(i){ ['a', i] }
596
- }
597
- json.array! [4,5], opts do |x|
598
- json.top "hello" + x.to_s
599
- end
600
- end
601
- JBUILDER
602
-
603
- expected = strip_format(<<-JS)
604
- (function(){
605
- var fragments={};
606
- var lastFragmentName;
607
- var lastFragmentPath;
608
- var cache={};
609
- var defers=[];
610
- cache["#{cache_keys[0]}"]={"top":"hello4"};
611
- cache["#{cache_keys[1]}"]={"top":"hello5"};
612
- return ({"data":{"hello":[cache["#{cache_keys[0]}"],cache["#{cache_keys[1]}"]]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
613
- })()
614
- JS
615
-
616
- assert_equal expected, result
617
- end
618
-
619
- test "nested caching generates a depth-first list of cache nodes" do
620
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
621
-
622
- result = jbuild(<<-JBUILDER)
623
- json.hello cache: [['a', 'b']] do
624
- json.content cache: [['d', 'z']] do
625
- json.subcontent 'inner'
626
- end
627
- json.other cache: [['e', 'z']] do
628
- json.subcontent 'other'
629
- end
630
- end
631
- JBUILDER
632
-
633
- expected = strip_format(<<-JS)
634
- (function(){
635
- var fragments={};
636
- var lastFragmentName;
637
- var lastFragmentPath;
638
- var cache={};
639
- var defers=[];
640
- cache["#{cache_keys[0]}"]={"subcontent":"inner"};
641
- cache["#{cache_keys[1]}"]={"subcontent":"other"};
642
- cache["#{cache_keys[2]}"]={"content":cache["#{cache_keys[0]}"],"other":cache["#{cache_keys[1]}"]};
643
- return ({"data":{"hello":cache["#{cache_keys[2]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
644
- })()
645
- JS
646
-
647
- assert_equal expected, result
648
- end
649
-
650
- test "caching an empty block generates no cache and no errors" do
651
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
652
-
653
- result = nil
654
-
655
- assert_nothing_raised do
656
- result = jbuild(<<-JBUILDER)
657
- json.hello do
658
- json.array! [4,5], cache: ->(i){['a', i]} do |x|
659
- end
660
- end
661
- JBUILDER
662
- end
663
-
664
- expected = strip_format(<<-JS)
665
- (function(){
666
- var fragments={};
667
- var lastFragmentName;
668
- var lastFragmentPath;
669
- var cache={};
670
- var defers=[];
671
- return ({\"data\":{\"hello\":[]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
672
- })()
673
- JS
674
-
675
- assert_equal expected, result
676
- end
677
- #
678
- # test "child! accepts cache options" do
679
- # undef_context_methods :fragment_name_with_digest, :cache_fragment_name
680
- #
681
- # result = jbuild(<<-JBUILDER)
682
- # json.comments do
683
- # json.child!(cache: ['e', 'z']) { json.content "hello" }
684
- # json.child! { json.content "world" }
685
- # end
686
- # JBUILDER
687
- #
688
- # expected = strip_format(<<-JS)
689
- # (function(){
690
- # cache["#{cache_keys[0]}", {"content":"hello"});
691
- # return ({"data":{"comments":[cache["#{cache_keys[0]}"),{"content":"world"}]}});
692
- # })()
693
- # JS
694
- #
695
- # assert_equal expected, result
696
- # end
697
-
698
- test "fragment caching" do
699
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
700
-
701
- jbuild(<<-JBUILDER)
702
- opts = {cache: ['cachekey']}
703
- json.post opts do
704
- json.name "Cache"
705
- end
706
- JBUILDER
707
-
708
- result = jbuild(<<-JBUILDER)
709
- opts = {cache: ['cachekey']}
710
- json.post opts do
711
- json.name "Miss"
712
- end
713
- JBUILDER
714
-
715
- expected = strip_format(<<-JS)
716
- (function(){
717
- var fragments={};
718
- var lastFragmentName;
719
- var lastFragmentPath;
720
- var cache={};
721
- var defers=[];
722
- cache["#{cache_keys[0]}"]={"name":"Cache"};
723
- return ({"data":{"post":cache["#{cache_keys[0]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
724
- })()
725
- JS
726
-
727
- assert_equal expected, result
728
- end
729
-
730
- test "fragment caching deserializes an array" do
731
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
732
-
733
- result = jbuild <<-JBUILDER
734
- json.content cache: "cachekey" do
735
- json.array! %w[a b c]
736
- end
737
- JBUILDER
738
-
739
- expected = strip_format(<<-JS)
740
- (function(){
741
- var fragments={};
742
- var lastFragmentName;
743
- var lastFragmentPath;
744
- var cache={};
745
- var defers=[];
746
- cache["#{cache_keys[0]}"]=["a","b","c"];
747
- return ({"data":{"content":cache["#{cache_keys[0]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
748
- })()
749
- JS
750
-
751
- assert_equal expected, result
752
- end
753
- #
754
- test "fragment caching works with previous version of cache digests" do
755
- undef_context_methods :cache_fragment_name
756
-
757
- if !@context.class.method_defined? :fragment_name_with_digest
758
- @context.class_eval do
759
- def fragment_name_with_digest
760
- end
761
- end
762
- end
763
-
764
- @context.expects :fragment_name_with_digest
765
-
766
- jbuild <<-JBUILDER
767
- json.content cache: 'cachekey' do
768
- json.name "Cache"
769
- end
770
- JBUILDER
771
- end
772
-
773
- test "fragment caching works with current cache digests" do
774
- undef_context_methods :fragment_name_with_digest
775
-
776
- @context.expects :cache_fragment_name
777
- ActiveSupport::Cache.expects :expand_cache_key
778
-
779
- jbuild <<-JBUILDER
780
- json.content cache: 'cachekey' do
781
- json.name "Cache"
782
- end
783
- JBUILDER
784
- end
785
-
786
- # test "current cache digest option accepts options through the last element hash" do
787
- # undef_context_methods :fragment_name_with_digest
788
- #
789
- # @context.expects(:cache_fragment_name)
790
- # .with("cachekey", skip_digest: true)
791
- # .returns("cachekey")
792
- #
793
- # ActiveSupport::Cache.expects :expand_cache_key
794
- #
795
- # jbuild <<-JBUILDER
796
- # json.wrap! :cache, 'cachekey', skip_digest: true
797
- # json.content do
798
- # json.name "Cache"
799
- # end
800
- # JBUILDER
801
- # end
802
-
803
- test "does not perform caching when controller.perform_caching is false" do
804
- controller.perform_caching = false
805
-
806
- result = jbuild <<-JBUILDER
807
- json.content cache: 'cachekey' do
808
- json.name "Cache"
809
- end
810
- JBUILDER
811
-
812
- expected = strip_format(<<-JS)
813
- (function(){
814
- var fragments={};
815
- var lastFragmentName;
816
- var lastFragmentPath;
817
- var cache={};
818
- var defers=[];
819
- return ({"data":{"content":{"name":"Cache"}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
820
- })()
821
- JS
822
-
823
- assert_equal expected, result
824
- end
825
-
826
- test "invokes templates via params via set! and caches" do
827
- @post = BLOG_POST_COLLECTION.first
828
-
829
- result = jbuild(<<-JBUILDER)
830
- json.post @post, partial: ["blog_post", as: :blog_post], cache: [['a', 'b']]
831
- JBUILDER
832
-
833
- expected = strip_format(<<-JS)
834
- (function(){
835
- var fragments={};
836
- var lastFragmentName;
837
- var lastFragmentPath;
838
- var cache={};
839
- var defers=[];
840
- cache["#{cache_keys[0]}"]={"id":1,"body":"post body 1","author":{"firstName":"David","lastName":"Heinemeier Hansson"}};
841
- return ({"data":{"post":cache["#{cache_keys[0]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
842
- })()
843
- JS
844
-
845
- assert_equal expected, result
846
- end
847
-
848
- test "shares partial caches (via the partial's digest) across multiple templates" do
849
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
850
-
851
- @hit = BlogPost.new(1, "hit", "John Smith")
852
- @miss = BlogPost.new(2, "miss", "John Smith")
853
-
854
- cat = jbuild(<<-JBUILDER)
855
- opts = {
856
- cache: [['a', 'b']],
857
- partial: ["blog_post", as: :blog_post]
858
- }
859
-
860
- json.post @hit, opts
861
- JBUILDER
862
-
863
- result = jbuild(<<-JBUILDER)
864
- opts = {
865
- cache: [['a', 'b']],
866
- partial: ["blog_post", as: :blog_post]
867
- }
868
-
869
- json.post @miss, opts
870
- JBUILDER
871
-
872
- expected = strip_format(<<-JS)
873
- (function(){
874
- var fragments={};
875
- var lastFragmentName;
876
- var lastFragmentPath;
877
- var cache={};
878
- var defers=[];
879
- cache["#{cache_keys[0]}"]={"id":1,"body":"hit","author":{"firstName":"John","lastName":"Smith"}};
880
- return ({"data":{"post":cache["#{cache_keys[0]}"]},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
881
- })()
882
- JS
883
-
884
- assert_equal expected, result
885
- end
886
-
887
-
888
- test "render array of partials and caches" do
889
- result = jbuild(<<-JBUILDER)
890
- opts = {
891
- cache: (->(d){ ['a', d.id] }),
892
- partial: ["blog_post", as: :blog_post]
893
- }
894
- json.array! BLOG_POST_COLLECTION, opts
895
- JBUILDER
896
- Rails.cache.clear
897
-
898
- expected = strip_format(<<-JS)
899
- (function(){
900
- var fragments={};
901
- var lastFragmentName;
902
- var lastFragmentPath;
903
- var cache={};
904
- var defers=[];
905
- cache["#{cache_keys[0]}"]={"id":1,"body":"post body 1","author":{"firstName":"David","lastName":"Heinemeier Hansson"}};
906
- cache["#{cache_keys[1]}"]={"id":2,"body":"post body 2","author":{"firstName":"Pavel","lastName":"Pravosud"}};
907
- cache["#{cache_keys[2]}"]={"id":3,"body":"post body 3","author":{"firstName":"David","lastName":"Heinemeier Hansson"}};
908
- cache["#{cache_keys[3]}"]={"id":4,"body":"post body 4","author":{"firstName":"Pavel","lastName":"Pravosud"}};
909
- cache["#{cache_keys[4]}"]={"id":5,"body":"post body 5","author":{"firstName":"David","lastName":"Heinemeier Hansson"}};
910
- cache["#{cache_keys[5]}"]={"id":6,"body":"post body 6","author":{"firstName":"Pavel","lastName":"Pravosud"}};
911
- cache["#{cache_keys[6]}"]={"id":7,"body":"post body 7","author":{"firstName":"David","lastName":"Heinemeier Hansson"}};
912
- cache["#{cache_keys[7]}"]={"id":8,"body":"post body 8","author":{"firstName":"Pavel","lastName":"Pravosud"}};
913
- cache["#{cache_keys[8]}"]={"id":9,"body":"post body 9","author":{"firstName":"David","lastName":"Heinemeier Hansson"}};
914
- cache["#{cache_keys[9]}"]={"id":10,"body":"post body 10","author":{"firstName":"Pavel","lastName":"Pravosud"}};
915
- return ({"data":[cache["#{cache_keys[0]}"],cache["#{cache_keys[1]}"],cache["#{cache_keys[2]}"],cache["#{cache_keys[3]}"],cache["#{cache_keys[4]}"],cache["#{cache_keys[5]}"],cache["#{cache_keys[6]}"],cache["#{cache_keys[7]}"],cache["#{cache_keys[8]}"],cache["#{cache_keys[9]}"]],"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
916
- })()
917
- JS
918
- assert_equal expected, result
919
- end
920
-
921
- test "filtering for a node in the tree" do
922
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
923
- json.hit do
924
- json.hit2 do
925
- json.greeting 'hello world'
926
- end
927
- end
928
-
929
- json.miss do
930
- json.miss2 do
931
- raise 'this should not be called'
932
- json.greeting 'missed call'
933
- end
934
- end
935
- JBUILDER
936
- Rails.cache.clear
937
-
938
- expected = strip_format(<<-JS)
939
- (function(){
940
- var fragments={};
941
- var lastFragmentName;
942
- var lastFragmentPath;
943
- var cache={};
944
- var defers=[];
945
- return (
946
- {"data":{"greeting":"hello world"},"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
947
- );
948
- })()
949
- JS
950
-
951
- assert_equal expected, result
952
- end
953
-
954
- test "filtering for a node in the tree with camelized keys" do
955
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit_one.hit_two')
956
- json.hit_one do
957
- json.hit_two do
958
- json.greeting 'hello world'
959
- end
960
- end
961
- JBUILDER
962
- Rails.cache.clear
963
-
964
- expected = strip_format(<<-JS)
965
- (function(){
966
- var fragments={};
967
- var lastFragmentName;
968
- var lastFragmentPath;
969
- var cache={};
970
- var defers=[];
971
- return (
972
- {"data":{"greeting":"hello world"},"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hitOne.hitTwo","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
973
- );
974
- })()
975
- JS
976
-
977
- assert_equal expected, result
978
- end
979
-
980
- test "filtering for a nonexistant node in the tree" do
981
- begin
982
- jbuild(<<-JBUILDER)
983
- json._set_search_path_once('miss.miss.miss.miss')
984
- json.hit do
985
- json.hit2 do
986
- json.greeting 'hello world'
987
- end
988
- end
989
- JBUILDER
990
- rescue => e
991
- assert_equal e.cause.class, BreezyTemplate::NotFoundError
992
- assert_equal e.message, 'Could not find node at ["miss", "miss", "miss", "miss"]'
993
- end
994
-
995
- Rails.cache.clear
996
- end
997
- test "filtering for a raw value is also possble" do
998
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
999
- json.hit do
1000
- json.hit2 23
1001
- end
1002
-
1003
- json.miss do
1004
- json.miss2 do
1005
- raise 'this should not be called'
1006
- json.greeting 'missed call'
1007
- end
1008
- end
1009
- JBUILDER
1010
- Rails.cache.clear
1011
-
1012
- expected = strip_format(<<-JS)
1013
- (function(){
1014
- var fragments={};
1015
- var lastFragmentName;
1016
- var lastFragmentPath;
1017
- var cache={};
1018
- var defers=[];
1019
- return (
1020
- {"data":23,"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1021
- );
1022
- })()
1023
- JS
1024
-
1025
- assert_equal expected, result
1026
- end
1027
-
1028
- test "filter with partials" do
1029
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.nested.terms')
1030
- json.hit do
1031
- json.hit2 nil, partial: "nested"
1032
- end
1033
- JBUILDER
1034
-
1035
- expected = strip_format(<<-JS)
1036
- (function(){
1037
- var fragments={};
1038
- var lastFragmentName;
1039
- var lastFragmentPath;
1040
- var cache={};
1041
- var defers=[];
1042
- return (
1043
- {"data":"You agree","screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2.nested.terms","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1044
- );
1045
- })()
1046
- JS
1047
- assert_equal expected, result
1048
- end
1049
-
1050
- test "filtering for a node in the tree via breezy_filter helper" do
1051
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
1052
- json.hit do
1053
- json.hit2 do
1054
- json.greeting 'hello world'
1055
- end
1056
- end
1057
-
1058
- json.miss do
1059
- json.miss2 do
1060
- raise 'this should not be called'
1061
- json.greeting 'missed call'
1062
- end
1063
- end
1064
- JBUILDER
1065
- Rails.cache.clear
1066
-
1067
- expected = strip_format(<<-JS)
1068
- (function(){
1069
- var fragments={};
1070
- var lastFragmentName;
1071
- var lastFragmentPath;
1072
- var cache={};
1073
- var defers=[];
1074
- return (
1075
- {"data":{"greeting":"hello world"},"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1076
- );
1077
- })()
1078
- JS
1079
-
1080
- assert_equal expected, result
1081
- end
1082
-
1083
- test "filtering a cached node returns just that" do
1084
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
1085
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
1086
- json.hit do
1087
- json.hit2 cache: 'a' do
1088
- json.greeting 'hello world'
1089
- end
1090
- end
1091
- JBUILDER
1092
- Rails.cache.clear
1093
-
1094
- expected = strip_format(<<-JS)
1095
- (function(){
1096
- var fragments={};
1097
- var lastFragmentName;
1098
- var lastFragmentPath;
1099
- var cache={};
1100
- var defers=[];
1101
- cache["#{cache_keys[0]}"]={"greeting":"hello world"};
1102
- return ({"data":cache["#{cache_keys[0]}"],"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
1103
- })()
1104
-
1105
-
1106
- JS
1107
-
1108
- assert_equal expected, result
1109
- end
1110
-
1111
- test "filtering for a node in an array of a tree by id" do
1112
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.id=1')
1113
- json.hit do
1114
- json.hit2 do
1115
- data = ObjectCollection.new([{id: 1, name: 'hit' }, {id:2, name: 'miss'}])
1116
- json.array! data do |x|
1117
- raise 'this should be be called' if x[:name] == 'miss'
1118
- json.name x[:name]
1119
- end
1120
- end
1121
- end
1122
- JBUILDER
1123
- Rails.cache.clear
1124
-
1125
- expected = strip_format(<<-JS)
1126
- (function(){
1127
- var fragments={};
1128
- var lastFragmentName;
1129
- var lastFragmentPath;
1130
- var cache={};
1131
- var defers=[];
1132
- return (
1133
- {"data":{"name":"hit"},"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2.id=1","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1134
- );
1135
- })()
1136
- JS
1137
-
1138
- assert_equal expected, result
1139
- end
1140
-
1141
- test "filtering for a node in an array of a tree by index" do
1142
- result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.0')
1143
- data = [{id: 1, name: 'hit' }, {id:2, name: 'miss'}]
1144
- json.hit do
1145
- json.hit2 do
1146
- json.array! data do |x|
1147
- raise 'this should be be called' if x[:name] == 'miss'
1148
- json.name x[:name]
1149
- end
1150
- end
1151
- end
1152
- JBUILDER
1153
- Rails.cache.clear
1154
-
1155
- expected = strip_format(<<-JS)
1156
- (function(){
1157
- var fragments={};
1158
- var lastFragmentName;
1159
- var lastFragmentPath;
1160
- var cache={};
1161
- var defers=[];
1162
- return (
1163
- {"data":{"name":"hit"},"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hit.hit2.0","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1164
- );
1165
- })()
1166
- JS
1167
-
1168
- assert_equal expected, result
1169
- end
1170
-
1171
- test "rendering with node deferement" do
1172
- req = action_controller_test_request
1173
- req.path = '/some_url'
1174
-
1175
- result = jbuild(<<-JBUILDER, request: req)
1176
- json.hit do
1177
- json.hit2(defer: :auto)do
1178
- json.hit3 do
1179
- json.greeting 'hello world'
1180
- end
1181
- end
1182
- end
1183
- JBUILDER
1184
- Rails.cache.clear
1185
-
1186
- expected = strip_format(<<-JS)
1187
- (function(){
1188
- var fragments={};
1189
- var lastFragmentName;
1190
- var lastFragmentPath;
1191
- var cache={};
1192
- var defers=[];
1193
- defers.push({url:'/some_url?_bz=hit.hit2'});
1194
- return (
1195
- {"data":{"hit":{"hit2":undefined}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1196
- );
1197
- })()
1198
- JS
1199
-
1200
- assert_equal expected, result
1201
- end
1202
-
1203
- test "rendering with manual node deferement" do
1204
- req = action_controller_test_request
1205
- req.path = '/some_url'
1206
-
1207
- result = jbuild(<<-JBUILDER, request: req)
1208
- json.hit do
1209
- json.hit2 defer: :manual do
1210
- json.hit3 do
1211
- json.greeting 'hello world'
1212
- end
1213
- end
1214
- end
1215
- JBUILDER
1216
- Rails.cache.clear
1217
-
1218
- expected = strip_format(<<-JS)
1219
- (function(){
1220
- var fragments={};
1221
- var lastFragmentName;
1222
- var lastFragmentPath;
1223
- var cache={};
1224
- var defers=[];
1225
- return (
1226
- {"data":{"hit":{"hit2":undefined}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1227
- );
1228
- })()
1229
- JS
1230
-
1231
- assert_equal expected, result
1232
- end
1233
-
1234
- test "rendering with selective array node deferment" do
1235
- req = action_controller_test_request
1236
- req.path = '/some_url'
1237
-
1238
- result = jbuild(<<-JBUILDER, request: req)
1239
- keep_first = lambda do |item|
1240
- if item[:id] == 1
1241
- false
1242
- else
1243
- :auto
1244
- end
1245
- end
1246
-
1247
- json.hit do
1248
- json.hit2 do
1249
- data = [{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]
1250
- json.array! data, key: :id, defer: keep_first do |item|
1251
- json.name item[:name]
1252
- end
1253
- end
1254
- end
1255
- JBUILDER
1256
- Rails.cache.clear
1257
-
1258
- expected = strip_format(<<-JS)
1259
- (function(){
1260
- var fragments={};
1261
- var lastFragmentName;
1262
- var lastFragmentPath;
1263
- var cache={};
1264
- var defers=[];
1265
- defers.push({url:'/some_url?_bz=hit.hit2.id%3D2'});
1266
- return (
1267
- {"data":{"hit":{"hit2":[{"name":"foo"},{"id":2}]}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1268
- );
1269
- })()
1270
- JS
1271
-
1272
- assert_equal expected, result
1273
- end
1274
-
1275
- test "rendering with node array partial deferment" do
1276
- req = action_controller_test_request
1277
- req.path = '/some_url'
1278
-
1279
- result = jbuild(<<-JBUILDER, request: req)
1280
- keep_first = lambda do |item|
1281
- if item[:id] == 1
1282
- false
1283
- else
1284
- :auto
1285
- end
1286
- end
1287
-
1288
- json.hit do
1289
- json.hit2 do
1290
- data = [{id: 1, email: 'foo'}, {id: 2, email: 'bar'}]
1291
- json.array! data, key: :id, defer: keep_first, partial: ['record', as: :record]
1292
- end
1293
- end
1294
- JBUILDER
1295
- Rails.cache.clear
1296
-
1297
- expected = strip_format(<<-JS)
1298
- (function(){
1299
- var fragments={};
1300
- var lastFragmentName;
1301
- var lastFragmentPath;
1302
- var cache={};
1303
- var defers=[];
1304
- defers.push({url:'/some_url?_bz=hit.hit2.id%3D2'});
1305
- return (
1306
- {"data":{"hit":{"hit2":[{"email":"foo"},{"id":2}]}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1307
- );
1308
- })()
1309
- JS
1310
-
1311
- assert_equal expected, result
1312
- end
1313
-
1314
- test "rendering with node array deferment" do
1315
- req = action_controller_test_request
1316
- req.path = '/some_url'
1317
-
1318
- result = jbuild(<<-JBUILDER, request: req)
1319
- json.hit do
1320
- json.hit2 do
1321
- data = [{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]
1322
- json.array! data, key: :id, defer: :auto do |item|
1323
- json.name item[:name]
1324
- end
1325
- end
1326
- end
1327
- JBUILDER
1328
- Rails.cache.clear
1329
-
1330
- expected = strip_format(<<-JS)
1331
- (function(){
1332
- var fragments={};
1333
- var lastFragmentName;
1334
- var lastFragmentPath;
1335
- var cache={};
1336
- var defers=[];
1337
- defers.push({url:'/some_url?_bz=hit.hit2.id%3D1'});
1338
- defers.push({url:'/some_url?_bz=hit.hit2.id%3D2'});
1339
- return (
1340
- {"data":{"hit":{"hit2":[{"id":1},{"id":2}]}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1341
- );
1342
- })()
1343
- JS
1344
-
1345
- assert_equal expected, result
1346
- end
1347
-
1348
- test "rendering with node array deferment using index" do
1349
- req = action_controller_test_request
1350
- req.path = '/some_url'
1351
-
1352
- result = jbuild(<<-JBUILDER, request: req)
1353
- json.hit do
1354
- json.hit2 do
1355
- data = [{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]
1356
- json.array! data, defer: :auto do |item|
1357
- json.name item[:name]
1358
- end
1359
- end
1360
- end
1361
- JBUILDER
1362
- Rails.cache.clear
1363
-
1364
- expected = strip_format(<<-JS)
1365
- (function(){
1366
- var fragments={};
1367
- var lastFragmentName;
1368
- var lastFragmentPath;
1369
- var cache={};
1370
- var defers=[];
1371
- defers.push({url:'/some_url?_bz=hit.hit2.0'});
1372
- defers.push({url:'/some_url?_bz=hit.hit2.1'});
1373
- return (
1374
- {"data":{"hit":{"hit2":[undefined,undefined]}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1375
- );
1376
- })()
1377
- JS
1378
-
1379
- assert_equal expected, result
1380
- end
1381
-
1382
- test "rendering with node array deferment on nested node" do
1383
- req = action_controller_test_request
1384
- req.path = '/some_url'
1385
-
1386
- result = jbuild(<<-JBUILDER, request: req)
1387
- json.hit do
1388
- json.hit2 do
1389
- data = [{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]
1390
- json.array! data, key: :id do
1391
- json.greeting defer: :auto do
1392
- json.gree 'hi'
1393
- end
1394
- end
1395
- end
1396
- end
1397
- JBUILDER
1398
- Rails.cache.clear
1399
-
1400
- expected = strip_format(<<-JS)
1401
- (function(){
1402
- var fragments={};
1403
- var lastFragmentName;
1404
- var lastFragmentPath;
1405
- var cache={};
1406
- var defers=[];
1407
- defers.push({url:'/some_url?_bz=hit.hit2.id%3D1.greeting'});
1408
- defers.push({url:'/some_url?_bz=hit.hit2.id%3D2.greeting'});
1409
- return (
1410
- {"data":{"hit":{"hit2":[{"greeting":undefined},{"greeting":undefined}]}},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}}
1411
- );
1412
- })()
1413
- JS
1414
-
1415
- assert_equal expected, result
1416
- end
1417
-
1418
- test 'deferment does not work on values' do
1419
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
1420
-
1421
- result = jbuild(<<-JBUILDER)
1422
- json.hello(32, defer: :auto)
1423
- JBUILDER
1424
-
1425
- expected = strip_format(<<-JS)
1426
- (function(){
1427
- var fragments={};
1428
- var lastFragmentName;
1429
- var lastFragmentPath;
1430
- var cache={};
1431
- var defers=[];
1432
- return ({"data":{"hello":32},"screen":"test","fragments":fragments,"privateOpts":{"lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
1433
- })()
1434
- JS
1435
-
1436
- assert_equal expected, result
1437
- end
1438
-
1439
- test 'deferment is disabled when filtering by keypath' do
1440
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
1441
- result = jbuild(<<-JBUILDER, breezy_filter: 'hello.world')
1442
- json.hello defer: :auto do
1443
- json.world 32
1444
- end
1445
- JBUILDER
1446
-
1447
- expected = strip_format(<<-JS)
1448
- (function(){
1449
- var fragments={};
1450
- var lastFragmentName;
1451
- var lastFragmentPath;
1452
- var cache={};
1453
- var defers=[];
1454
- return ({"data":32,"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hello.world","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
1455
- })()
1456
- JS
1457
-
1458
- assert_equal expected, result
1459
-
1460
- end
1461
-
1462
- test 'deferment is enabled at the end of a keypath when filtering' do
1463
- undef_context_methods :fragment_name_with_digest, :cache_fragment_name
1464
- result = jbuild(<<-JBUILDER, breezy_filter: 'hello')
1465
- json.hello do
1466
- json.content defer: :auto do
1467
- json.world 32
1468
- end
1469
- end
1470
- JBUILDER
1471
-
1472
- expected = strip_format(<<-JS)
1473
- (function(){
1474
- var fragments={};
1475
- var lastFragmentName;
1476
- var lastFragmentPath;
1477
- var cache={};
1478
- var defers=[];
1479
- defers.push({url:'?_bz=hello.content'});
1480
- return ({"data":{"content":undefined},"screen":"test","fragments":fragments,"privateOpts":{"action":"graft","path":"hello","lastFragmentName":lastFragmentName,"lastFragmentPath":lastFragmentPath,"defers":defers}});
1481
- })()
1482
- JS
1483
-
1484
- assert_equal expected, result
1485
- end
1486
138
  end
1487
139
 
1488
140