-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstimulus_spec.rb
135 lines (112 loc) · 4.35 KB
/
stimulus_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
gem 'minitest'
require 'minitest/autorun'
require 'ruby2js/filter/stimulus'
describe Ruby2JS::Filter::Stimulus do
def to_js(string)
_(Ruby2JS.convert(string, eslevel: 2022,
filters: [Ruby2JS::Filter::Stimulus]).to_s)
end
def to_js_esm(string)
_(Ruby2JS.convert(string, eslevel: 2022,
filters: [Ruby2JS::Filter::Stimulus, Ruby2JS::Filter::ESM]).to_s)
end
def to_js_skypack(string)
_(Ruby2JS.convert(string, eslevel: 2022, import_from_skypack: true,
filters: [Ruby2JS::Filter::Stimulus, Ruby2JS::Filter::ESM]).to_s)
end
describe "class aliases" do
it "handles ruby scope syntax" do
to_js( 'class Foo<Stimulus::Controller; end' ).
must_equal 'class Foo extends Stimulus.Controller {}'
end
it "handles JS scope syntax" do
to_js( 'class Foo<Stimulus.Controller; end' ).
must_equal 'class Foo extends Stimulus.Controller {}'
end
it "handles shorthand" do
to_js( 'class Foo<Stimulus; end' ).
must_equal 'class Foo extends Stimulus.Controller {}'
end
end
describe "common stimulus properties" do
it "must handle application property" do
to_js( 'class Foo<Stimulus; def reg(); application.register("x", Class.new {}); end; end' ).
must_include 'this.application.register("x", class {})'
end
it "must handle element property" do
to_js( 'class Foo<Stimulus; def clear(); element.textContent = ""; end; end' ).
must_include 'this.element.textContent = ""'
end
end
describe "initialize method" do
it "must NOT map initialize to constructor" do
to_js( 'class Foo<Stimulus; def initialize(); @index=0; end; end' ).
must_include 'initialize() {this.#index = 0}'
end
end
describe "targets" do
it "should handle xTarget" do
to_js( 'class Foo<Stimulus; def bar(); xTarget; end; end' ).
must_include 'static targets = ["x"]; bar() {this.xTarget}'
end
it "should handle xTargets" do
to_js( 'class Foo<Stimulus; def bar(); xTargets; end; end' ).
must_include 'static targets = ["x"]; bar() {this.xTargets}'
end
it "should handle hasXTarget" do
to_js( 'class Foo<Stimulus; def bar(); hasXTarget; end; end' ).
must_include 'static targets = ["x"]; bar() {this.hasXTarget}'
end
end
describe "values" do
it "should handle xValue" do
to_js( 'class Foo<Stimulus; def bar(); xValue; end; end' ).
must_include 'static values = {x: String}; bar() {this.xValue}'
end
it "should handle xValue=1" do
to_js( 'class Foo<Stimulus; def bar(); xValue=1; end; end' ).
must_include 'static values = {x: String}; bar() {this.xValue = 1}'
end
it "should handle hasXValue" do
to_js( 'class Foo<Stimulus; def bar(); hasXValue; end; end' ).
must_include 'static values = {x: String}; bar() {this.hasXValue}'
end
it "should not override value type" do
to_js( 'class Foo<Stimulus; self.values = {x: Numeric}; def bar(); hasXValue; end; end' ).
must_include 'static values = {x: Numeric}; bar() {this.hasXValue}'
end
end
describe "classes" do
it "should handle xClass" do
to_js( 'class Foo<Stimulus; def bar(); xClass; end; end' ).
must_include 'static classes = ["x"]; bar() {this.xClass}'
end
it "should handle hasXClass" do
to_js( 'class Foo<Stimulus; def bar(); hasXClass; end; end' ).
must_include 'static classes = ["x"]; bar() {this.hasXClass}'
end
end
describe "inheritance" do
it "must inherit element property" do
to_js( 'class Base<Stimulus; end; class Foo< Base; def clear(); element.textContent = ""; end; end' ).
must_include 'this.element.textContent = ""'
end
end
describe "modules" do
it "imports from Stimulus" do
to_js_esm( 'class Foo<Stimulus::Controller; end' ).
must_equal 'import * as Stimulus from "@hotwired/stimulus"; ' +
'class Foo extends Stimulus.Controller {}'
end
it "imports from skypack" do
to_js_skypack( 'class Foo<Stimulus::Controller; end' ).
must_equal 'import * as Stimulus from "https://cdn.skypack.dev/@hotwired/stimulus"; ' +
'class Foo extends Stimulus.Controller {}'
end
end
describe Ruby2JS::Filter::DEFAULTS do
it "should include Stimulus" do
_(Ruby2JS::Filter::DEFAULTS).must_include Ruby2JS::Filter::Stimulus
end
end
end