/
MainTest.elm
469 lines (419 loc) · 18 KB
/
MainTest.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
module MainTest exposing (..)
import Database
import Expect exposing (Expectation)
import Json.Encode as Encode
import Main exposing (..)
import ProgramTest exposing (ProgramTest, SimulatedEffect, advanceTime, clickButton, clickLink, done, ensureBrowserUrl, expectView, expectViewHas, expectViewHasNot, fillIn, simulateDomEvent, within)
import Route
import SimulatedEffect.Cmd as SCmd
import SimulatedEffect.Navigation as Navigation
import SimulatedEffect.Process as SProcess
import SimulatedEffect.Task as STask
import Test exposing (..)
import Test.Html.Query as Query
import Test.Html.Selector as Selector exposing (Selector)
import Time
type alias NotesTest =
ProgramTest (Model ()) Msg Effect
start : NotesTest
start =
ProgramTest.createApplication
{ init = init
, onUrlChange = UrlChanged
, onUrlRequest = ClickedLink
, update = update
, view = view
}
|> ProgramTest.withBaseUrl "https://notes.bytes.zone/"
|> ProgramTest.withSimulatedEffects testPerform
|> ProgramTest.start
(Encode.object
[ ( "now", Encode.int 0 )
, ( "documents", Encode.list identity [] )
, ( "settings", Encode.null )
]
)
testPerform : Effect -> SimulatedEffect Msg
testPerform effect =
case effect of
NoEffect ->
SCmd.none
Batch effects ->
SCmd.batch (List.map testPerform effects)
LoadUrl url ->
SCmd.none
PushUrl url ->
Navigation.pushUrl (Route.toString url)
ReplaceUrl url ->
Navigation.replaceUrl (Route.toString url)
Put _ ->
-- port!
SCmd.none
FocusOnEditor ->
SCmd.none
SyncOnce _ ->
SCmd.none
Compact ->
SCmd.none
programTest : Test
programTest =
describe "notes"
[ test "it should be possible to add a note and see it in the sidebar after adding" <|
\_ ->
start
|> clickButton "New Note"
|> fillIn "editor" "Content" "What's up?"
|> expectSidebar (Query.find [ Selector.tag "li" ] >> Query.has [ Selector.text "What's up?" ])
, test "after editing, blurring finalizes the note" <|
\_ ->
start
|> addNote "What's up?"
|> blur
|> expectViewDoesntHaveInput
, test "after editing, hitting escape finalizes the note" <|
\_ ->
start
|> addNote "What's up?"
|> hitShortcutKey [] Escape
|> expectViewDoesntHaveInput
, test "if I make a syntax error, I should see it" <|
\_ ->
start
|> clickButton "New Note"
|> fillIn "editor" "Content" "test ["
|> expectNote (Query.find [ Selector.tag "li" ] >> Query.has [ Selector.text "While parsing a link, I was expecting the closing ']' of a [link](url)" ])
, test "after editing a note, hitting enter creates a new child note" <|
\_ ->
start
|> addNoteAndChildren "What's up?" [ "Not much, you?" ]
|> expectNote (Query.has [ Selector.text "Not much, you?" ])
, test "when I hit enter with my cursor in the middle of the text, it splits there" <|
\_ ->
start
|> addNoteAndChildren "Parent" [ "baseball" ]
|> clickButton "baseball"
|> moveSelectionToOffset 4 4
|> hitShortcutKey [] Enter
|> hitShortcutKey [] Escape
|> expectSiblingsIn
(Query.find [ Selector.tag "section" ]
>> Query.children [ Selector.tag "li" ]
)
[ Selector.text "base"
, Selector.text "ball"
]
, test "after adding two notes, you should be able to click to select either" <|
\_ ->
start
|> addNote "What's up?"
|> addNote "Not much."
|> clickButton "What's up?"
|> expectSidebar (Query.has [ Selector.text "What's up?" ])
, test "after a note has been edited, clicking it repoens it for editing" <|
\_ ->
start
|> addNoteAndChildren "Hey, I'm a Note!" []
|> within
(Query.find [ Selector.tag "section" ])
(clickButton "Hey, I'm a Note!")
|> expectAnEditingNode
, test "after a child has been edited, clicking it reopens it for editing" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "I'm a child!" ]
|> clickButton "I'm a child!"
|> expectAnEditingNode
, test "when adding a note, tab indents" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Parent", "Child" ]
|> clickButton "Child"
|> hitShortcutKey [] Tab
|> hitShortcutKey [] Escape
|> expectNote
(Query.find
[ Selector.tag "li"
, Selector.containing [ Selector.text "Parent" ]
]
>> Query.children [ Selector.tag "li" ]
>> Query.first
>> Query.has [ Selector.text "Child" ]
)
, test "when adding a note, shift-tab dedents" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Parent", "Child" ]
|> clickButton "Child"
|> hitShortcutKey [] Tab
|> hitShortcutKey [ Shift ] Tab
|> hitShortcutKey [] Escape
|> expectNote
(Query.find
[ Selector.tag "li"
, Selector.containing [ Selector.text "Parent" ]
]
>> Query.children [ Selector.tag "li" ]
>> Query.first
>> Query.hasNot [ Selector.text "Child" ]
)
, test "when a child is tabbed into a list with other children, it's inserted as the last child" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Child", "Grandchild 1", "Grandchild 2" ]
|> clickButton "Grandchild 1"
|> hitShortcutKey [] Tab
|> clickButton "Grandchild 2"
|> hitShortcutKey [] Tab
|> hitShortcutKey [] Escape
|> expectSiblingsIn
(Query.find
[ Selector.tag "li"
, Selector.containing [ Selector.text "Child" ]
]
>> Query.children [ Selector.tag "li" ]
)
[ Selector.text "Grandchild 1"
, Selector.text "Grandchild 2"
]
, test "hitting backspace in an empty node removes it from the note" <|
\_ ->
start
|> addNote "Note"
|> hitShortcutKey [] Enter
|> hitShortcutKey [] Backspace
|> hitShortcutKey [] Escape
|> expectNote
(Query.find [ Selector.containing [ Selector.text "Note" ] ]
>> Query.children [ Selector.tag "li" ]
>> Query.count (Expect.equal 0)
)
, test "hitting backspace at the beginning of a node joins it with the previous node" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "base", "ball" ]
|> clickButton "ball"
|> moveSelectionToOffset 0 0
|> hitShortcutKey [] Backspace
|> hitShortcutKey [] Escape
|> expectNote
(Query.find [ Selector.tag "li" ]
>> Query.has [ Selector.text "baseball" ]
)
, test "hitting alt-up while editing moves the node up" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "First", "Second" ]
|> clickButton "Second"
|> hitShortcutKey [ Alt ] ArrowUp
|> hitShortcutKey [] Escape
|> expectSiblingsIn
(Query.find [ Selector.tag "section" ] >> Query.children [ Selector.tag "li" ])
[ Selector.text "Second"
, Selector.text "First"
]
, test "hitting alt-down while editing moves the node down" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "First", "Second" ]
|> clickButton "First"
|> hitShortcutKey [ Alt ] ArrowDown
|> hitShortcutKey [] Escape
|> expectSiblingsIn
(Query.find [ Selector.tag "section" ] >> Query.children [ Selector.tag "li" ])
[ Selector.text "Second"
, Selector.text "First"
]
, test "hitting alt-up when the child is the first child moves it above the parent" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Parent", "Child" ]
|> clickButton "Child"
|> hitShortcutKey [] Tab
|> hitShortcutKey [ Alt ] ArrowUp
|> hitShortcutKey [] Escape
|> expectSiblingsIn
(Query.find [ Selector.tag "section" ] >> Query.children [ Selector.tag "li" ])
[ Selector.text "Child"
, Selector.text "Parent"
]
, test "hitting alt-down when the child is the last child moves it below the parent's next sibling" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Parent", "Child", "Aunt" ]
|> clickButton "Child"
|> hitShortcutKey [] Tab
|> hitShortcutKey [ Alt ] ArrowDown
|> hitShortcutKey [] Escape
|> expectSiblingsIn
(Query.find [ Selector.tag "section" ] >> Query.children [ Selector.tag "li" ])
[ Selector.text "Parent"
, Selector.text "Aunt"
, Selector.text "Child"
]
, test "clicking a note link creates a node with that name if it didn't exist before" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Go to [[another note]]" ]
|> clickLink "another note" "/node/another%20note"
|> expectNote (Query.has [ Selector.text "another note" ])
, test "linking to a note and then changing the title should change the link" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "Go to [[another note]]" ]
|> clickLink "another note" "/node/another%20note"
|> edit "another note"
|> fillIn "editor" "Content" "an awesome note"
|> hitShortcutKey [] Escape
|> clickSidebarLink "Note"
|> expectNote (Query.has [ Selector.text "an awesome note" ])
, test "you can see the incoming links to a note from the note's page" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "also discussed in [[another note]]" ]
|> addNoteAndChildren "another note" [ "note body" ]
|> expectNote
(Query.find [ Selector.tag "aside" ]
>> Query.has [ Selector.text "also discussed in" ]
)
, test "you don't see incoming links for a node with a note's title" <|
\_ ->
start
|> addNoteAndChildren "Note" [ "also discussed in [[notes]]" ]
|> addNoteAndChildren "another note" [ "notes" ]
|> expectNote
(Query.find [ Selector.tag "aside" ]
>> Query.hasNot [ Selector.text "also discussed in" ]
)
, test "you can click the status at the bottom to go to the sync settings screen" <|
\_ ->
start
|> clickLink "Sync" "/settings/sync"
|> ensureBrowserUrl (Expect.equal "https://notes.bytes.zone/settings/sync")
|> clickButton "Sync with a new CouchDB Server"
|> fillIn "" "Host" "https://couch.bytes.zone"
|> fillIn "" "Database" "notes"
|> fillIn "" "Username" "brian"
|> fillIn "" "Password" "password"
|> clickButton "Start Syncing"
|> Expect.all
[ expectView
(Query.find [ Selector.tag "section" ]
>> Query.has [ Selector.text "https://couch.bytes.zone" ]
)
, expectView
(Query.find [ Selector.tag "section" ]
>> Query.has [ Selector.text "notes" ]
)
, expectView
(Query.find [ Selector.tag "section" ]
>> Query.has [ Selector.text "brian" ]
)
, expectView
(Query.find [ Selector.tag "section" ]
>> Query.hasNot [ Selector.text "password" ]
)
]
]
addNote : String -> NotesTest -> NotesTest
addNote text =
clickButton "New Note"
>> fillIn "editor" "Content" text
>> moveSelectionToOffset (String.length text) (String.length text)
addNoteAndChildren : String -> List String -> NotesTest -> NotesTest
addNoteAndChildren note siblings test =
let
withNote =
test
|> addNote note
|> hitShortcutKey [] Enter
addSiblings =
siblings
|> List.map
(\content ->
[ fillIn "editor" "Content" content
, moveSelectionToOffset (String.length content) (String.length content)
]
)
|> List.intersperse [ hitShortcutKey [] Enter ]
|> List.concat
in
List.foldl (\action progress -> action progress) withNote addSiblings
|> hitShortcutKey [] Escape
expectSiblingsIn : (Query.Single Msg -> Query.Multiple Msg) -> List Selector -> NotesTest -> Expectation
expectSiblingsIn parent assertions =
assertions
|> List.indexedMap
(\i selector ->
expectView
(parent
>> Query.index i
>> Query.has [ selector ]
)
)
|> Expect.all
blur : NotesTest -> NotesTest
blur =
simulateDomEvent (Query.find [ input ]) ( "blur", Encode.object [] )
edit : String -> NotesTest -> NotesTest
edit name =
simulateDomEvent
(Query.find [ Selector.tag "section" ]
>> Query.find
[ Selector.tag "button"
, Selector.containing [ Selector.text name ]
]
)
( "click", Encode.object [] )
clickSidebarLink : String -> NotesTest -> NotesTest
clickSidebarLink name =
simulateDomEvent
(Query.find [ Selector.tag "nav" ]
>> Query.find
[ Selector.tag "button"
, Selector.containing [ Selector.text name ]
]
)
( "click", Encode.object [] )
type Key
= Enter
| Escape
| Tab
| Backspace
| ArrowUp
| ArrowDown
type Modifier
= Shift
| Alt
hitShortcutKey : List Modifier -> Key -> NotesTest -> NotesTest
hitShortcutKey modifiers key =
simulateDomEvent (Query.find [ Selector.id "editor" ]) (keyDown modifiers key)
keyDown : List Modifier -> Key -> ( String, Encode.Value )
keyDown modifiers key =
( "keydown"
, Encode.object
[ ( "key", Encode.string (Debug.toString key) )
, ( "shiftKey", Encode.bool (List.member Shift modifiers) )
, ( "altKey", Encode.bool (List.member Alt modifiers) )
]
)
expectSidebar : (Query.Single Msg -> Expectation) -> NotesTest -> Expectation
expectSidebar expect notesTest =
expectView (Query.find [ Selector.tag "nav" ] >> expect) notesTest
expectNote : (Query.Single Msg -> Expectation) -> NotesTest -> Expectation
expectNote expect notesTest =
expectView (Query.find [ Selector.tag "section" ] >> expect) notesTest
expectAnEditingNode : NotesTest -> Expectation
expectAnEditingNode =
expectView (Query.has [ input ])
expectViewDoesntHaveInput : NotesTest -> Expectation
expectViewDoesntHaveInput =
expectView (Query.hasNot [ input ])
input : Selector
input =
Selector.all
[ Selector.tag "textarea"
, Selector.id "editor"
]
moveSelectionToOffset : Int -> Int -> NotesTest -> NotesTest
moveSelectionToOffset start_ end_ =
ProgramTest.update (UserChangedSelection { start = start_, end = end_ })