FeedHenry Mocks
A library that enables you to mock out APIs from the FeedHenry JS SDK. This enables unit testing of application logic without the need to actually run the SDK code which may be impractical or impossible for testing purposes.
Usage
To use this mocking library simply include it in a script tag in a custom test HTML file or inject it using Karma (which is a much better idea). Alternatively you can run your tests in a Node.js environment since this module will create a $fh global on either window or global depending on where you're running it, a browser or Node.js respectively. See here for an example of Node.js tests for a library that is actually meant for a browser!
This should be included instead of or after the standard feedhenry.js SDK script when testing.
What's nice about this is that you can write all tests in a blocking manner which keeps things pretty simple. You can write them using non-blocking syntax via the usual Jasmine/Mocha done callback (these are the only two I'm familiar with) if required too, just call the mock flush method when appropriate.
Example
'use strict'; var assert = ;// var fh = require('fh-js-mocks') <-- Possible with browserify var USER = 'johndoe' PASS = 'ismaithliomcácamilis'; ;
API
This library mimics the normal FeedHenry JavaScript SDK. As you might be aware the majority of API functions work like so:
$fh;
This library allows you to override existing implementations, and will create one if none exists. For example this will replace/create the $fh.act API:
$fh.createShim('act');
MockApiInstance
Created using $fh.createApiShim(name). Works just like a regular $fh API. Well, almost like a regular API. It's still a same old function definition you know and love: $fh.someApi(opts, success, fail). What's different is that it has some new functions appended as described here, and demonstrated in the examples.
MockApiInstance.expect(input)
The expect function tells our mock API service to expect a specific input and returns a MockApiResponder that allows us to configure a response scenario for the given input as detailed below.
MockApiInstance.flush([count])
Respond to count calls that have been queued to the API mock. Calls are only responed to if the same parameters it provided were given via an expect. The parameter comparison is done by using JSON.stringify on your inputs passed to $fh.shim.expect(input) and $fh.shim(inputs, success, fail).
You'll probably not need to supply the optional count argument often, perhaps even never.
MockApiInstance.verifyNoOutstandingRequest()
Verify that all calls to your shim were satisfied. Can be called in an afterEach hook to ensure you're cleanly responding to each call.
MockApiInstance.verifyNoOutstandingExpectation()
Verify that all expectations had a request made for them. Can be called in an afterEach hook to ensure you're not defining unnecessary expectations and that those you do declare are utilised.
MockApiResponder
An instance of this is returned from $fh.shim.expect. This class should be used to configure responses to your SDK inputs. The only function you should need to use is setResponse unless certain edge cases arise that require otherwise.
setResponse(err, res)
Configure the response that should be returned when inputs to expect match the input you provide to $fh.shim. Providing a non null value as the first parameter will cause the error callback you provided to the SDK to be called upon flushing the api call queue using flush. See the example below:
;