-
Notifications
You must be signed in to change notification settings - Fork 74
Action Testing
Testing
hey testing friends, gather 'round the github campfire!
check this thang out: https://github.com/adobe-photoshop/spaces-design/blob/master/test/spec/actions/application-test.js
this is a sample test of a sample action
there are few pieces here working together
at a high level, here's what we want to do:
-
define a mock response to a call to
_spaces.ps.descriptor.get
-
set up the "test store" to listen for the events that we expect the sample action to emit; in this case
HOST_VERSION
-
bind a handler in the test store for that action which will verify that it happens like we expect; in this case, it's just verifying that the contents of the action payload makes sense
github: Pull request closed: #14 Minor initialization cleanup by volfied
(in other cases, it might check that a bunch of actions are fired in the appropriate order, or that some actions AREN'T fired, etc.)
- Fire the action that we want to test
that's the last step
that gets the ball rolling; the action will make some calls that are eventually handled by the mock _spaces object; the mock will give a response that we defined in step 1.; then the test store will handle the events that the action(s) emits and verify that they're correct
the most complicated part is setting up the spaces mock
but, it's not that bad I swear 😀
basically, what we want to do is define request-response pairs
which means IF the playground gets a request (from a play call or a get call) that looks a certain way THEN the callback (from the play or get call) will be applied with a given response
you express the first part using a "test" function
in the case of mocking out a get call, the test is a function that looks at an action reference and decides if it's associated response is the one that should be returned
in the example, we're mocking a get "application" call
so the referenceTest
checks whether the action reference passed in looks like one of those, and returns true if so
anywho, when referenceTest
returns true, then the mock will apply response
to the callback
the response is an object that has an err
and a result
property; those correspond to the two arguments for the get
callback
note that for the magic mockGet
and mockPlay
methods to appear, you have to call the spaces-mock-helper setup and teardown functions as shown here: https://github.com/adobe-photoshop/spaces-design/blob/master/test/spec/application-test.js#L36-L44
you can call this.mockGet
and this.mockPlay
repeatedly; it stores a list of request-response pairs
and it walks down the list looking for a request that passes a test
if get gets to the bottom of the list (meaning, your test made a call to _playground that is NOT mocked) it will throw an exception
this is to make it easier to mock out different calls
also for the magic method this.bindTestAction
to show up you need to call the fluxxor-test-helper setup method
and you need to call it in the funny way that I've done in that example module to make sure the receiver (this
) is correctly bound
also note that the response
is an object literal here, but if you want you can define it in terms of the request parameters
i.e., instead of a response you can have a makeResponse
function that, in the case of get
, takes the input-reference as a parameter, and in the case of play
takes the input command name and action descriptor as a parameter
this allows the response to be dictated by the request
for example, the play call could be "make me 5 jibbers", the test could be "does it look like a request for some number of jibbers?" and the response could be a function that looks at the action descriptor that references 5 jibbers and returns an object that has 5 fake jibbers in it