-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRandomGifTests.elm
65 lines (58 loc) · 2.48 KB
/
RandomGifTests.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
module RandomGifTests exposing (..)
import Expect
import RandomGif
import Test exposing (..)
import Testable.TestContext exposing (..)
import Testable.Http as Http
catsComponent =
{ init = RandomGif.init "__API_KEY__" "cats"
, update = RandomGif.update
}
assertShownImage expectedImageUrl testContext =
testContext
|> currentModel
|> Result.map .gifUrl
|> Expect.equal (Ok expectedImageUrl)
all : Test
all =
describe "RandomGif"
[ test "sets initial topic" <|
\() ->
catsComponent
|> startForTest
|> currentModel
|> Result.map .topic
|> Expect.equal (Ok "cats")
, test "sets initial loading image" <|
\() ->
catsComponent
|> startForTest
|> assertShownImage "/favicon.ico"
, test "makes initial API request" <|
\() ->
catsComponent
|> startForTest
|> assertHttpRequest (Http.getRequest "https://api.giphy.com/v1/gifs/random?api_key=__API_KEY__&tag=cats")
, test "shows the new image on API success" <|
\() ->
catsComponent
|> startForTest
|> resolveHttpRequest (Http.getRequest "https://api.giphy.com/v1/gifs/random?api_key=__API_KEY__&tag=cats")
(Http.ok """{"data":{"image_url":"http://giphy.com/cat2000.gif"}}""")
|> assertShownImage "http://giphy.com/cat2000.gif"
, test "shows the loading image on API failure" <|
\() ->
catsComponent
|> startForTest
|> resolveHttpRequest (Http.getRequest "https://api.giphy.com/v1/gifs/random?api_key=__API_KEY__&tag=cats")
(Http.serverError)
|> assertShownImage "/favicon.ico"
, test "pressing the button makes a new API request" <|
\() ->
catsComponent
|> startForTest
|> resolveHttpRequest (Http.getRequest "https://api.giphy.com/v1/gifs/random?api_key=__API_KEY__&tag=cats")
(Http.ok """{"data":{"image_url":"http://giphy.com/cat2000.gif"}}""")
|> update RandomGif.RequestMore
|> assertHttpRequest (Http.getRequest "https://api.giphy.com/v1/gifs/random?api_key=__API_KEY__&tag=cats")
]