Skip to content

Commit 08e5de5

Browse files
committed
Specs improvements
1 parent 791a144 commit 08e5de5

File tree

2 files changed

+101
-106
lines changed

2 files changed

+101
-106
lines changed

spec/dummy/app/admin/posts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
148148
add_field(f, :data_field_271, :boolean, df271)
149149

150150
# --- addStyle
151-
df281 = { if: 'checked', then: 'addStyle font-size: 10px; color: red', target: '#post_data_field_281' }
151+
df281 = { if: 'checked', then: 'addStyle font-size: 10px; padding: 3px', target: '#post_data_field_281' }
152152
add_field(f, :data_field_281, :boolean, df281, {}, { 'style': 'margin-right: 20px' })
153153

154154
# --- gtarget

spec/system/dynamic_fields_spec.rb

Lines changed: 100 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -13,162 +13,157 @@
1313
author.destroy
1414
end
1515

16-
context 'with some dynamic fields' do
17-
it 'checks the conditions and actions' do
18-
visit "/admin/posts/#{post.id}/edit"
16+
def apply_action(action, inverse: false)
17+
case action[0]
18+
when :click
19+
find(action[1]).click
20+
when :fill
21+
fill_in(action[1], with: inverse ? '' : action[2])
22+
find('body').click
23+
when :select
24+
select(inverse ? '' : action[2], from: action[1])
25+
end
26+
end
1927

20-
expect(page).to have_css('#post_data_field_111[data-if="checked"][data-then="addClass red"][data-target="#post_data_field_111_input label"]') # rubocop:disable Layout/LineLength
28+
def spec_message(string)
29+
RSpec.configuration.reporter.message(string)
30+
end
2131

22-
# --- if
23-
expect(page).not_to have_css('#post_data_field_111_input label.red')
24-
find('#post_data_field_111').click
25-
expect(page).to have_css('#post_data_field_111_input label.red')
32+
def test_set_css(target, options = {})
33+
spec_message("test set#{options[:one_way] ? '' : '/unset'} CSS on #{target} ...")
2634

27-
expect(page).to have_css('#post_data_field_121_input label.red')
28-
find('#post_data_field_121').click
29-
expect(page).not_to have_css('#post_data_field_121_input label.red')
35+
expect(page).not_to have_css(target)
36+
block_given? ? yield : apply_action(options[:action])
37+
expect(page).to have_css(target)
38+
return if options[:one_way]
3039

31-
expect(page).to have_css('#post_data_field_132_input label.red')
32-
fill_in('post_data_field_132', with: 'something')
33-
find('body').click
34-
expect(page).not_to have_css('#post_data_field_132_input label.red')
40+
block_given? ? yield : apply_action(options[:action], inverse: true)
41+
expect(page).not_to have_css(target)
42+
end
3543

36-
expect(page).not_to have_css('#post_data_field_141_input label.red')
37-
fill_in('post_data_field_141', with: 'something')
38-
find('body').click
39-
expect(page).to have_css('#post_data_field_141_input label.red')
44+
def test_unset_css(target, options = {})
45+
spec_message("test unset#{options[:one_way] ? '' : '/set'} CSS on #{target} ...")
4046

41-
expect(page).not_to have_css('#post_data_field_142_input label.red')
42-
fill_in('post_data_field_142', with: 'something')
43-
find('body').click
44-
expect(page).to have_css('#post_data_field_142_input label.red')
47+
expect(page).to have_css(target)
48+
block_given? ? yield : apply_action(options[:action])
49+
expect(page).not_to have_css(target)
50+
return if options[:one_way]
4551

46-
expect(page).not_to have_css('#post_data_field_151_input label.red')
47-
find('#post_data_field_151').click
48-
expect(page).to have_css('#post_data_field_151_input label.red')
52+
block_given? ? yield : apply_action(options[:action], inverse: true)
53+
expect(page).to have_css(target)
54+
end
4955

50-
expect(page).not_to have_css('#post_data_field_152_input label.red')
51-
fill_in('post_data_field_152', with: 'something')
52-
find('body').click
53-
expect(page).to have_css('#post_data_field_152_input label.red')
56+
def test_change_css(target, attrs1, attrs2, options = {})
57+
spec_message("test change CSS on #{target} ...")
5458

55-
expect(page).not_to have_css('#post_data_field_153_input label.red')
56-
fill_in('post_data_field_153', with: 'something')
57-
find('body').click
58-
expect(page).to have_css('#post_data_field_153_input label.red')
59+
expect(page).to have_css(target, attrs1)
60+
block_given? ? yield : apply_action(options[:action])
61+
expect(page).to have_css(target, attrs2)
62+
return if options[:one_way]
5963

60-
# --- eq
61-
expect(page).not_to have_css('#post_data_field_161_input label.red')
62-
fill_in('post_data_field_161', with: '161')
63-
find('body').click
64-
expect(page).to have_css('#post_data_field_161_input label.red')
64+
block_given? ? yield : apply_action(options[:action], inverse: true)
65+
expect(page).to have_css(target, attrs1)
66+
end
6567

66-
expect(page).not_to have_css('#post_data_field_162_input label.red')
67-
select('162', from: 'post_data_field_162')
68-
expect(page).to have_css('#post_data_field_162_input label.red')
68+
context 'with some dynamic fields' do
69+
it 'checks the conditions and actions' do
70+
visit "/admin/posts/#{post.id}/edit"
6971

70-
expect(page).not_to have_css('#post_data_field_163_input label.red')
71-
fill_in('post_data_field_163', with: '163')
72-
find('body').click
73-
expect(page).to have_css('#post_data_field_163_input label.red')
72+
expect(page).to have_css('#post_data_field_111[data-if="checked"][data-then="addClass red"][data-target="#post_data_field_111_input label"]') # rubocop:disable Layout/LineLength
7473

75-
# --- not
76-
expect(page).to have_css('#post_data_field_171_input label.red')
77-
fill_in('post_data_field_171', with: '171')
78-
find('body').click
79-
expect(page).not_to have_css('#post_data_field_171_input label.red')
74+
# --- if
75+
spec_message('check data-if condition')
76+
test_set_css('#post_data_field_111_input label.red', action: [:click, '#post_data_field_111'])
77+
# test_unset_css('#post_data_field_112_input label.red', action: [:click, '#post_data_field_112'])
78+
test_unset_css('#post_data_field_121_input label.red', action: [:click, '#post_data_field_121'])
79+
test_unset_css('#post_data_field_131_input label.red', action: [:fill, 'post_data_field_131', 'something'])
80+
test_unset_css('#post_data_field_132_input label.red', action: [:fill, 'post_data_field_132', 'something'])
81+
test_set_css('#post_data_field_141_input label.red', action: [:fill, 'post_data_field_141', 'something'])
82+
test_set_css('#post_data_field_142_input label.red', action: [:fill, 'post_data_field_142', 'something'])
83+
test_set_css('#post_data_field_151_input label.red', one_way: true, action: [:click, '#post_data_field_151'])
84+
action = [:fill, 'post_data_field_152', 'something']
85+
test_set_css('#post_data_field_152_input label.red', one_way: true, action: action)
86+
action = [:fill, 'post_data_field_153', 'something']
87+
test_set_css('#post_data_field_153_input label.red', one_way: true, action: action)
8088

81-
expect(page).to have_css('#post_data_field_172_input label.red')
82-
select('172', from: 'post_data_field_172')
83-
expect(page).not_to have_css('#post_data_field_172_input label.red')
89+
# --- eq
90+
spec_message('check data-eq condition')
91+
test_set_css('#post_data_field_161_input label.red', action: [:fill, 'post_data_field_161', '161'])
92+
test_set_css('#post_data_field_162_input label.red', action: [:select, 'post_data_field_162', '162'])
93+
test_set_css('#post_data_field_163_input label.red', action: [:fill, 'post_data_field_163', '163'])
94+
# test_unset_css('#post_data_field_164_input label.red', action: [:fill, 'post_data_field_164', '164'])
8495

85-
expect(page).to have_css('#post_data_field_173_input label.red')
86-
fill_in('post_data_field_173', with: '173')
87-
find('body').click
88-
expect(page).not_to have_css('#post_data_field_173_input label.red')
96+
# --- not
97+
spec_message('check data-not condition')
98+
test_unset_css('#post_data_field_171_input label.red', action: [:fill, 'post_data_field_171', '171'])
99+
test_unset_css('#post_data_field_172_input label.red', action: [:select, 'post_data_field_172', '172'])
100+
test_unset_css('#post_data_field_173_input label.red', action: [:fill, 'post_data_field_173', '173'])
89101

90102
# --- match
91-
expect(page).not_to have_css('#post_data_field_181_input label.red')
92-
fill_in('post_data_field_181', with: ' Something new ...')
93-
find('body').click
94-
expect(page).to have_css('#post_data_field_181_input label.red')
103+
spec_message('check data-match condition')
104+
test_set_css('#post_data_field_181_input label.red', action: [:fill, 'post_data_field_181', ' Something new ...'])
95105

96106
# --- mismatch
97-
expect(page).to have_css('#post_data_field_191_input label.red')
98-
fill_in('post_data_field_191', with: '1234')
99-
find('body').click
100-
expect(page).not_to have_css('#post_data_field_191_input label.red')
107+
spec_message('check data-mismatch condition')
108+
test_unset_css('#post_data_field_191_input label.red', action: [:fill, 'post_data_field_191', '1234'])
101109

102110
# --- function
103-
expect(page).not_to have_css('#post_data_field_201_input label.red')
104-
fill_in('post_data_field_201', with: 'test')
105-
find('body').click
106-
expect(page).to have_css('#post_data_field_201_input label.red')
107-
111+
spec_message('check data-function condition')
112+
test_set_css('#post_data_field_201_input label.red', action: [:fill, 'post_data_field_201', 'test'])
108113
expect(page).to have_css('#post_data_field_202[data-df-errors="custom function not found"]')
109-
110-
expect(page).to have_css('#post_data_field_203.red')
111-
find('#post_data_field_203').click
112-
expect(page).not_to have_css('#post_data_field_203.red')
114+
test_unset_css('#post_data_field_203.red', action: [:click, '#post_data_field_203'])
113115

114116
# --- addClass
115-
expect(page).not_to have_css('#post_data_field_211_input label.red')
116-
find('#post_data_field_211').click
117-
expect(page).to have_css('#post_data_field_211_input label.red')
118-
find('#post_data_field_211').click
119-
expect(page).not_to have_css('#post_data_field_211_input label.red')
117+
spec_message('check data-then="addClass ..." action')
118+
test_set_css('#post_data_field_211_input label.red', action: [:click, '#post_data_field_211'])
120119

121120
# --- callback
122-
expect(page).not_to have_css('body.test_callback_arg')
123-
find('#post_data_field_221').click
124-
expect(page).to have_css('body.test_callback_arg')
125-
121+
spec_message('check data-then="callback ..." action')
122+
test_set_css('body.test_callback_arg', one_way: true, action: [:click, '#post_data_field_221'])
126123
find('#post_data_field_222').click
127124
expect(page).to have_css('#post_data_field_222[data-df-errors="callback function not found"]')
128125

129126
# --- setValue
127+
spec_message('check data-then="setValue ..." action')
130128
expect(find('#post_data_test').value).to be_empty
131129
find('#post_data_field_231').click
132130
expect(find('#post_data_test').value).to eq 'data test'
133131

134132
# --- hide
135-
expect(page).to have_css('#post_data_field_241_input .inline-hints', visible: :visible)
136-
find('#post_data_field_241').click
137-
expect(page).to have_css('#post_data_field_241_input .inline-hints', visible: :hidden)
133+
spec_message('check data-then="hide" action')
134+
target = '#post_data_field_241_input .inline-hints'
135+
test_change_css(target, { visible: :visible }, { visible: :hidden }, action: [:click, '#post_data_field_241'])
138136

139137
# --- fade
140-
expect(page).to have_css('#post_data_field_251_input .inline-hints', visible: :visible)
141-
find('#post_data_field_251').click
142-
expect(page).to have_css('#post_data_field_251_input .inline-hints', visible: :hidden)
138+
spec_message('check data-then="fade" action')
139+
target = '#post_data_field_251_input .inline-hints'
140+
test_change_css(target, { visible: :visible }, { visible: :hidden }, action: [:click, '#post_data_field_251'])
143141

144142
# --- slide
145-
expect(page).to have_css('#post_data_field_261_input .inline-hints', visible: :visible)
146-
find('#post_data_field_261').click
147-
expect(page).to have_css('#post_data_field_261_input .inline-hints', visible: :hidden)
143+
spec_message('check data-then="slide" action')
144+
target = '#post_data_field_261_input .inline-hints'
145+
test_change_css(target, { visible: :visible }, { visible: :hidden }, action: [:click, '#post_data_field_261'])
148146

149147
# --- setText
148+
spec_message('check data-then="setText ..." action')
150149
expect(find('#post_data_field_271_input .inline-hints').text).not_to eq 'data test'
151150
find('#post_data_field_271').click
152-
expect(find('#post_data_field_271_input .inline-hints').text).to eq 'data test'
151+
expect(page).to have_css('#post_data_field_271_input .inline-hints', text: 'data test')
153152

154153
# --- addStyle
155-
expect(find('#post_data_field_281')[:style]).to eq 'margin-right: 20px;'
156-
find('#post_data_field_281').click
157-
expect(find('#post_data_field_281')[:style]).to eq 'margin-right: 20px; font-size: 10px; color: red;'
158-
find('#post_data_field_281').click
159-
expect(find('#post_data_field_281')[:style]).to eq 'margin-right: 20px;'
154+
spec_message('check data-then="addStyle ..." action')
155+
style1 = { style: { 'margin-right': '20px' } }
156+
style2 = { style: 'margin-right: 20px; font-size: 10px; padding: 3px' }
157+
test_change_css('#post_data_field_281', style1, style2, action: [:click, '#post_data_field_281'])
160158

161159
# --- gtarget
162-
expect(page).not_to have_css('body.active_admin.red')
163-
find('#post_data_field_301').click
164-
expect(page).to have_css('body.active_admin.red')
165-
find('#post_data_field_301').click
166-
expect(page).not_to have_css('body.active_admin.red')
167-
160+
spec_message('check data-gtarget="..."')
161+
test_set_css('body.active_admin.red', action: [:click, '#post_data_field_301'])
168162
find('#post_data_field_302').click # checks that using simply "target" will not work
169163
expect(page).not_to have_css('body.active_admin.red')
170164

171165
# --- else
166+
spec_message('check data-else="..."')
172167
expect(page).not_to have_css('#post_data_field_321_input label.red')
173168
expect(page).to have_css('#post_data_field_321_input label.green')
174169
find('#post_data_field_321').click

0 commit comments

Comments
 (0)