|
13 | 13 | author.destroy |
14 | 14 | end |
15 | 15 |
|
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 |
19 | 27 |
|
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 |
21 | 31 |
|
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} ...") |
26 | 34 |
|
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] |
30 | 39 |
|
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 |
35 | 43 |
|
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} ...") |
40 | 46 |
|
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] |
45 | 51 |
|
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 |
49 | 55 |
|
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} ...") |
54 | 58 |
|
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] |
59 | 63 |
|
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 |
65 | 67 |
|
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" |
69 | 71 |
|
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 |
74 | 73 |
|
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) |
80 | 88 |
|
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']) |
84 | 95 |
|
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']) |
89 | 101 |
|
90 | 102 | # --- 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 ...']) |
95 | 105 |
|
96 | 106 | # --- 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']) |
101 | 109 |
|
102 | 110 | # --- 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']) |
108 | 113 | 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']) |
113 | 115 |
|
114 | 116 | # --- 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']) |
120 | 119 |
|
121 | 120 | # --- 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']) |
126 | 123 | find('#post_data_field_222').click |
127 | 124 | expect(page).to have_css('#post_data_field_222[data-df-errors="callback function not found"]') |
128 | 125 |
|
129 | 126 | # --- setValue |
| 127 | + spec_message('check data-then="setValue ..." action') |
130 | 128 | expect(find('#post_data_test').value).to be_empty |
131 | 129 | find('#post_data_field_231').click |
132 | 130 | expect(find('#post_data_test').value).to eq 'data test' |
133 | 131 |
|
134 | 132 | # --- 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']) |
138 | 136 |
|
139 | 137 | # --- 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']) |
143 | 141 |
|
144 | 142 | # --- 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']) |
148 | 146 |
|
149 | 147 | # --- setText |
| 148 | + spec_message('check data-then="setText ..." action') |
150 | 149 | expect(find('#post_data_field_271_input .inline-hints').text).not_to eq 'data test' |
151 | 150 | 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') |
153 | 152 |
|
154 | 153 | # --- 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']) |
160 | 158 |
|
161 | 159 | # --- 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']) |
168 | 162 | find('#post_data_field_302').click # checks that using simply "target" will not work |
169 | 163 | expect(page).not_to have_css('body.active_admin.red') |
170 | 164 |
|
171 | 165 | # --- else |
| 166 | + spec_message('check data-else="..."') |
172 | 167 | expect(page).not_to have_css('#post_data_field_321_input label.red') |
173 | 168 | expect(page).to have_css('#post_data_field_321_input label.green') |
174 | 169 | find('#post_data_field_321').click |
|
0 commit comments