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.
data/test/test_helper.rb CHANGED
@@ -7,10 +7,20 @@ require 'active_record'
7
7
  require 'active_support/testing/autorun' if Rails.version >= '4'
8
8
  require 'active_support/test_case'
9
9
 
10
+ require "mocha"
11
+ require 'delegate'
12
+ require "action_view"
13
+ require "action_view/testing/resolvers"
14
+ require "breezy_template"
15
+ require 'byebug'
16
+ require 'mocha/test_unit'
17
+
10
18
  ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_to?(:test_order=)
11
19
  ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
12
20
  Rails.cache = ActiveSupport::Cache::MemoryStore.new
13
21
 
22
+ require 'breezy_template/core_ext'
23
+
14
24
  class ObjectCollection < SimpleDelegator
15
25
  def member_at(index)
16
26
  at(index)
@@ -23,5 +33,156 @@ class ObjectCollection < SimpleDelegator
23
33
  end
24
34
  end
25
35
 
36
+ RAISE_IF_USED_PARTIAL = <<-JBUILDER
37
+ if used
38
+ raise 'No hit expected here'
39
+ end
40
+ JBUILDER
41
+
42
+ BLOG_POST_PARTIAL = <<-JBUILDER
43
+ json.extract! blog_post, :id, :body
44
+ json.author do
45
+ first_name, last_name = blog_post.author_name.split(nil, 2)
46
+ json.first_name first_name
47
+ json.last_name last_name
48
+ end
49
+ JBUILDER
50
+
51
+ COLLECTION_PARTIAL = <<-JBUILDER
52
+ json.extract! collection, :id, :name
53
+ JBUILDER
54
+
55
+ PROFILE_PARTIAL = <<-JBUILDER
56
+ json.email email
57
+ JBUILDER
58
+
59
+ RECORD_PARTIAL = <<-JBUILDER
60
+ json.email record[:email]
61
+ JBUILDER
62
+
63
+ FOOTER_PARTIAL = <<-JBUILDER
64
+ json.terms "You agree"
65
+ JBUILDER
66
+
67
+ NESTED_PARTIAL = <<-JBUILDER
68
+ json.foo do
69
+ json.bar 'goo'
70
+ end
71
+
72
+ json.nested nil, partial: "footer"
73
+ JBUILDER
74
+
75
+ FLATTENED_PARTIAL = <<-JBUILDER
76
+ json.array! [1,2]
77
+ JBUILDER
78
+
79
+ PARTIALS = {
80
+ "_partial.js.breezy" => "foo ||= 'hello'; json.content foo",
81
+ "_blog_post.js.breezy" => BLOG_POST_PARTIAL,
82
+ "_profile.js.breezy" => PROFILE_PARTIAL,
83
+ "_record.js.breezy" => RECORD_PARTIAL,
84
+ "_footer.js.breezy" => FOOTER_PARTIAL,
85
+ "_nested.js.breezy" => NESTED_PARTIAL,
86
+ "_collection.js.breezy" => COLLECTION_PARTIAL,
87
+ "_flattened.js.breezy" => FLATTENED_PARTIAL,
88
+ "_raise_if_used.js.breezy" => RAISE_IF_USED_PARTIAL
89
+ }
90
+
91
+ ActionView::Template.register_template_handler :breezy, BreezyTemplate::Handler
92
+
93
+ BlogPost = Struct.new(:id, :body, :author_name)
94
+ Collection = Struct.new(:id, :name)
95
+ blog_authors = [ "David Heinemeier Hansson", "Pavel Pravosud" ].cycle
96
+ BLOG_POST_COLLECTION = Array.new(10){ |i| BlogPost.new(i+1, "post body #{i+1}", blog_authors.next) }
97
+ COLLECTION_COLLECTION = Array.new(5){ |i| Collection.new(i+1, "collection #{i+1}") }
98
+
99
+ class BreezyTemplateTestCase < ActionView::TestCase
100
+ setup do
101
+ self.request_forgery = false
102
+ BreezyTemplate.configuration.track_sprockets_assets = []
103
+ BreezyTemplate.configuration.track_pack_assets = []
104
+
105
+ # this is a stub. Normally this would be set by the
106
+ # controller locals
107
+ self.breezy = {}
108
+
109
+ @context = self
110
+ Rails.cache.clear
111
+ end
112
+
113
+ teardown do
114
+ # Mocha didn't auto teardown??
115
+ Mocha::Mockery.teardown
116
+ end
117
+
118
+ cattr_accessor :request_forgery, :breezy
119
+ self.request_forgery = false
120
+
121
+ def breezy_filter
122
+ @breezy_filter
123
+ end
124
+
125
+ def asset_pack_path(asset)
126
+ return asset
127
+ end
128
+
129
+ def strip_format(str)
130
+ str.strip_heredoc.gsub(/\n\s*/, "")
131
+ end
132
+
133
+ def request
134
+ @request
135
+ end
136
+
137
+ # Stub out a couple of methods that'll get called from cache_fragment_name
138
+ def view_cache_dependencies
139
+ []
140
+ end
141
+
142
+ def jbuild(source, opts={})
143
+ @breezy_filter = opts[:breezy_filter]
144
+ @request = opts[:request] || action_controller_test_request
145
+ @rendered = []
146
+ partials = PARTIALS.clone
147
+ partials["test.js.breezy"] = source
148
+ resolver = ActionView::FixtureResolver.new(partials)
149
+ lookup_context.view_paths = [resolver]
150
+ lookup_context.formats = [:js]
151
+ template = ActionView::Template.new(source, "test", BreezyTemplate::Handler, virtual_path: "test")
152
+ template.render(self, {}).strip
153
+ end
154
+
155
+ def action_controller_test_request
156
+ if ::Rails.version.start_with?('5.2')
157
+ ::ActionController::TestRequest.create({})
158
+ elsif ::Rails.version.start_with?('5.1')
159
+ ::ActionController::TestRequest.create({})
160
+ elsif ::Rails.version.start_with?('5')
161
+ ::ActionController::TestRequest.create
162
+ else
163
+ ::ActionController::TestRequest.new
164
+ end
165
+ end
166
+
167
+ def cache_keys
168
+ major_v = Rails::VERSION::MAJOR
169
+ minor_v = Rails::VERSION::MINOR
170
+ rails_v = "rails#{major_v}#{minor_v}"
171
+ path = File.expand_path("../fixtures/cache_keys.yaml", __FILE__)
172
+ keys = YAML.load_file(path)
173
+ keys[method_name][rails_v]
174
+ end
175
+
176
+ def undef_context_methods(*names)
177
+ self.class_eval do
178
+ names.each do |name|
179
+ undef_method name.to_sym if method_defined?(name.to_sym)
180
+ end
181
+ end
182
+ end
183
+
184
+ def form_authenticity_token
185
+ "secret"
186
+ end
187
+ end
26
188
 
27
- require 'breezy_template/core_ext'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breezy_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-02 00:00:00.000000000 Z
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -102,8 +102,12 @@ files:
102
102
  - lib/breezy_template/partial_extension.rb
103
103
  - lib/breezy_template/search_extension.rb
104
104
  - lib/breezy_template/var.rb
105
+ - test/cache_extension_test.rb
106
+ - test/deferement_test.rb
105
107
  - test/dependency_tracker_test.rb
106
108
  - test/extensions_test.rb
109
+ - test/partial_extension_test.rb
110
+ - test/search_extension_test.rb
107
111
  - test/template_test.rb
108
112
  - test/test_helper.rb
109
113
  homepage: https://github.com/jho406/breezy/
@@ -131,7 +135,11 @@ signing_key:
131
135
  specification_version: 4
132
136
  summary: Breezy Templates for React props
133
137
  test_files:
138
+ - test/search_extension_test.rb
139
+ - test/partial_extension_test.rb
134
140
  - test/dependency_tracker_test.rb
135
141
  - test/extensions_test.rb
142
+ - test/deferement_test.rb
143
+ - test/cache_extension_test.rb
136
144
  - test/template_test.rb
137
145
  - test/test_helper.rb