Skip to content

Commit ef1920a

Browse files
committed
Add test suite for field views.
1 parent 8460e2c commit ef1920a

File tree

2 files changed

+389
-1
lines changed

2 files changed

+389
-1
lines changed

tests/FieldsTest.elm

Lines changed: 388 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,388 @@
1+
module FieldsTest exposing (suite)
2+
3+
import Expect exposing (Expectation)
4+
import Form as F
5+
import Form.Validate
6+
import Html
7+
import Html.Attributes
8+
import Json.Encode
9+
import Json.Schema.Builder exposing (..)
10+
import Json.Schema.Definitions exposing (..)
11+
import Json.Schema.Form.Fields exposing (schemaView)
12+
import Json.Schema.Form.Value exposing (Value(..))
13+
import Test exposing (..)
14+
import Test.Html.Event as Event
15+
import Test.Html.Query as Query
16+
import Test.Html.Selector exposing (..)
17+
18+
19+
options =
20+
{ errors = \_ _ -> ""
21+
, formats = []
22+
}
23+
24+
25+
form =
26+
F.initial [] (Form.Validate.succeed EmptyValue)
27+
28+
29+
view : (Query.Single F.Msg -> Expectation) -> SchemaBuilder -> Expectation
30+
view func schema =
31+
case toSchema schema of
32+
Ok schema_ ->
33+
schemaView options [] schema_ form
34+
|> Query.fromHtml
35+
|> func
36+
37+
Err error ->
38+
Expect.fail error
39+
40+
41+
suite : Test
42+
suite =
43+
describe "Json.Schema.Form.Fields"
44+
[ describe "schemaView"
45+
[ describe "with a blank schema"
46+
[ test "should be a checkbox" <|
47+
\_ -> buildSchema |> isCheckbox
48+
]
49+
, describe "with a boolean schema"
50+
[ describe "true"
51+
[ test "should be div with text 'True'" <|
52+
\_ ->
53+
boolSchema True
54+
|> view
55+
(Query.has
56+
[ tag "div"
57+
, text "True"
58+
]
59+
)
60+
]
61+
, describe "false"
62+
[ test "should be div with text 'False'" <|
63+
\_ ->
64+
boolSchema False
65+
|> view
66+
(Query.has
67+
[ tag "div"
68+
, text "False"
69+
]
70+
)
71+
]
72+
]
73+
, describe "with an object schema"
74+
[ describe "any type"
75+
[ test "should be a checkbox" <|
76+
\_ -> buildSchema |> isCheckbox
77+
, describe "oneOf"
78+
[ test "should be a switch" <|
79+
\_ -> buildSchema |> isSwitch
80+
]
81+
, describe "anyOf"
82+
[ test "should be a switch" <|
83+
\_ -> buildSchema |> isSwitch
84+
]
85+
]
86+
, describe "nullable type" singleTypes
87+
, describe "union type"
88+
[ describe "with all types"
89+
[ test "should be a text field" <|
90+
\_ ->
91+
(buildSchema
92+
|> withUnionType
93+
[ "integer"
94+
, "number"
95+
, "string"
96+
, "boolean"
97+
, "array"
98+
, "object"
99+
]
100+
)
101+
|> isTextField
102+
]
103+
]
104+
, describe "single type" singleTypes
105+
]
106+
]
107+
]
108+
109+
110+
singleTypes =
111+
[ describe "integer"
112+
[ test "should be a text field" <|
113+
\_ ->
114+
buildSchema
115+
|> withType "integer"
116+
|> isTextField
117+
]
118+
, describe "number"
119+
[ test "should be a text field" <|
120+
\_ ->
121+
buildSchema
122+
|> withType "number"
123+
|> isTextField
124+
]
125+
, describe "string"
126+
[ test "should be a text field" <|
127+
\_ ->
128+
buildSchema
129+
|> withType "string"
130+
|> isTextField
131+
]
132+
, describe "boolean"
133+
[ test "should be a checkbox" <|
134+
\_ ->
135+
buildSchema
136+
|> withType "boolean"
137+
|> isCheckbox
138+
]
139+
, describe "array"
140+
[ describe "without item schema"
141+
[ test "should be a list" <|
142+
\_ ->
143+
buildSchema
144+
|> withType "array"
145+
|> isList
146+
]
147+
, describe "with item schema"
148+
[ test "should be a list" <|
149+
\_ ->
150+
buildSchema
151+
|> withType "array"
152+
|> withItem buildSchema
153+
|> isList
154+
]
155+
, describe "with multiple item schemas"
156+
[ test "should be a tuple" <|
157+
\_ ->
158+
buildSchema
159+
|> withType "array"
160+
|> withItems [ buildSchema, buildSchema ]
161+
|> isTuple
162+
]
163+
]
164+
, describe "object"
165+
[ test "should be a fieldset" <|
166+
\_ ->
167+
buildSchema
168+
|> withType "object"
169+
|> withProperties [ ( "test", buildSchema ) ]
170+
|> isFieldset
171+
]
172+
, describe "null"
173+
[ test "should be an empty div" <|
174+
\_ ->
175+
buildSchema
176+
|> withType "null"
177+
|> Expect.all
178+
[ view (Query.has [ tag "div" ])
179+
, view
180+
(Query.children []
181+
>> Query.count (Expect.equal 0)
182+
)
183+
]
184+
]
185+
]
186+
187+
188+
isField schema =
189+
schema
190+
|> view
191+
(Query.has
192+
[ tag "div"
193+
, classes [ "form-group" ]
194+
]
195+
)
196+
197+
198+
hasFieldTitle schema =
199+
schema
200+
|> withTitle "Test"
201+
|> view
202+
(Query.find [ tag "label" ]
203+
>> Query.find [ tag "span", class "label-text" ]
204+
>> Query.has [ text "Test" ]
205+
)
206+
207+
208+
hasFieldDescription schema =
209+
schema
210+
|> withDescription "Lorem ipsum."
211+
|> view
212+
(Query.find [ tag "div", class "form-text" ]
213+
>> Query.has [ text "Lorem ipsum." ]
214+
)
215+
216+
217+
isCheckbox schema =
218+
schema
219+
|> withTitle "Test"
220+
|> Expect.all
221+
[ isField
222+
, hasFieldDescription
223+
, view
224+
(Expect.all
225+
[ Query.has [ tag "div", class "form-check" ]
226+
, Query.find [ tag "label" ]
227+
>> Query.has [ class "form-check-label", text "Test" ]
228+
, Query.find [ tag "input" ]
229+
>> Query.has
230+
[ class "form-check-input"
231+
, attribute
232+
(Html.Attributes.attribute
233+
"type"
234+
"checkbox"
235+
)
236+
, checked False
237+
]
238+
]
239+
)
240+
]
241+
242+
243+
isTextField =
244+
Expect.all
245+
[ isField
246+
, hasFieldTitle
247+
, hasFieldDescription
248+
, view
249+
(Expect.all
250+
[ Query.find [ tag "input" ]
251+
>> Query.has
252+
[ attribute
253+
(Html.Attributes.attribute
254+
"type"
255+
"text"
256+
)
257+
]
258+
]
259+
)
260+
]
261+
262+
263+
isFieldset schema =
264+
schema
265+
|> withTitle "Test"
266+
|> withDescription "Lorem ipsum."
267+
|> view
268+
(Expect.all
269+
[ Query.has [ tag "fieldset" ]
270+
, Query.find [ tag "legend" ]
271+
>> Query.has [ text "Test" ]
272+
, Query.children [ tag "div", class "form-group" ]
273+
>> Query.first
274+
>> Query.find [ tag "p" ]
275+
>> Query.has [ text "Lorem ipsum." ]
276+
]
277+
)
278+
279+
280+
isList schema =
281+
schema
282+
|> withTitle "Test"
283+
|> withDescription "Lorem ipsum."
284+
|> view
285+
(Expect.all
286+
[ Query.has [ tag "div", class "form-group" ]
287+
, Query.find
288+
[ tag "button"
289+
, classes
290+
[ "btn"
291+
, "btn-secondary"
292+
, "btn-add"
293+
]
294+
]
295+
>> Query.has [ text "Test" ]
296+
, Query.find [ tag "button", class "btn-add" ]
297+
>> Event.simulate Event.click
298+
>> Event.expect (F.Append "")
299+
, Query.find [ tag "div", class "form-text" ]
300+
>> Query.has [ text "Lorem ipsum." ]
301+
, Query.children [ tag "ol", class "list-group" ]
302+
>> Query.count (Expect.equal 1)
303+
]
304+
)
305+
306+
307+
isTuple schema =
308+
schema
309+
|> Expect.all
310+
[ isField
311+
, hasFieldDescription
312+
, view
313+
(Expect.all
314+
[ Query.has [ tag "div", class "form-group" ]
315+
, Query.findAll [ tag "div", class "form-group" ]
316+
>> Query.count (Expect.atLeast 1)
317+
]
318+
)
319+
]
320+
321+
322+
isSwitch schema =
323+
schema
324+
|> withAnyOf
325+
[ buildSchema
326+
|> withTitle "One"
327+
|> withConst (Json.Encode.string "one")
328+
, buildSchema
329+
|> withTitle "Two"
330+
|> withConst (Json.Encode.string "two")
331+
]
332+
|> Expect.all
333+
[ isField
334+
, hasFieldDescription
335+
, view
336+
(Expect.all
337+
[ Query.has [ tag "div", class "form-group" ]
338+
, Query.find [ tag "div", class "form-group" ]
339+
>> Query.children [ tag "div", class "form-check" ]
340+
>> Query.each
341+
(Expect.all
342+
[ Query.find [ tag "input" ]
343+
>> Query.has
344+
[ class "form-check-input"
345+
, checked False
346+
]
347+
]
348+
)
349+
, Query.findAll [ class "form-check-label" ]
350+
>> Query.index 0
351+
>> Query.has [ text "One" ]
352+
, Query.findAll [ class "form-check-label" ]
353+
>> Query.index 1
354+
>> Query.has [ text "Two" ]
355+
]
356+
)
357+
]
358+
359+
360+
isSelect schema =
361+
schema
362+
|> withAnyOf
363+
[ buildSchema
364+
|> withTitle "One"
365+
|> withConst (Json.Encode.string "one")
366+
, buildSchema
367+
|> withTitle "Two"
368+
|> withConst (Json.Encode.string "two")
369+
]
370+
|> Expect.all
371+
[ isField
372+
, view
373+
(Expect.all
374+
[ Query.find
375+
[ tag "select"
376+
, classes [ "form-control", "custom-select" ]
377+
]
378+
>> Query.children [ tag "option" ]
379+
>> Query.count (Expect.equal 2)
380+
, Query.findAll [ tag "option" ]
381+
>> Query.index 0
382+
>> Query.has [ text "One" ]
383+
, Query.findAll [ tag "option" ]
384+
>> Query.index 1
385+
>> Query.has [ text "Two" ]
386+
]
387+
)
388+
]

tests/ValidationTest.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ suite =
138138
singleTypes =
139139
[ describe "integer"
140140
((buildSchema
141-
|> withUnionType [ "integer" ]
141+
|> withType "integer"
142142
|> toSchema
143143
)
144144
|> validateMultiple

0 commit comments

Comments
 (0)