Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Controller Spec Example
describe('Dataset Show Controller', function () {
describe('fresh load', function () {
var noOp = function(){};
beforeEach(function() {
//get dataset from json
json = getJSONFixture('modules/datasets/show/inlined-dataset.json');
//setup the app options
var appOptions = {
baseUrl: 'http://localhost:3000',
environment: 'unit-test',
inlinedData:{
dataset: json
}
};
//create the app
composer = Composer.create(appOptions);
//composer fakes
spyOn(composer,'loaderStart').andCallFake(noOp);
spyOn(composer,'loaderDone').andCallFake(noOp);
spyOn(composer,'execute').andCallFake(noOp);
spyOn(composer,'getPreviousPage').andCallFake(function(){return '';});
//and a spy
spyOn(composer.slideRegion, 'attachView').andCallThrough();
//create the controller
ctl = new Composer.DatasetsModule.Show.Controller({datasetId: json.id});
//setup controller fakes
spyOn(ctl, '_execAddDataset').andCallFake( function(model, callback){
callback();
});
spyOn(ctl,'_execGetSampleFeature').andCallFake(function(model){
return {attributes:[]};
});
spyOn(ctl,'initMap').andCallFake(noOp);
spyOn(ctl,'_fetchModel').andCallFake(noOp);
//setup controller spies
spyOn(ctl,'setMapbarMessage').andCallThrough();
spyOn(ctl,'_showProgress').andCallThrough();
spyOn(ctl,'_clearProgress').andCallThrough();
//fire the method we are testing
ctl.initUi({datasetId:json.data.id});
});
afterEach(function() {
if(Backbone.history){
Backbone.history.stop();
}
});
it('should set the map bar message with the dataset name', function() {
expect(ctl.setMapbarMessage).toHaveBeenCalledWith(json.data.name);
});
it('should hydrate model from inlined data', function() {
expect(ctl.model).toBeDefined();
expect(ctl.model.get('description')).toEqual(json.data.description);
expect(ctl.model.get('fieldsLoaded')).toEqual(true);
expect(ctl.model.get('fields')).toEqual(jasmine.any(Backbone.Collection));
expect(ctl.model.get('fields').length).toEqual(json.data.fields.length);
expect(ctl.model.get('dataUrl')).toEqual(json.dataUrl);
});
it('should not call fetch on the model', function() {
expect(ctl._fetchModel).not.toHaveBeenCalled();
});
it('should call initMap', function() {
expect(ctl.initMap).toHaveBeenCalled();
});
it('should call clearProgress', function() {
expect(ctl._clearProgress).toHaveBeenCalled();
});
it('should attach the layout to the slideRegion', function() {
expect(composer.slideRegion.attachView).toHaveBeenCalledWith(jasmine.any(Marionette.Layout),'detail');
});
it('should setup the main layout', function() {
expect(ctl.layout).toBeDefined();
});
it('should setup the subnav view', function() {
expect(ctl.navView).toBeDefined();
});
it('should start and stop the loader', function() {
expect(composer.loaderStart).toHaveBeenCalled();
expect(composer.loaderDone).toHaveBeenCalled();
});
it('should create details controller', function() {
expect(ctl.detailsController).toBeDefined();
});
it('should add the dataset layer', function() {
expect(ctl._execAddDataset).toHaveBeenCalled();
});
it('should get sample feature', function() {
expect(ctl._execGetSampleFeature).toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.