I am currently setting up tests that utilize OpenLayers and I would like to be able to mock requests to services like Bing Maps in Karma unit tests. I've tried to use Sinon to mock XHR requests but realized it is generating these requests by dynamically creating and injecting a script
tag into the DOM head
and binding the JSONP response.
I am using PhantomJS on TravisCI and Chrome locally to test and I am trying to work out how provide my own JSONP response in place of the request coming from injecting this tag into the head
element. This will be to reduce on API requests, speed up testing and avoid potential API/network issues occurring during unit tests.
Currently I've tried SinonJS using the following setup.
server = sinon.fakeServer.create();
server.respondWith('GET', bingRequestUrl,
[200, {"Content-Type": "application/x-javascript; charset=utf-8"},
bingFakeResponse
]);
$scope.$digest();
$timeout.flush();
server.restore();
The idea being that I am only mocking requests during the time they will be processed and then restoring normal functionality.
Is there are way to achieve what I need using Sinon or does it only mock XHR requests and not requests coming from DOM manipulation shown in this example.