Skip to content

Commit 278d49f

Browse files
committed
stimulus filter tests
1 parent 289c3a4 commit 278d49f

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

lib/ruby2js/filter/stimulus.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,17 @@ def stim_walk(node)
139139
if child.type == :send and child.children.length == 2 and
140140
[nil, s(:self), s(:send, nil, :this)].include? child.children[0]
141141

142-
if child.children[1] =~ /^(\w+)Targets?$/
142+
if child.children[1] =~ /^has([A-Z]\w*)(Target|Value|Class)$/
143+
name = s(:str, $1[0].downcase + $1[1..-1])
144+
@stim_targets.add name if $2 == 'Target'
145+
@stim_values.add name if $2 == 'Value'
146+
@stim_classes.add name if $2 == 'Class'
147+
elsif child.children[1] =~ /^(\w+)Targets?$/
143148
@stim_targets.add s(:str, $1)
144149
elsif child.children[1] =~ /^(\w+)Value=?$/
145150
@stim_values.add s(:str, $1)
146151
elsif child.children[1] =~ /^(\w+)Class$/
147152
@stim_classes.add s(:str, $1)
148-
elsif child.children[1] =~ /^has([A-Z]\w*)(Target|Value|Class)$/
149-
name = s(:str, $1[0].downcase + $1[1..-1])
150-
@stim_targets.add name if $2 == 'Target'
151-
@stim_values.add name if $2 == 'Value'
152-
@stim_classes.add name if $2 == 'Class'
153153
end
154154

155155
elsif child.type == :send and child.children.length == 3 and

spec/stimulus_spec.rb

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
gem 'minitest'
2+
require 'minitest/autorun'
3+
require 'ruby2js/filter/stimulus'
4+
5+
describe Ruby2JS::Filter::Stimulus do
6+
7+
def to_js(string)
8+
_(Ruby2JS.convert(string, eslevel: 2022,
9+
filters: [Ruby2JS::Filter::Stimulus]).to_s)
10+
end
11+
12+
describe "class aliases" do
13+
it "handles ruby scope syntax" do
14+
to_js( 'class Foo<Stimulus::Controller; end' ).
15+
must_equal 'import Stimulus as * from "stimulus"; ' +
16+
'class Foo extends Stimulus.Controller {}'
17+
end
18+
19+
it "handles JS scope syntax" do
20+
to_js( 'class Foo<Stimulus.Controller; end' ).
21+
must_equal 'import Stimulus as * from "stimulus"; ' +
22+
'class Foo extends Stimulus.Controller {}'
23+
end
24+
25+
it "handles shorthand" do
26+
to_js( 'class Foo<Stimulus; end' ).
27+
must_equal 'import Stimulus as * from "stimulus"; ' +
28+
'class Foo extends Stimulus.Controller {}'
29+
end
30+
end
31+
32+
describe "common stimulus properties" do
33+
it "must handle application property" do
34+
to_js( 'class Foo<Stimulus; def reg(); application.register("x", Class.new {}); end; end' ).
35+
must_include 'this.application.register("x", class {})'
36+
end
37+
38+
it "must handle element property" do
39+
to_js( 'class Foo<Stimulus; def clear(); element.textContent = ""; end; end' ).
40+
must_include 'this.element.textContent = ""'
41+
end
42+
end
43+
44+
describe "initialize method" do
45+
it "must NOT map initialize to constructor" do
46+
to_js( 'class Foo<Stimulus; def initialize(); @index=0; end; end' ).
47+
must_include 'initialize() {this.#index = 0}'
48+
end
49+
end
50+
51+
describe "targets" do
52+
it "should handle xTarget" do
53+
to_js( 'class Foo<Stimulus; def bar(); xTarget; end; end' ).
54+
must_include 'static targets = ["x"]; bar() {this.xTarget}'
55+
end
56+
57+
it "should handle xTargets" do
58+
to_js( 'class Foo<Stimulus; def bar(); xTargets; end; end' ).
59+
must_include 'static targets = ["x"]; bar() {this.xTargets}'
60+
end
61+
62+
it "should handle hasXTarget" do
63+
to_js( 'class Foo<Stimulus; def bar(); hasXTarget; end; end' ).
64+
must_include 'static targets = ["x"]; bar() {this.hasXTarget}'
65+
end
66+
end
67+
68+
describe "values" do
69+
it "should handle xValue" do
70+
to_js( 'class Foo<Stimulus; def bar(); xValue; end; end' ).
71+
must_include 'static values = {x: String}; bar() {this.xValue}'
72+
end
73+
74+
it "should handle xValue=1" do
75+
to_js( 'class Foo<Stimulus; def bar(); xValue=1; end; end' ).
76+
must_include 'static values = {x: String}; bar() {this.xValue = 1}'
77+
end
78+
79+
it "should handle hasXValue" do
80+
to_js( 'class Foo<Stimulus; def bar(); hasXValue; end; end' ).
81+
must_include 'static values = {x: String}; bar() {this.hasXValue}'
82+
end
83+
84+
it "should not override value type" do
85+
to_js( 'class Foo<Stimulus; self.values = {x: Numeric}; def bar(); hasXValue; end; end' ).
86+
must_include 'static values = {x: Numeric}; bar() {this.hasXValue}'
87+
end
88+
end
89+
90+
describe "classes" do
91+
it "should handle xClass" do
92+
to_js( 'class Foo<Stimulus; def bar(); xClass; end; end' ).
93+
must_include 'static classes = ["x"]; bar() {this.xClass}'
94+
end
95+
96+
it "should handle hasXClass" do
97+
to_js( 'class Foo<Stimulus; def bar(); hasXClass; end; end' ).
98+
must_include 'static classes = ["x"]; bar() {this.hasXClass}'
99+
end
100+
end
101+
102+
describe "inheritance" do
103+
it "must inherit element property" do
104+
to_js( 'class Base<Stimulus; end; class Foo< Base; def clear(); element.textContent = ""; end; end' ).
105+
must_include 'this.element.textContent = ""'
106+
end
107+
end
108+
109+
describe Ruby2JS::Filter::DEFAULTS do
110+
it "should include Stimulus" do
111+
_(Ruby2JS::Filter::DEFAULTS).must_include Ruby2JS::Filter::Stimulus
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)