Skip to content

Commit bdd5d1e

Browse files
committed
Improve specs
1 parent bcabdd1 commit bdd5d1e

File tree

5 files changed

+276
-28
lines changed

5 files changed

+276
-28
lines changed

spec/dummy/app/admin/authors.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
hint: (object.avatar.attached? ? "Current: #{object.avatar.filename}" : nil)
5454
end
5555
f.has_many :profile, allow_destroy: true do |ff|
56-
ff.input :description
56+
dyn_description = { if: 'not_blank', then: 'addClass red', gtarget: 'body' }
57+
ff.input :description, input_html: { data: dyn_description }
5758
end
5859
f.actions
5960
end

spec/dummy/app/admin/posts.rb

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,112 @@
3838
end
3939

4040
form do |f|
41+
def add_field(form, name, type, data, override_options = {})
42+
options = { as: type, input_html: { data: data }, hint: data.inspect }
43+
options.merge! override_options
44+
form.input name, options
45+
end
46+
4147
f.inputs 'Post' do
4248
f.input :author
4349
f.input :title
44-
f.input :description, input_html: { data: { if: 'blank', then: 'setValue no title', target: '#post_category' } }
45-
f.input :category
46-
f.input :published, input_html: { data: { if: 'not_checked', then: 'hide', target: '.group1' } }
47-
f.input :dt, wrapper_html: { class: 'group1' }
48-
f.input :position, wrapper_html: { class: 'group1' }
50+
51+
f.input :data_test, as: :string
52+
53+
# --- if
54+
df111 = { if: 'checked', then: 'addClass red', target: '#post_data_field_111_input label' }
55+
add_field(f, :data_field_111, :boolean, df111)
56+
57+
df121 = { if: 'not_checked', then: 'addClass red', target: '#post_data_field_121_input label' }
58+
add_field(f, :data_field_121, :boolean, df121)
59+
60+
df131 = { if: 'blank', then: 'addClass red', target: '#post_data_field_131_input label' }
61+
add_field(f, :data_field_131, :string, df131)
62+
63+
df132 = { if: 'blank', then: 'addClass red', target: '#post_data_field_132_input label' }
64+
add_field(f, :data_field_132, :text, df132)
65+
66+
df141 = { if: 'not_blank', then: 'addClass red', target: '#post_data_field_141_input label' }
67+
add_field(f, :data_field_141, :string, df141)
68+
69+
df142 = { if: 'not_blank', then: 'addClass red', target: '#post_data_field_142_input label' }
70+
add_field(f, :data_field_142, :text, df142)
71+
72+
df151 = { if: 'changed', then: 'addClass red', target: '#post_data_field_151_input label' }
73+
add_field(f, :data_field_151, :boolean, df151)
74+
75+
df152 = { if: 'changed', then: 'addClass red', target: '#post_data_field_152_input label' }
76+
add_field(f, :data_field_152, :string, df152)
77+
78+
df153 = { if: 'changed', then: 'addClass red', target: '#post_data_field_153_input label' }
79+
add_field(f, :data_field_153, :text, df153)
80+
81+
# --- eq
82+
df161 = { eq: '161', then: 'addClass red', target: '#post_data_field_161_input label' }
83+
add_field(f, :data_field_161, :string, df161)
84+
85+
df162 = { eq: '162', then: 'addClass red', target: '#post_data_field_162_input label' }
86+
add_field(f, :data_field_162, :select, df162, collection: [161, 162, 163])
87+
88+
df163 = { eq: '163', then: 'addClass red', target: '#post_data_field_163_input label' }
89+
add_field(f, :data_field_163, :text, df163)
90+
91+
# --- not
92+
df181 = { not: '181', then: 'addClass red', target: '#post_data_field_181_input label' }
93+
add_field(f, :data_field_181, :string, df181)
94+
95+
df182 = { not: '182', then: 'addClass red', target: '#post_data_field_182_input label' }
96+
add_field(f, :data_field_182, :select, df182, collection: [181, 182, 183])
97+
98+
df183 = { not: '183', then: 'addClass red', target: '#post_data_field_183_input label' }
99+
add_field(f, :data_field_183, :text, df183)
100+
101+
# --- function
102+
df201 = { function: 'test_fun', then: 'addClass red', target: '#post_data_field_201_input label' }
103+
add_field(f, :data_field_201, :string, df201)
104+
105+
df202 = { function: 'missing_fun', then: 'addClass red', target: '#post_data_field_202_input label' }
106+
add_field(f, :data_field_202, :string, df202)
107+
108+
df203 = { function: 'test_fun2' }
109+
add_field(f, :data_field_203, :boolean, df203)
110+
111+
# --- addClass
112+
df211 = { if: 'checked', then: 'addClass red', target: '#post_data_field_211_input label' }
113+
add_field(f, :data_field_211, :boolean, df211)
114+
115+
# --- callback
116+
df221 = { if: 'checked', then: 'callback test_callback', args: 'test_callback_arg' }
117+
add_field(f, :data_field_221, :boolean, df221)
118+
119+
df222 = { if: 'checked', then: 'callback missing_callback', args: 'callback arg' }
120+
add_field(f, :data_field_222, :boolean, df222)
121+
122+
# --- setValue
123+
df231 = { if: 'checked', then: 'setValue data test', target: '#post_data_test' }
124+
add_field(f, :data_field_231, :boolean, df231)
125+
126+
# --- hide
127+
df241 = { if: 'checked', then: 'hide', target: '#post_data_field_241_input .inline-hints' }
128+
add_field(f, :data_field_241, :boolean, df241)
129+
130+
# --- fade
131+
df251 = { if: 'checked', then: 'fade', target: '#post_data_field_251_input .inline-hints' }
132+
add_field(f, :data_field_251, :boolean, df251)
133+
134+
# --- slide
135+
df261 = { if: 'checked', then: 'slide', target: '#post_data_field_261_input .inline-hints' }
136+
add_field(f, :data_field_261, :boolean, df261)
137+
138+
# --- gtarget
139+
df301 = { if: 'checked', then: 'addClass red', gtarget: 'body.active_admin' }
140+
add_field(f, :data_field_301, :boolean, df301)
141+
142+
# This will not work - here only for testing:
143+
df302 = { if: 'checked', then: 'addClass red', target: 'body.active_admin' }
144+
add_field(f, :data_field_302, :boolean, df302)
145+
146+
# ---
49147
end
50148

51149
f.inputs 'Tags' do
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
//= require active_admin/base
22

33
//= require activeadmin/dynamic_fields
4+
5+
function test_fun(el) {
6+
return el.val() == 'test'
7+
}
8+
9+
function test_fun2(el) {
10+
el.toggleClass('red', !el.is(':checked'))
11+
}
12+
13+
function test_callback(args) {
14+
$('body').addClass(args)
15+
}

spec/dummy/app/models/post.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,34 @@ class Post < ApplicationRecord
1010
has_many :post_tags, inverse_of: :post, dependent: :destroy
1111
has_many :tags, through: :post_tags
1212

13+
serialize :description, JSON
14+
15+
after_initialize -> { self.description = {} if description.nil? }
16+
1317
validates :title, allow_blank: false, presence: true
1418

1519
scope :published, -> { where(published: true) }
1620
scope :recents, -> { where('created_at > ?', Date.today - 8.month) }
1721

22+
def method_missing(method_name, *arguments, &_block)
23+
method = method_name.to_s
24+
if method.start_with? 'data_'
25+
method.gsub! /\Adata_/, ''
26+
if method.end_with? '='
27+
self.description ||= {}
28+
description.send(:[]=, method.chop, arguments.any? ? arguments[0] : nil)
29+
else
30+
description&.send(:[], method)
31+
end
32+
else
33+
super
34+
end
35+
end
36+
37+
def respond_to_missing?(method_name, _include_private = false) # rubocop:disable Style/OptionalBooleanParameter
38+
method_name.to_s.start_with?('data_') ? true : super
39+
end
40+
1841
def short_title
1942
title.truncate 10
2043
end

spec/system/dynamic_fields_spec.rb

Lines changed: 136 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,150 @@
1414
end
1515

1616
context 'with some dynamic fields' do
17-
it 'toggles the .group1 elements when clicking on the checkbox' do
17+
it 'checks the conditions and actions' do
1818
visit "/admin/posts/#{post.id}/edit"
1919

20-
expect(page).to have_css('#post_published[data-if="not_checked"][data-then="hide"][data-target=".group1"]')
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
2121

22-
expect(find('#post_published')).not_to be_checked
23-
expect(page).to have_css('#post_dt_input', visible: :hidden)
24-
expect(page).to have_css('#post_position_input', visible: :hidden)
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')
2526

26-
find('#post_published').set(true)
27-
expect(page).to have_css('#post_dt_input', visible: :visible)
28-
expect(page).to have_css('#post_position_input', visible: :visible)
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')
2930

30-
find('#post_published').set(false)
31-
expect(page).to have_css('#post_dt_input', visible: :hidden)
32-
expect(page).to have_css('#post_position_input', visible: :hidden)
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')
35+
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')
40+
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')
45+
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')
49+
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')
54+
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+
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')
65+
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')
69+
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')
74+
75+
# --- not
76+
expect(page).to have_css('#post_data_field_181_input label.red')
77+
fill_in('post_data_field_181', with: '181')
78+
find('body').click
79+
expect(page).not_to have_css('#post_data_field_181_input label.red')
80+
81+
expect(page).to have_css('#post_data_field_182_input label.red')
82+
select('182', from: 'post_data_field_182')
83+
expect(page).not_to have_css('#post_data_field_182_input label.red')
84+
85+
expect(page).to have_css('#post_data_field_183_input label.red')
86+
fill_in('post_data_field_183', with: '183')
87+
find('body').click
88+
expect(page).not_to have_css('#post_data_field_183_input label.red')
89+
90+
# --- function
91+
expect(page).not_to have_css('#post_data_field_201_input label.red')
92+
fill_in('post_data_field_201', with: 'test')
93+
find('body').click
94+
expect(page).to have_css('#post_data_field_201_input label.red')
95+
96+
expect(page).to have_css('#post_data_field_202[data-df-errors="custom function not found"]')
97+
98+
expect(page).to have_css('#post_data_field_203.red')
99+
find('#post_data_field_203').click
100+
expect(page).not_to have_css('#post_data_field_203.red')
101+
102+
# --- addClass
103+
expect(page).not_to have_css('#post_data_field_211_input label.red')
104+
find('#post_data_field_211').click
105+
expect(page).to have_css('#post_data_field_211_input label.red')
106+
find('#post_data_field_211').click
107+
expect(page).not_to have_css('#post_data_field_211_input label.red')
108+
109+
# --- callback
110+
expect(page).not_to have_css('body.test_callback_arg')
111+
find('#post_data_field_221').click
112+
expect(page).to have_css('body.test_callback_arg')
113+
114+
find('#post_data_field_222').click
115+
expect(page).to have_css('#post_data_field_222[data-df-errors="callback function not found"]')
116+
117+
# --- setValue
118+
expect(find('#post_data_test').value).to be_empty
119+
find('#post_data_field_231').click
120+
expect(find('#post_data_test').value).to eq 'data test'
121+
122+
# --- hide
123+
expect(page).to have_css('#post_data_field_241_input .inline-hints', visible: :visible)
124+
find('#post_data_field_241').click
125+
expect(page).to have_css('#post_data_field_241_input .inline-hints', visible: :hidden)
126+
127+
# --- fade
128+
expect(page).to have_css('#post_data_field_251_input .inline-hints', visible: :visible)
129+
find('#post_data_field_251').click
130+
expect(page).to have_css('#post_data_field_251_input .inline-hints', visible: :hidden)
131+
132+
# --- slide
133+
expect(page).to have_css('#post_data_field_261_input .inline-hints', visible: :visible)
134+
find('#post_data_field_261').click
135+
expect(page).to have_css('#post_data_field_261_input .inline-hints', visible: :hidden)
136+
137+
# --- gtarget
138+
expect(page).not_to have_css('body.active_admin.red')
139+
find('#post_data_field_301').click
140+
expect(page).to have_css('body.active_admin.red')
141+
find('#post_data_field_301').click
142+
expect(page).not_to have_css('body.active_admin.red')
143+
144+
find('#post_data_field_302').click # checks that using simply "target" will not work
145+
expect(page).not_to have_css('body.active_admin.red')
33146
end
147+
end
34148

35-
it 'changes the value of target when the source element is blank' do
36-
visit "/admin/posts/#{post.id}/edit"
149+
context 'with some dynamic fields on a nested resource' do
150+
it 'checks the conditions and actions' do
151+
visit '/admin/authors/new'
37152

38-
expect(page).to have_css(
39-
'#post_description[data-if="blank"][data-then="setValue no title"][data-target="#post_category"]'
40-
)
41-
expect(find('#post_category').value).to eq 'no title'
42-
find('#post_category').set('...')
43-
find('#post_description').set('...')
44-
expect(find('#post_category').value).to eq '...'
45-
find('#post_description').set('')
46-
expect(find('#post_category').value).to eq 'no title'
153+
expect(page).not_to have_css('body.active_admin.red')
154+
find('body.active_admin .profile.has_many_container .button.has_many_add').click
155+
fill_in('author_profile_attributes_description', with: 'Some content')
156+
find('body').click
157+
expect(page).to have_css('body.active_admin.red')
158+
fill_in('author_profile_attributes_description', with: ' ')
159+
find('body').click
160+
expect(page).not_to have_css('body.active_admin.red')
47161
end
48162
end
49163
end

0 commit comments

Comments
 (0)