var aliasedClientStubs = {}; var clientStubs = {}; /** * This module stubs the soap module to allow for offline * testing and stubbed clients. All clients' methods are stubbed with sinon and have * additional functionality: * *
* myClientStub.someMethod.errorOnCall = createErroringStub(error); * * // elsewhere * * myClientStub.someMethod.errorOnCall(); ** * @param {?} object anything * @return {Function} */ function createErroringStub(err) { return function() { this.args.forEach(function(argSet) { setTimeout(argSet[1].bind(null, err)); }); this.yields(err); }; } /** * Returns a method that calls all callbacks given to the method it is attached * to with the given response. * *
* myClientStub.someMethod.respondWithError = createRespondingStub(errorResponse); * * // elsewhere * * myClientStub.someMethod.respondWithError(); ** * @param {?} object anything * @return {Function} */ function createRespondingStub(object, body) { return function() { this.args.forEach(function(argSet) { setTimeout(argSet[1].bind(null, null, object)); }); this.yields(null, object, body); }; } /** * Registers a stubbed client with soap-stub. urlToWsdl is the path you will use * in your app. * * @param {String} alias A simple name to refer to the clientStub as. * @param {String} urlToWsdl May be file system URL or http URL. * @param {Object} clientStub A client with stubbed methods. */ function registerClient(alias, urlToWsdl, clientStub) { aliasedClientStubs[alias] = clientStub; clientStubs[urlToWsdl] = clientStub; } /** * Resets state associated with clientStubs. */ function reset() { Object.values(clientStubs).forEach(resetStubbedMethods); this.errOnCreateClient = false; } /** * Returns a previously registered client stub. * * @param {String} aliasOrWsdlUrl * @return {Object} clientStub */ function getStub(aliasOrWsdlUrl) { return aliasedClientStubs[aliasOrWsdlUrl] || clientStubs[aliasOrWsdlUrl]; } function resetStubbedMethods(client) { Object.keys(client).forEach(function(method) { method = client[method]; if (typeof method === 'function' && typeof method.reset === 'function') { method.reset(); } }); }