forked from railslove/rack-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_helper_spec.rb
More file actions
51 lines (39 loc) · 1.59 KB
/
javascript_helper_spec.rb
File metadata and controls
51 lines (39 loc) · 1.59 KB
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
RSpec.describe Rack::Tracker do
let(:escaper) do
Class.new { include Rack::Tracker::JavaScriptHelper }.new
end
subject { escaper }
# Instance methods are mixed in
it { should respond_to :escape_javascript }
it { should respond_to :j }
context 'with plain values' do
it 'should return untouched values' do
expect(escaper.j('Hello')).to eq 'Hello'
end
end
# The following is a RSpec port of original rails's test suite for #j method.
# https://github.com/rails/rails/blob/master/actionview/test/template/javascript_helper_test.rb
describe '#escape_javascript' do
# Simple matcher to shorten specs
matcher :escape do |value|
match do |actual|
raise ArgumentError, 'macther syntax is escape("some value").to("expected value")' unless @expected
actual.j(value) == @expected
end
# Syntactic sugar for matcher
chain :to do |to|
@expected = to
self
end
end
it { should escape(nil).to '' }
it { should escape(%(This "thing" is really\n netos')).to %(This \\"thing\\" is really\\n netos\\') }
it { should escape(%(backslash\\test)).to %(backslash\\\\test) }
it { should escape(%(dont </close> tags)).to %(dont <\\/close> tags) }
it { should escape(%(unicode \342\200\250 newline).force_encoding(Encoding::UTF_8).encode!).to %(unicode 
 newline) }
it { should escape(%(unicode \342\200\251 newline).force_encoding(Encoding::UTF_8).encode!).to %(unicode 
 newline) }
it 'works with symbols' do
expect(subject).to escape(:dimension1).to 'dimension1'
end
end
end