|
150 | 150 | let(:template) { "{{#{name} name name=name}}" } |
151 | 151 |
|
152 | 152 | before do |
153 | | - allow(function).to receive(:call).with(any_args).and_call_original |
154 | | - engine.register_helper(name, &function) |
| 153 | + engine.register_helper(name, function) |
155 | 154 | end |
156 | 155 |
|
157 | 156 | it "is defined" do |
158 | 157 | expect(engine).to respond_to(:register_helper) |
159 | 158 | end |
160 | 159 |
|
161 | 160 | context "with positional parameters" do |
162 | | - before do |
163 | | - engine.register_helper(name, &function) |
| 161 | + context "when function is argument" do |
| 162 | + before do |
| 163 | + engine.register_helper(name, function) |
| 164 | + end |
| 165 | + |
| 166 | + describe "rendering" do |
| 167 | + include_examples "rendering" |
| 168 | + end |
164 | 169 | end |
165 | 170 |
|
166 | | - describe "rendering" do |
167 | | - include_examples "rendering" |
| 171 | + context "when function is block" do |
| 172 | + before do |
| 173 | + engine.register_helper(name, &function) |
| 174 | + end |
| 175 | + |
| 176 | + describe "rendering" do |
| 177 | + include_examples "rendering" |
| 178 | + end |
168 | 179 | end |
169 | 180 | end |
170 | 181 |
|
|
178 | 189 | end |
179 | 190 | end |
180 | 191 |
|
181 | | - describe "parameters" do |
| 192 | + context "with a Ruby function" do |
| 193 | + before do |
| 194 | + allow(function).to receive(:call).with(any_args).and_call_original |
| 195 | + end |
| 196 | + |
182 | 197 | describe "the first parameter" do |
183 | 198 | it "is the context" do |
184 | 199 | render_context.transform_keys!(&:to_s) |
|
233 | 248 | end |
234 | 249 | end |
235 | 250 | end |
| 251 | + |
| 252 | + context "with a JavaScript function" do |
| 253 | + let(:function) { |
| 254 | + <<~JS |
| 255 | + function (...args) { |
| 256 | + args.unshift(this); |
| 257 | + return tester(...args); |
| 258 | + } |
| 259 | + JS |
| 260 | + } |
| 261 | + let(:tester) { ->(_ctx, *_args, _opts) { rendered } } |
| 262 | + |
| 263 | + before do |
| 264 | + allow(tester).to receive(:call).with(any_args).and_call_original |
| 265 | + engine_context.attach("tester", tester) |
| 266 | + end |
| 267 | + |
| 268 | + describe "rendering" do |
| 269 | + include_examples "rendering" |
| 270 | + end |
| 271 | + |
| 272 | + describe "`this`" do |
| 273 | + it "is the context" do |
| 274 | + render_context.transform_keys!(&:to_s) |
| 275 | + args = [render_context, any_args, anything] |
| 276 | + render |
| 277 | + expect(tester).to have_received(:call).with(*args) |
| 278 | + end |
| 279 | + end |
| 280 | + |
| 281 | + describe "the first parameter(s)" do |
| 282 | + it "is the positional argument(s)" do |
| 283 | + args = [anything, *render_context.values_at(:name), anything] |
| 284 | + render |
| 285 | + expect(tester).to have_received(:call).with(*args) |
| 286 | + end |
| 287 | + end |
| 288 | + |
| 289 | + describe "the last parameter" do |
| 290 | + it "is the options" do |
| 291 | + opts = include( |
| 292 | + "data" => kind_of(Hash), |
| 293 | + "hash" => { "name" => render_context[:name] }, |
| 294 | + "name" => name.to_s, |
| 295 | + ) |
| 296 | + args = [anything, any_args, opts] |
| 297 | + render |
| 298 | + expect(tester).to have_received(:call).with(*args) |
| 299 | + end |
| 300 | + end |
| 301 | + |
| 302 | + context "with a block helper" do |
| 303 | + let(:template) { "{{##{name} age}}function{{else}}inverse{{/#{name}}}" } |
| 304 | + let(:function) { |
| 305 | + <<~JS |
| 306 | + function (age, opts) { |
| 307 | + return age > 0 ? opts.fn() : opts.inverse(); |
| 308 | + } |
| 309 | + JS |
| 310 | + } |
| 311 | + |
| 312 | + describe "the options" do |
| 313 | + it "includes the main block function" do |
| 314 | + render_context[:age] = 30 |
| 315 | + expect(render).to eq("function") |
| 316 | + end |
| 317 | + |
| 318 | + it "includes the else block function" do |
| 319 | + render_context[:age] = 0 |
| 320 | + expect(render).to eq("inverse") |
| 321 | + end |
| 322 | + end |
| 323 | + end |
| 324 | + end |
236 | 325 | end |
237 | 326 |
|
238 | 327 | describe "#unregister_helper" do |
|
0 commit comments