/
handlebars_filter_spec.rb
73 lines (58 loc) · 2.33 KB
/
handlebars_filter_spec.rb
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
describe "HandlebarsFilter" do
HandlebarsFilter ||= Rake::Pipeline::Web::Filters::HandlebarsFilter
MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
MemoryManifest ||= Rake::Pipeline::SpecHelpers::MemoryManifest
let(:handlebars_input) {
'<h1 class="title">{{title}}</h1>'
}
let(:expected_output) {
"Ember.TEMPLATES['test']=Ember.Handlebars.compile(\"<h1 class=\\\"title\\\">{{title}}</h1>\");"
}
def input_file(name, content)
MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
end
def output_file(name)
MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
end
def setup_filter(filter, input_filename = "test.handlebars")
filter.file_wrapper_class = MemoryFileWrapper
filter.manifest = MemoryManifest.new
filter.last_manifest = MemoryManifest.new
filter.input_files = [input_file(input_filename, handlebars_input)]
filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new
filter
end
it "generates output" do
filter = setup_filter HandlebarsFilter.new
filter.output_files.should == [output_file("test.js")]
tasks = filter.generate_rake_tasks
tasks.each(&:invoke)
file = MemoryFileWrapper.files["/path/to/output/test.js"]
file.body.should == expected_output
file.encoding.should == "UTF-8"
end
describe "naming output files" do
it "translates .handlebars extensions to .js by default" do
filter = setup_filter HandlebarsFilter.new, "test.handlebars"
filter.output_files.first.path.should == "test.js"
end
it "translates .hbs extensions to .js by default" do
filter = setup_filter HandlebarsFilter.new, "test.hbs"
filter.output_files.first.path.should == "test.js"
end
it "accepts a block to customize output file names" do
filter = setup_filter(HandlebarsFilter.new { |input| "squid" })
filter.output_files.first.path.should == "squid"
end
end
describe "options" do
it "should allow an option to name the key" do
filter = setup_filter(HandlebarsFilter.new(:key_name_proc => proc { |input| "new_name_key" }))
tasks = filter.generate_rake_tasks
tasks.each(&:invoke)
file = MemoryFileWrapper.files["/path/to/output/test.js"]
file.body.should =~ /^Ember\.TEMPLATES\['new_name_key'\]/
end
end
end