[Time Controller] Test ConductorRepresenter

WTD-1515
This commit is contained in:
Victor Woeltjen
2015-09-04 12:44:49 -07:00
parent af462ff3ee
commit 01a6d2e6a7
2 changed files with 93 additions and 3 deletions

View File

@ -105,7 +105,7 @@ define(
// Create a new scope for the conductor // Create a new scope for the conductor
this.conductorScope(this.getScope().$new()); this.conductorScope(this.getScope().$new());
this.wireScope( wireScope(
this.conductorService.getConductor(), this.conductorService.getConductor(),
this.conductorScope(), this.conductorScope(),
this.getScope() this.getScope()

View File

@ -19,7 +19,7 @@
* this source code distribution or the Licensing information page available * this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information. * at runtime from the About dialog for additional information.
*****************************************************************************/ *****************************************************************************/
/*global define,describe,it,expect,beforeEach,waitsFor,jasmine*/ /*global define,describe,it,expect,beforeEach,waitsFor,afterEach,jasmine*/
/** /**
* EventSpec. Created by vwoeltje on 11/6/14. Modified by shale on 06/23/2015. * EventSpec. Created by vwoeltje on 11/6/14. Modified by shale on 06/23/2015.
@ -40,7 +40,10 @@ define(
ELEMENT_METHODS = [ ELEMENT_METHODS = [
'hasClass', 'hasClass',
'addClass', 'addClass',
'css' 'removeClass',
'css',
'after',
'remove'
]; ];
describe("ConductorRepresenter", function () { describe("ConductorRepresenter", function () {
@ -55,6 +58,14 @@ define(
mockNewElement, mockNewElement,
representer; representer;
function fireWatch(scope, watch, value) {
scope.$watch.calls.forEach(function (call) {
if (call.args[0] === watch) {
call.args[1](value);
}
});
}
beforeEach(function () { beforeEach(function () {
mockConductorService = jasmine.createSpyObj( mockConductorService = jasmine.createSpyObj(
'conductorService', 'conductorService',
@ -70,6 +81,85 @@ define(
); );
mockCompiledTemplate = jasmine.createSpy('template'); mockCompiledTemplate = jasmine.createSpy('template');
mockNewScope = jasmine.createSpyObj('newScope', SCOPE_METHODS); mockNewScope = jasmine.createSpyObj('newScope', SCOPE_METHODS);
mockNewElement = jasmine.createSpyObj('newElement', ELEMENT_METHODS);
mockNewElement[0] = mockNewElement;
mockConductorService.getConductor.andReturn(mockConductor);
mockCompile.andReturn(mockCompiledTemplate);
mockCompiledTemplate.andReturn(mockNewElement);
mockScope.$new.andReturn(mockNewScope);
representer = new ConductorRepresenter(
mockConductorService,
mockCompile,
testViews,
mockScope,
mockElement
);
});
afterEach(function () {
representer.destroy();
});
it("adds a conductor to views", function () {
representer.represent(testViews[0], {});
expect(mockElement.after).toHaveBeenCalledWith(mockNewElement);
});
it("adds nothing to non-view representations", function () {
representer.represent({ someKey: "something else" }, {});
expect(mockElement.after).not.toHaveBeenCalled();
});
it("removes the conductor when destroyed", function () {
representer.represent(testViews[0], {});
expect(mockNewElement.remove).not.toHaveBeenCalled();
representer.destroy();
expect(mockNewElement.remove).toHaveBeenCalled();
});
it("destroys any new scope created", function () {
representer.represent(testViews[0], {});
representer.destroy();
expect(mockNewScope.$destroy.calls.length)
.toEqual(mockScope.$new.calls.length);
});
it("exposes conductor state in scope", function () {
mockConductor.queryStart.andReturn(42);
mockConductor.queryEnd.andReturn(12321);
mockConductor.displayStart.andReturn(1977);
mockConductor.displayEnd.andReturn(1984);
representer.represent(testViews[0], {});
expect(mockNewScope.conductor).toEqual({
inner: [ 1977, 1984 ],
outer: [ 42, 12321 ]
});
});
it("updates conductor state from scope", function () {
var testState = {
inner: [ 42, 1984 ],
outer: [ -1977, 12321 ]
};
representer.represent(testViews[0], {});
mockNewScope.conductor = testState;
fireWatch(mockNewScope, 'conductor.inner[0]', testState.inner[0]);
expect(mockConductor.displayStart).toHaveBeenCalledWith(42);
fireWatch(mockNewScope, 'conductor.inner[1]', testState.inner[1]);
expect(mockConductor.displayEnd).toHaveBeenCalledWith(1984);
fireWatch(mockNewScope, 'conductor.outer[0]', -1977);
expect(mockConductor.queryStart).toHaveBeenCalledWith(-1977);
fireWatch(mockNewScope, 'conductor.outer[1]', 12321);
expect(mockConductor.queryEnd).toHaveBeenCalledWith(12321);
}); });
}); });