|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require "logger" |
3 | 4 | require "tempfile" |
4 | 5 |
|
5 | 6 | RSpec.describe Handlebars::Engine do |
6 | 7 | let(:engine) { described_class.new(**engine_options) } |
7 | 8 | let(:engine_context) { engine.instance_variable_get(:@context) } |
8 | 9 | let(:engine_options) { {} } |
| 10 | + let(:log) { Tempfile.new } |
| 11 | + let(:logger) { Logger.new(log, level: Logger::FATAL) } |
9 | 12 | let(:render) { renderer.call(render_context, render_options) } |
10 | 13 | let(:render_context) { { name: "Zach", age: 30 } } |
11 | 14 | let(:render_options) { {} } |
|
40 | 43 | end |
41 | 44 |
|
42 | 45 | it "does not create the context" do |
43 | | - expect(engine_context).to be nil |
| 46 | + expect(engine_context).to be_nil |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "when `logger` is defined" do |
| 51 | + before do |
| 52 | + engine_options[:logger] = logger |
| 53 | + logger.debug! |
| 54 | + end |
| 55 | + |
| 56 | + it "logs initialization" do |
| 57 | + engine |
| 58 | + log.rewind |
| 59 | + expect(log.read).to include("[handlebars] initializing") |
| 60 | + end |
| 61 | + |
| 62 | + it "logs javascript" do |
| 63 | + engine.send(:evaluate, "console.log('js', 'log')") |
| 64 | + log.rewind |
| 65 | + expect(log.read).to include("[handlebars] js log") |
44 | 66 | end |
45 | 67 | end |
46 | 68 |
|
|
255 | 277 | describe "the options" do |
256 | 278 | it "includes the main block function" do |
257 | 279 | opts = include( |
258 | | - "fn" => kind_of(MiniRacer::JavaScriptFunction), |
| 280 | + "fn" => "function", # kind_of(MiniRacer::JavaScriptFunction), |
259 | 281 | ) |
260 | 282 | args = [anything, any_args, opts] |
261 | 283 | render |
|
264 | 286 |
|
265 | 287 | it "includes the else block function" do |
266 | 288 | opts = include( |
267 | | - "inverse" => kind_of(MiniRacer::JavaScriptFunction), |
| 289 | + "inverse" => "function", # kind_of(MiniRacer::JavaScriptFunction), |
268 | 290 | ) |
269 | 291 | args = [anything, any_args, opts] |
270 | 292 | render |
|
279 | 301 | <<~JS |
280 | 302 | function (...args) { |
281 | 303 | args.unshift(this); |
| 304 | + const { ...options } = args[args.length-1]; |
| 305 | + Object.entries(options).forEach(([key, value]) => { |
| 306 | + if (typeof value === "function") { |
| 307 | + // functions are cannot be passed back to Ruby |
| 308 | + options[key] = "function"; |
| 309 | + } |
| 310 | + }); |
| 311 | + args[args.length-1] = options |
282 | 312 | return tester(...args); |
283 | 313 | } |
284 | 314 | JS |
|
0 commit comments