Created
January 13, 2016 13:46
-
-
Save alexfaber2011/389132ce23d9a523d528 to your computer and use it in GitHub Desktop.
Testing Re-Frame Handlers that Dispatch Other Handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; I have a question about how to test handlers that dispatch other handlers (say when fetching from a server). | |
; Here's what the wiki says about fetching from servers | |
(ns my.app.handlers | |
(:require [ajax.core :refer [GET POST]] | |
[re-frame.core :refer [register-handler])) | |
(register-handler | |
:request-it | |
(fn | |
[db _] | |
(ajax.core/GET | |
"http://json.my-endpoint.com/blah" | |
{:handler #(dispatch [:process-response %1]) | |
:error-handler #(dispatch [:bad-response %1])}) | |
(assoc db :loading? true))) | |
; This approach relies on dispatching another re-frame handler (either `:bad-response` or `:process-response`). | |
; To test the `:request-it` handler the testing section in the wiki tells us to yank out the anonymous function | |
; which exposes it for testing like so: | |
(ns my.app.handlers | |
(:require [ajax.core :refer [GET POST]] | |
[re-frame.core :refer [register-handler])) | |
(defn request-it | |
[db _] | |
(ajax.core/GET | |
"http://json.my-endpoint.com/blah" | |
{:handler #(dispatch [:process-response %1]) | |
:error-handler #(dispatch [:bad-response %1])}) | |
(assoc db :loading? true))) | |
(register-handler | |
:request-it | |
request-it) | |
; The problem is we're only able to test what `request-it` returns which is an updated version of the app db | |
; that has it's `:loading?` flag set to true. What's the best way to test what's happening downstream, say | |
; after `:process-response` or `:bad-response` is called? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Response by nberger on re-frame slack channel
cljs.test/async
andwith-redefs
to mock the get request (you don't want to hit the server, right?) but not mockdispatch
.dispatch
, you could still test everything in one test (by callingbad-response
orprocess-response
yourself, in your redef'eddispatch
)[8:34]
With the second approach you can get closer to "unit tests". In the first one you are heading to "integration tests". I usually prefer the first approach, so I'm also more confident that everything is orchestrated correctly, but I know others prefer the second one
[8:36]
Yesterday someone answered a similar question involving om components doing an async request during will-mount, it might help you as an example of using
cljs.test/async
andwith-redefs
: http://stackoverflow.com/questions/34116750/testing-components-with-async-api-calls-by-mocking-the-request