Skip to content

Instantly share code, notes, and snippets.

@cnstaging
Created July 2, 2012 16:00
Show Gist options
  • Save cnstaging/3033957 to your computer and use it in GitHub Desktop.
Save cnstaging/3033957 to your computer and use it in GitHub Desktop.
Jasmine test suite for Deft.mixin.Controllable
// Generated by CoffeeScript 1.3.3
/*
Copyright (c) 2012 [DeftJS Framework Contributors](http://deftjs.org)
Open source under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
*/
/*
Jasmine test suite for Deft.mixin.Controllable
*/
describe('Deft.mixin.Controllable', function() {
it('should create an instance of the view controller specified by the target view `controller` property and configure it with a reference to the target view instance when an instance of the target view is created', function() {
var constructorSpy, exampleViewControllerInstance, exampleViewInstance;
exampleViewInstance = null;
exampleViewControllerInstance = null;
Ext.define('ExampleViewController', {
extend: 'Deft.mvc.ViewController'
});
Ext.define('ExampleView', {
extend: 'Ext.Container',
mixins: ['Deft.mixin.Controllable'],
controller: 'ExampleViewController',
constructor : function (config) {
var me = this;
me.callParent(arguments);
}
});
constructorSpy = spyOn(ExampleViewController.prototype, 'constructor').andCallFake(function() {
exampleViewControllerInstance = this;
return constructorSpy.originalValue.apply(this, arguments);
});
exampleViewInstance = Ext.create('ExampleView');
expect(ExampleViewController.prototype.constructor).toHaveBeenCalled();
expect(ExampleViewController.prototype.constructor.callCount).toBe(1);
expect(exampleViewControllerInstance.getView()).toBe(exampleViewInstance);
});
it('should pass the configuration object defined in the target view\'s `controllerConfig` config to the view controller\'s constructor', function() {
var constructorSpy, exampleViewControllerInstance, exampleViewInstance;
exampleViewInstance = null;
exampleViewControllerInstance = null;
Ext.define('ExampleViewController', {
extend: 'Deft.mvc.ViewController',
config: {
value: null
}
});
Ext.define('ExampleView', {
extend: 'Ext.Container',
alias: 'widget.ExampleView',
mixins: ['Deft.mixin.Controllable'],
controller: 'ExampleViewController',
controllerConfig: {
value: 'expected value'
}
});
constructorSpy = spyOn(ExampleViewController.prototype, 'constructor').andCallFake(function() {
exampleViewControllerInstance = this;
return constructorSpy.originalValue.apply(this, arguments);
});
exampleViewInstance = Ext.create('ExampleView');
expect(ExampleViewController.prototype.constructor).toHaveBeenCalledWith({
view: exampleViewInstance,
value: 'expected value'
});
expect(ExampleViewController.prototype.constructor.callCount).toBe(1);
expect(exampleViewControllerInstance.getView()).toBe(exampleViewInstance);
expect(exampleViewControllerInstance.getValue()).toBe('expected value');
});
it('should pass the configuration object passed to the target view\'s `controllerConfig` config to the view controller\'s constructor', function() {
var constructorSpy, exampleViewControllerInstance, exampleViewInstance;
exampleViewInstance = null;
exampleViewControllerInstance = null;
Ext.define('ExampleViewController', {
extend: 'Deft.mvc.ViewController',
config: {
value: null
}
});
Ext.define('ExampleView', {
extend: 'Ext.Container',
alias: 'widget.ExampleView',
mixins: ['Deft.mixin.Controllable'],
controller: 'ExampleViewController'
});
constructorSpy = spyOn(ExampleViewController.prototype, 'constructor').andCallFake(function() {
exampleViewControllerInstance = this;
return constructorSpy.originalValue.apply(this, arguments);
});
exampleViewInstance = Ext.create('ExampleView', {
controllerConfig: {
value: 'expected value'
}
});
expect(ExampleViewController.prototype.constructor).toHaveBeenCalledWith({
view: exampleViewInstance,
value: 'expected value'
});
expect(ExampleViewController.prototype.constructor.callCount).toBe(1);
expect(exampleViewControllerInstance.getView()).toBe(exampleViewInstance);
expect(exampleViewControllerInstance.getValue()).toBe('expected value');
});
it('should automatically add a getController() accessor method to the target view that returns the associated the view controller instance', function() {
var constructorSpy, exampleViewControllerInstance, exampleViewInstance;
exampleViewInstance = null;
exampleViewControllerInstance = null;
Ext.define('ExampleViewController', {
extend: 'Deft.mvc.ViewController'
});
Ext.define('ExampleView', {
extend: 'Ext.Container',
mixins: ['Deft.mixin.Controllable'],
controller: 'ExampleViewController'
});
constructorSpy = spyOn(ExampleViewController.prototype, 'constructor').andCallFake(function() {
exampleViewControllerInstance = this;
return constructorSpy.originalValue.apply(this, arguments);
});
exampleViewInstance = Ext.create('ExampleView');
return expect(exampleViewInstance.getController()).toBe(exampleViewControllerInstance);
});
it('should automatically remove that getController() accessor method from the target view when it is destroyed', function() {
var exampleViewControllerInstance, exampleViewInstance;
exampleViewInstance = null;
exampleViewControllerInstance = null;
Ext.define('ExampleViewController', {
extend: 'Deft.mvc.ViewController'
});
Ext.define('ExampleView', {
extend: 'Ext.Container',
mixins: ['Deft.mixin.Controllable'],
controller: 'ExampleViewController'
});
exampleViewInstance = Ext.create('ExampleView');
exampleViewInstance.destroy();
return expect(exampleViewInstance.getController).toBe(void 0);
});
return it('should re-throw any error thrown by the view controller during instantiation', function() {
Ext.define('ExampleErrorThrowingViewController', {
extend: 'Deft.mvc.ViewController',
constructor: function() {
throw new Error('Error thrown by \`ExampleErrorThrowingViewController\`.');
}
});
Ext.define('ExampleView', {
extend: 'Ext.Container',
mixins: ['Deft.mixin.Controllable'],
controller: 'ExampleErrorThrowingViewController'
});
expect(function() {
Ext.create('ExampleView');
}).toThrow('Error thrown by \`ExampleErrorThrowingViewController\`.');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment