Skip to content

Commit

Permalink
Updated specs to use requirejs and define
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtueMe committed Oct 31, 2012
1 parent 2df1724 commit 60fc46a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 142 deletions.
9 changes: 2 additions & 7 deletions spec/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

<script type="text/javascript" src="../src/extensions/backbone/lib/require.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="js/mediator_spec.js"></script>

<script type="text/javascript">

// Aura configuration object is separate from require.config so we can have
Expand Down Expand Up @@ -83,10 +80,8 @@
require.config( require.aura );

/*****/

require(['aura_core'], function (core) {

window.core = core;
<!-- require source files here... -->
require(['spec/js/mediator_spec'], function () {

var htmlReporter = new jasmine.HtmlReporter();
var jasmineEnv = jasmine.getEnv();
Expand Down
276 changes: 141 additions & 135 deletions spec/js/mediator_spec.js
Original file line number Diff line number Diff line change
@@ -1,156 +1,162 @@
describe('Mediator', function () {
define(['aura_core'], function (core) {
describe('Mediator', function () {

var mediator,
channels,
TEST_CHANNEL = 'test';
var mediator,
getChannels,
TEST_CHANNEL = 'test';


beforeEach(function() {
mediator = window.core;
channels = mediator.getChannels();
beforeEach(function() {
mediator = core;
channels = mediator.getChannels();

//override method util as it uses jQuery proxy and doesn't
//allow comparison of actual callback function object.
mediator.util.method = function (fn, context) {
return fn;
};
mediator.util.method = function (fn, context) {
return fn;
};

//verify setup
expect(mediator).toBeDefined();
expect(channels).toBeDefined();

delete channels[TEST_CHANNEL]; //clean our test channel
});

describe('on', function() {

describe('verification of parameters', function() {
it('should throw an error if all the params are not specified', function () {
expect(function () {
mediator.on();
}).toThrow(new Error('Channel, callback, and context must be defined'));
});

it("should throw an error if typeof channel is NOT string", function() {
expect(function() {
mediator.on({}, 'subscriber', function () {}, {})
}).toThrow(new Error('Channel must be a string'));
});

it("should throw an error if typeof subscriber is NOT string", function() {
expect(function() {
mediator.on('channel', {}, function(){}, {})
}).toThrow(new Error('Subscriber must be a string'));
})

it("should throw an error if typeof callback is NOT a function", function() {
expect(function() {
mediator.on('channel', 'subscriber', 'callback', {})
}).toThrow(new Error('Callback must be a function'));
});
expect(mediator).toBeDefined();
expect(channels).toBeDefined();

delete channels[TEST_CHANNEL]; //clean our test channel
});

describe('on', function() {

describe('verification of parameters', function() {
it('should throw an error if all the params are not specified', function () {
expect(function () {
mediator.on();
}).toThrow(new Error('Channel, callback, and context must be defined'));
});

it('should allow an event to be subscribed', function() {
mediator.on(TEST_CHANNEL, 'spec', function() {}, this);
expect(channels[TEST_CHANNEL]).toBeDefined();
});

it('should be able assign a specific callback for subscribed event', function() {
var callback,
callbackResult = 'callback';
mediator.on(TEST_CHANNEL, 'spec', function() { return callbackResult; }, this);
callback = channels[TEST_CHANNEL][0].callback;
expect(callback()).toBe(callbackResult);
});

it('should allow subscribing multiple callbacks for single event channel', function() {
var callback1 = function() {};
var callback2 = function() {};

mediator.on(TEST_CHANNEL, 'spec', callback1, this);
mediator.on(TEST_CHANNEL, 'spec', callback2, this);

//expect(channels[TEST_CHANNEL]).toContain(callback1, callback2);
expect(channels[TEST_CHANNEL].length).toBe(2);
});
});

describe('emit', function() {

describe('verification of parameters', function() {
it('should throw an error if all the params are not specified', function () {
expect(function () {
mediator.emit();
}).toThrow(new Error('Channel must be defined'));
});

it('should throw an error if typeof channel param is not string', function () {
expect(function () {
mediator.emit({});
}).toThrow(new Error('Channel must be a string'));
});
it("should throw an error if typeof channel is NOT string", function() {
expect(function() {
mediator.on({}, 'subscriber', function () {}, {});
}).toThrow(new Error('Channel must be a string'));
});

it('should call every callback for a channel, within the correct context', function () {
var callback = sinon.spy();
channels[TEST_CHANNEL] = [
{callback:callback}
];
it("should throw an error if typeof subscriber is NOT string", function() {
expect(function() {
mediator.on('channel', {}, function(){}, {});
}).toThrow(new Error('Subscriber must be a string'));
});

mediator.emit(TEST_CHANNEL);
it("should throw an error if typeof callback is NOT a function", function() {
expect(function() {
mediator.on('channel', 'subscriber', 'callback', {});
}).toThrow(new Error('Callback must be a function'));
});
});

expect(callback).toHaveBeenCalled();
it('should allow an event to be subscribed', function() {
mediator.on(TEST_CHANNEL, 'spec', function() {}, this);
expect(channels[TEST_CHANNEL]).toBeDefined();
});

it('should pass additional arguments to every call callback for a channel', function () {
var callback = sinon.spy();
var argument = {};
channels[TEST_CHANNEL] = [
{callback:callback}
];
it('should be able assign a specific callback for subscribed event', function() {
var callback,
callbackResult = 'callback';

mediator.emit(TEST_CHANNEL, argument);
mediator.on(TEST_CHANNEL, 'spec', function() { return callbackResult; }, this);
callback = channels[TEST_CHANNEL][0].callback;
expect(callback()).toBe(callbackResult);
});

expect(callback).toHaveBeenCalledWith(argument);
});
it('should allow subscribing multiple callbacks for single event channel', function() {
var callback1 = function() {};
var callback2 = function() {};

mediator.on(TEST_CHANNEL, 'spec', callback1, this);
mediator.on(TEST_CHANNEL, 'spec', callback2, this);

//expect(channels[TEST_CHANNEL]).toContain(callback1, callback2);
expect(channels[TEST_CHANNEL].length).toBe(2);
});
});

it('should return false if channel has not been defined', function () {
var called = mediator.emit(TEST_CHANNEL);
expect(called).toBe(false);
describe('emit', function() {

describe('verification of parameters', function() {
it('should throw an error if all the params are not specified', function () {
expect(function () {
mediator.emit();
}).toThrow(new Error('Channel must be defined'));
});

it('should add to emit queue if widget is loading', function() {
channels[TEST_CHANNEL] = [
{callback:function() {}}
];
mediator.start({ channel:TEST_CHANNEL, options: { element: '#nothing' } });

mediator.emit(TEST_CHANNEL);

expect(mediator.getEmitQueueLength()).toBe(1);
})
});

xdescribe('start', function() {
it('should throw an error if all the params are not specified', function () {});
it('should throw an error if all the params are not the correct type', function () {});
it('should load (require) a widget that corresponds with a channel', function () {});
it('should call every callback for the channel, within the correct context', function () {});
it('should trigger a requirejs error if the widget does not exist', function (){});
});

xdescribe('stop', function() {
it('should throw an error if all the params are not specified', function () {});
it('should throw an error if all the params are not the correct type', function () {});
it('should call unload with the correct widget to unload from the app', function () {});
it('should empty the contents of a specific widget\'s container div', function () {});
});

// This one will need to be researched a little more to determine exactly what require does
xdescribe('unload', function () {
it('should throw an error if all the params are not specified', function () {});
it('should throw an error if all the params are not the correct type', function () {});
it('should unload a module and all modules under its widget path', function () {});
});

});
it('should throw an error if typeof channel param is not string', function () {
expect(function () {
mediator.emit({});
}).toThrow(new Error('Channel must be a string'));
});
});

it('should call every callback for a channel, within the correct context', function () {
var callback = sinon.spy();

channels[TEST_CHANNEL] = [
{callback:callback}
];

mediator.emit(TEST_CHANNEL);

expect(callback).toHaveBeenCalled();
});

it('should pass additional arguments to every call callback for a channel', function () {
var callback = sinon.spy();
var argument = {};

channels[TEST_CHANNEL] = [
{callback:callback}
];

mediator.emit(TEST_CHANNEL, argument);

expect(callback).toHaveBeenCalledWith(argument);
});

it('should return false if channel has not been defined', function () {
var called = mediator.emit(TEST_CHANNEL);

expect(called).toBe(false);
});

it('should add to emit queue if widget is loading', function() {
channels[TEST_CHANNEL] = [
{callback:function() {}}
];

mediator.start({ channel:TEST_CHANNEL, options: { element: '#nothing' } });

mediator.emit(TEST_CHANNEL);

expect(mediator.getEmitQueueLength()).toBe(1);
});
});

xdescribe('start', function() {
it('should throw an error if all the params are not specified', function () {});
it('should throw an error if all the params are not the correct type', function () {});
it('should load (require) a widget that corresponds with a channel', function () {});
it('should call every callback for the channel, within the correct context', function () {});
it('should trigger a requirejs error if the widget does not exist', function (){});
});

xdescribe('stop', function() {
it('should throw an error if all the params are not specified', function () {});
it('should throw an error if all the params are not the correct type', function () {});
it('should call unload with the correct widget to unload from the app', function () {});
it('should empty the contents of a specific widget\'s container div', function () {});
});

// This one will need to be researched a little more to determine exactly what require does
xdescribe('unload', function () {
it('should throw an error if all the params are not specified', function () {});
it('should throw an error if all the params are not the correct type', function () {});
it('should unload a module and all modules under its widget path', function () {});
});
});
});

0 comments on commit 60fc46a

Please sign in to comment.