Last active
September 8, 2015 04:15
-
-
Save mmarum-sugarcrm/11b4afd795e6f2d1b066 to your computer and use it in GitHub Desktop.
Jasmine tests for Case Count by Status dashlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Basic Jasmine tests for Case Count by Status dashlet | |
*/ | |
ddescribe("Case Count by Status", function () { | |
/* | |
* Some useful constants for our tests. | |
* We use them to keep track of the module, layout, and view we are testing | |
*/ | |
var moduleName = 'Cases'; | |
var viewName = 'case-count-by-status'; | |
var layoutName = 'record-list'; | |
/* | |
* Variables shared by all tests | |
*/ | |
var app; | |
var view; | |
var layout; | |
/** | |
* Called before each test below. We use this function to setup (or mock up) the necessary pieces | |
* in order to test our Sidecar controller properly. | |
* | |
* Typically, we need to define Sugar view metadata and ensure that our controller JS file has been loaded | |
* by the Sidecar framework. We utilize some SugarTest utility functions to accomplish this. | |
*/ | |
beforeEach(function() { | |
// Proxy for our typical Sidecar `app` object | |
app = SugarTest.app; | |
//Ensure test metadata is initialized | |
SugarTest.testMetadata.init(); | |
//Load custom Handlebars template (usually optional) | |
SugarTest.loadCustomHandlebarsTemplate(viewName, 'view', 'base'); | |
//Load custom component JS (required) | |
SugarTest.loadCustomComponent('base', 'view', viewName); | |
//Mock view metadata for our custom view (usually required) | |
SugarTest.testMetadata.addViewDefinition( | |
viewName, | |
{ | |
'panels': [ | |
{ | |
fields: [] | |
} | |
] | |
}, | |
moduleName | |
); | |
//Commit custom metadata into Sidecar | |
SugarTest.testMetadata.set(); | |
//Mock the Sidecar context object | |
var context = app.context.getContext(); | |
context.set({ | |
module: moduleName, | |
layout: layoutName | |
}); | |
context.prepare(); | |
//Create parent layout for our view using fake context | |
layout = app.view.createLayout({ | |
name: layoutName, | |
context: context | |
}); | |
//Create our custom View before each test | |
view = app.view.createView({ | |
name : viewName, | |
context : context, | |
module : moduleName, | |
layout: layout, | |
platform: 'base' | |
}); | |
}); | |
/** | |
* Perform cleanup after each test. | |
*/ | |
afterEach(function() { | |
//Delete test metadata | |
SugarTest.testMetadata.dispose(); | |
//Delete list of declared components | |
app.view.reset(); | |
//Dispose of our view | |
view.dispose(); | |
}); | |
/** | |
* Make sure that our view object exists | |
*/ | |
it('should exist.', function() { | |
expect(view).toBeTruthy(); | |
}); | |
/** | |
* Make sure that when our controller creates expected HTML when render is called | |
*/ | |
it('should render HTML', function(){ | |
view.render(); | |
expect(view.$el.html()).toContain("LBL_CASE_COUNT_BY_STATUS_TOTAL"); | |
}); | |
/** | |
* Tests for the _parseModels function. | |
*/ | |
describe('_parseModels', function(){ | |
it('should parse values based on available models', function(){ | |
// Mock out the list of models (this normally comes from API) | |
var models = [ | |
new Backbone.Model({id:'1', status:'Open'}), | |
new Backbone.Model({id:'2', status:'Closed'}), | |
new Backbone.Model({id:'3', status:'Open'}) | |
]; | |
// Call function with our mock list | |
view._parseModels(models, false); | |
//We had 2 open Cases in models array | |
expect(view.values['Open'].count).toBe(2); | |
//We had 1 closed Case in models array | |
expect(view.values['Closed'].count).toBe(1); | |
//Only 2 Statuses should exist in values array | |
expect(_.size(view.values)).toBe(2); | |
//Expect to find 3 Cases | |
expect(view.totalCases).toBe(3); | |
}); | |
it('should handle empty models array', function(){ | |
// No models | |
var models = []; | |
view._parseModels(models, false); | |
expect(_.size(view.values)).toBe(0); | |
expect(view.totalCases).toBe(0); | |
}); | |
it('should handle unexpected values gracefully (null)', function(){ | |
// null models array | |
var models = null; | |
view._parseModels(models, false); | |
expect(_.size(view.values)).toBe(0); | |
expect(view.totalCases).toBe(0); | |
}); | |
it('should handle unexpected values gracefully (object)', function(){ | |
// object instead of array | |
var models = {}; | |
view._parseModels(models, false); | |
expect(_.size(view.values)).toBe(0); | |
expect(view.totalCases).toBe(0); | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment