-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquestion_page_spec.rb
More file actions
305 lines (239 loc) · 9.04 KB
/
Copy pathquestion_page_spec.rb
File metadata and controls
305 lines (239 loc) · 9.04 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Strata::Flows::QuestionPage do
before do
test_model_class = Class.new do
include ActiveModel::Model
include ActiveModel::Attributes
attribute :first_name, :string
validates :first_name, presence: true, on: :first_name
end
stub_const("TestModel", test_model_class)
end
let(:record) { TestModel.new }
describe "with only required attributes" do
let(:page) { described_class.new("first_name") }
it "uses the provided name as the set of fields" do
expect(page.name).to eq("first_name")
expect(page.fields).to eq([ :first_name ])
end
it "is always needed" do
expect(page).to be_needed(record)
end
it "is completed based on the page name" do
expect(page).not_to be_completed(record)
record.first_name = "Mary"
expect(page).to be_completed(record)
end
it "is started when the field has a value" do
expect(page).not_to be_started(record)
record.first_name = "Mary"
expect(page).to be_started(record)
end
it "returns the correct pathnames" do
expect(page.edit_pathname).to eq("edit_first_name")
expect(page.update_pathname).to eq("update_first_name")
end
end
describe "with conditional if" do
let(:page) { described_class.new("first_name", if: ->(record) { record.first_name.nil? }) }
it "is needed if conditional is true" do
expect(page).to be_needed(record)
end
it "skips the page if conditional is false" do
record.first_name = "Minnie"
expect(page).not_to be_needed(record)
end
end
describe "with explicit fields" do
let(:page) { described_class.new("name", fields: [ :first_name, :last_name ]) }
it "uses the passed in fields" do
expect(page.fields).to eq([ :first_name, :last_name ])
end
end
describe "#started? with a boolean field" do
before do
bool_class = Class.new do
include ActiveModel::Model
include ActiveModel::Attributes
attribute :consents, :boolean
end
stub_const("BooleanModel", bool_class)
end
let(:record) { BooleanModel.new }
let(:page) { described_class.new("consents") }
it "is not started when the field is nil" do
expect(page).not_to be_started(record)
end
it "is started when the field is true" do
record.consents = true
expect(page).to be_started(record)
end
it "is started when the field is explicitly false" do
record.consents = false
expect(page).to be_started(record)
end
context "when mixed with a string field that is blank" do
before do
mixed_class = Class.new do
include ActiveModel::Model
include ActiveModel::Attributes
attribute :consents, :boolean
attribute :note, :string
end
stub_const("MixedBooleanModel", mixed_class)
end
let(:record) { MixedBooleanModel.new }
let(:page) { described_class.new("preferences", fields: [ :note, :consents ]) }
it "is started when only the boolean field is explicitly false" do
record.consents = false
expect(page).to be_started(record)
end
end
end
describe "#started? when a field is not defined on the record" do
let(:page) { described_class.new("ghost", fields: [ :nonexistent_field ]) }
it "does not raise" do
expect { page.started?(record) }.not_to raise_error
end
it "is not started" do
expect(page).not_to be_started(record)
end
context "when mixed with a defined field that has a value" do
let(:page) { described_class.new("mixed", fields: [ :nonexistent_field, :first_name ]) }
it "is started" do
record.first_name = "Mary"
expect(page).to be_started(record)
end
end
end
describe "#started? with a multi-parameter field" do
before do
test_model_class = Class.new(ActiveRecord::Base) do
self.table_name = "test_records"
include Strata::Attributes
strata_attribute :date_of_birth, :memorable_date
end
stub_const("DobModel", test_model_class)
end
let(:record) { DobModel.new }
let(:page) { described_class.new("dob", fields: [ date_of_birth: [ :month, :day, :year ] ]) }
it "is not started when the field has no value" do
expect(page).not_to be_started(record)
end
it "is started when the field has a value" do
record.date_of_birth = Date.new(1990, 5, 15)
expect(page).to be_started(record)
end
end
describe "#started? with a nested-attributes field" do
before do
child_class = Class.new(ActiveRecord::Base) do
self.table_name = "test_application_forms"
end
stub_const("NestedAttrsForm", child_class)
parent_class = Class.new(ActiveRecord::Base) do
self.table_name = "users"
has_many :forms, class_name: "NestedAttrsForm", foreign_key: :user_id
accepts_nested_attributes_for :forms
end
stub_const("NestedAttrsUser", parent_class)
end
let(:record) { NestedAttrsUser.new(first_name: "Alice", last_name: "Smith") }
context "with the hash form" do
let(:page) { described_class.new("forms", fields: [ forms_attributes: [ :test_string ] ]) }
it "is not started when no nested records have been assigned" do
expect(page).not_to be_started(record)
end
it "is started after nested attributes are assigned" do
record.forms_attributes = [ { test_string: "hello" } ]
expect(page).to be_started(record)
end
end
context "with the symbol form" do
let(:page) { described_class.new("forms", fields: [ :forms_attributes ]) }
it "is started after nested attributes are assigned" do
record.forms_attributes = [ { test_string: "hello" } ]
expect(page).to be_started(record)
end
end
end
describe "#started? with multiple fields" do
before do
multi_field_class = Class.new do
include ActiveModel::Model
include ActiveModel::Attributes
attribute :first_name, :string
attribute :last_name, :string
end
stub_const("MultiFieldModel", multi_field_class)
end
let(:record) { MultiFieldModel.new }
let(:page) { described_class.new("name", fields: [ :first_name, :last_name ]) }
it "is not started when no fields have values" do
expect(page).not_to be_started(record)
end
it "is started when any field has a value" do
record.last_name = "Smith"
expect(page).to be_started(record)
end
end
describe "#attributes" do
before do
test_model_class = Class.new(ActiveRecord::Base) do
self.table_name = "test_records"
include Strata::Attributes
strata_attribute :applicant_name, :name
strata_attribute :home_address, :address
strata_attribute :date_of_birth, :memorable_date
strata_attribute :weekly_wage, :money
strata_attribute :tax_id, :tax_id
strata_attribute :adopted_on, :us_date
strata_attribute :period, :us_date, range: true
end
stub_const("StrataTestModel", test_model_class)
end
let(:record) { StrataTestModel.new }
it "expands name fields into component columns" do
page = described_class.new("name", fields: [ :applicant_name ])
expect(page.attributes(record.class)).to eq([
:applicant_name_first, :applicant_name_middle, :applicant_name_last, :applicant_name_suffix
])
end
it "expands address fields into component columns" do
page = described_class.new("address", fields: [ :home_address ])
expect(page.attributes(record.class)).to eq([
:home_address_street_line_1, :home_address_street_line_2,
:home_address_city, :home_address_state, :home_address_zip_code
])
end
it "expands memorable_date fields into a multi-parameter hash" do
page = described_class.new("dob", fields: [ :date_of_birth ])
expect(page.attributes(record.class)).to eq([
{ date_of_birth: [ :month, :day, :year ] }
])
end
it "expands range fields into start and end columns" do
page = described_class.new("period", fields: [ :period ])
expect(page.attributes(record.class)).to eq([
:period_start, :period_end
])
end
it "passes through single-column strata fields unchanged" do
page = described_class.new("wage", fields: [ :weekly_wage ])
expect(page.attributes(record.class)).to eq([ :weekly_wage ])
end
it "passes through non-strata fields unchanged" do
page = described_class.new("misc", fields: [ :some_plain_field ])
expect(page.attributes(record.class)).to eq([ :some_plain_field ])
end
it "handles a mix of strata and non-strata fields" do
page = described_class.new("mixed", fields: [ :applicant_name, :tax_id, :date_of_birth ])
expect(page.attributes(record.class)).to eq([
:applicant_name_first, :applicant_name_middle, :applicant_name_last, :applicant_name_suffix,
:tax_id,
{ date_of_birth: [ :month, :day, :year ] }
])
end
end
end