Skip to content

Instantly share code, notes, and snippets.

@odoe
Created December 9, 2013 15:22
Show Gist options
  • Save odoe/7873828 to your computer and use it in GitHub Desktop.
Save odoe/7873828 to your computer and use it in GitHub Desktop.
Sample of using Sinon to stub modules in Dojo AMD.
define([
'esri/tasks/FindTask',
'dojo/promise/Promise',
'widgets/search/searchservice'
], function(
FindTask,
Promise,
Widget) {
return describe('widgets/search/searchservice', function() {
var service,
stubFind,
options = {
settings: {
params: {},
findUrl: 'testpath'
}
};
beforeEach(function() {
service = new Widget(options);
stubFind = sinon.stub(FindTask.prototype, 'execute');
stubFind.returns({
then: function() {
return 999;
}
});
});
afterEach(function() {
stubFind.restore();
});
it(
'is a valid object',
function() {
expect(service).to.be.ok();
expect(service.options).to.be.ok();
expect(service.options.settings).to.eql(options.settings);
}
);
describe('#find', function() {
it(
'has its own service',
function() {
expect(service._task).to.be.ok();
}
);
it(
'returns a dojo/promise/Promise',
function() {
expect(service.find('999')).to.be.a(Promise);
}
);
it(
'calls a find task with its FindParameters',
function() {
service.find('999');
expect(stubFind.called).to.be.ok();
expect(stubFind.calledWith(service._findParams)).to.be.ok();
}
);
it(
'searches for given text',
function() {
var searchText = 'test string';
service.find(searchText);
expect(service._findParams.searchText).to.eql(searchText);
}
);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment