This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpeakAndSpellTest.elm
310 lines (228 loc) · 7.71 KB
/
SpeakAndSpellTest.elm
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
306
307
308
309
310
module SpeakAndSpellTest exposing
( clickCommands
, clickLetters
, clickSoundControls
, getWordsFromAPI
, isInitializedOutputScreen
, isPresentAppColours
, isPresentAppName
, isPresentBrandLink
, isPresentBrandLogo
, isPresentBrandName
, isPresentCommands
, isPresentKeyboard
, isPresentLoading
, isPresentShellLogo
, isPresentSoundControls
)
import Expect
import Fuzz exposing (string)
import Html exposing (Html)
import Html.Attributes as Attr
import Json.Decode exposing (decodeValue)
import Json.Encode as Encode
import SpeakAndSpell
exposing
( Model
, Msg(..)
, Sound(..)
, elmLogoBlue
, elmLogoGray
, initialModel
, namePlusLogo
, namePlusSoundCtrl
, newWordDecoder
, outputScreen
, outputText
, theKeyboard
, viewLoading
)
import Test exposing (Test, describe, fuzz3, test)
import Test.Html.Event as Event
import Test.Html.Query as Query exposing (Single)
import Test.Html.Selector exposing (attribute, classes, tag, text)
-- CONSTANTS
speakAndSpell : List ( String, String )
speakAndSpell =
[ ( "Speak", "text-red-600" )
, ( "&", "text-white" )
, ( "Spell", "text-blue-600" )
]
loadingText : List String
loadingText =
[ "L", "O", "A", "D", "I", "N", "G" ]
alphabet : List String
alphabet =
-- A to Z in ASCII is 65 to 90
List.range 65 90
|> List.map (\ascii -> String.fromChar (Char.fromCode ascii))
keyboardCommands : List ( Msg, String )
keyboardCommands =
[ ( EraseLetter, "ERASE [↤]" )
, ( ResetWord, "RESET [5]" )
, ( Speak, "SPEAK [8]" )
, ( Spell, "SPELL [9]" )
, ( SubmitWord, "SUBMIT [↵]" )
, ( ResetWord, "RETRY [6]" )
, ( GetAnotherWord, "NEW [0]" )
]
soundCommands : List ( Msg, String )
soundCommands =
[ ( SetSound Off, "SOUND OFF [3]" )
, ( SetSound On, "SOUND ON [2]" )
]
-- HELPER FUNCTIONS
findAriaLabel : Html msg -> String -> String -> Single msg
findAriaLabel componentToTest ariaLabelCommon ariaLabelSpecific =
componentToTest
|> Query.fromHtml
|> Query.find
[ attribute
(Attr.attribute "aria-label"
(ariaLabelCommon ++ ariaLabelSpecific)
)
]
allThingsClicker : Html Msg -> Msg -> String -> String -> Test
allThingsClicker componentToTest message ariaToFind item =
test ("clicking " ++ item) <|
\_ ->
findAriaLabel componentToTest ariaToFind (" " ++ item)
|> Event.simulate Event.click
|> Event.expect message
allThingsChecker : Html msg -> String -> String -> String -> Test
allThingsChecker componentToTest ariaToFind tagToFind item =
test ("is present " ++ item) <|
\_ ->
findAriaLabel componentToTest ariaToFind (" " ++ item)
|> Query.has [ tag tagToFind, text item ]
outputScreenQueryHtml : Single Msg
outputScreenQueryHtml =
initialModel
|> outputScreen
|> Query.fromHtml
-- LOADING SCREEN TESTS
isPresentLoading : Test
isPresentLoading =
describe "all letters are present on loading screen" <|
List.map (\letter -> checkLoadingLetters letter) loadingText
checkLoadingLetters : String -> Test
checkLoadingLetters letter =
test ("loading letter present " ++ letter) <|
\_ ->
findAriaLabel viewLoading "Loading Animation" ""
|> Query.has [ tag "p", text letter ]
-- BRAND, APP NAME, AND LOGOS TESTS
isPresentBrandName : Test
isPresentBrandName =
test "brand name present" <|
\_ ->
outputScreenQueryHtml
|> Query.has [ tag "a", text "Elm Instruments" ]
isPresentBrandLink : Test
isPresentBrandLink =
test "brand link present" <|
\_ ->
outputScreenQueryHtml
|> Query.has [ tag "a", attribute (Attr.href "https://elm-lang.org/") ]
isPresentBrandLogo : Test
isPresentBrandLogo =
test "brand logo present" <|
\_ ->
outputScreenQueryHtml
|> Query.has [ tag "img", attribute (Attr.src elmLogoGray) ]
isPresentShellLogo : Test
isPresentShellLogo =
test "yellow shell logo present" <|
\_ ->
findAriaLabel namePlusLogo "Elm Logo" ""
|> Query.has [ tag "img", attribute (Attr.src elmLogoBlue) ]
isPresentAppName : Test
isPresentAppName =
describe "app name words are present on yellow shell" <|
List.map (\word -> checkAppNameWording (Tuple.first word)) speakAndSpell
checkAppNameWording : String -> Test
checkAppNameWording word =
test ("speak and spell word " ++ word) <|
\_ ->
findAriaLabel namePlusLogo "App Name" ""
|> Query.has [ tag "p", text word ]
isPresentAppColours : Test
isPresentAppColours =
describe "all app name words have the right colours" <|
List.map (\colour -> checkAppNameColours (Tuple.second colour)) speakAndSpell
checkAppNameColours : String -> Test
checkAppNameColours colour =
test ("speak and spell colour " ++ colour) <|
\_ ->
findAriaLabel namePlusLogo "App Name" ""
|> Query.has [ tag "p", classes [ colour ] ]
-- API TESTS
getWordsFromAPI : Test
getWordsFromAPI =
fuzz3 string string string "get new words from the words API" <|
\word definition pronunciation ->
[ ( "word", Encode.string word )
, ( "definition", Encode.string definition )
, ( "pronunciation", Encode.string pronunciation )
]
|> Encode.object
|> decodeValue newWordDecoder
|> Expect.ok
-- OUTPUT SCREEN TESTS
isInitializedOutputScreen : Model -> Test
isInitializedOutputScreen model =
test "render the output screen with the default message" <|
\_ ->
outputText model
|> Expect.equal "START TYPING TO MATCH THE WORD ABOVE"
-- CLICKING TESTS
clickLetters : Model -> Test
clickLetters model =
describe "click all letters keys on the onscreen keyboard" <|
List.map
(\letter ->
allThingsClicker (theKeyboard model) (KeyClicked letter) "Keyboard Key" letter
)
alphabet
clickCommands : Model -> Test
clickCommands model =
describe "click all onscreen keyboard commands" <|
List.map
(\cmd ->
allThingsClicker (theKeyboard model) (Tuple.first cmd) "Command" (Tuple.second cmd)
)
keyboardCommands
clickSoundControls : Model -> Test
clickSoundControls model =
describe "click all sound controls commands" <|
List.map
(\cmd ->
allThingsClicker (namePlusSoundCtrl model) (Tuple.first cmd) "Command" (Tuple.second cmd)
)
soundCommands
-- LETTERS TESTS
isPresentKeyboard : Model -> Test
isPresentKeyboard model =
describe "all letters are present on the onscreen keyboard" <|
List.map
(\letter ->
allThingsChecker (theKeyboard model) "Keyboard Key" "button" letter
)
alphabet
-- COMMANDS TESTS
isPresentCommands : Model -> Test
isPresentCommands model =
describe "all commands are present on the onscreen keyboard" <|
List.map
(\command ->
allThingsChecker (theKeyboard model) "Command" "button" (Tuple.second command)
)
keyboardCommands
isPresentSoundControls : Model -> Test
isPresentSoundControls model =
describe "all sound controls are present" <|
List.map
(\command ->
allThingsChecker (namePlusSoundCtrl model) "Command" "button" (Tuple.second command)
)
soundCommands