2015-09-04 16:44:08 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2015-09-04 19:44:49 +00:00
|
|
|
/*global define,describe,it,expect,beforeEach,waitsFor,afterEach,jasmine*/
|
2015-09-04 16:44:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EventSpec. Created by vwoeltje on 11/6/14. Modified by shale on 06/23/2015.
|
|
|
|
*/
|
|
|
|
define(
|
|
|
|
["../src/ConductorRepresenter"],
|
|
|
|
function (ConductorRepresenter) {
|
|
|
|
"use strict";
|
|
|
|
|
2015-09-04 19:12:21 +00:00
|
|
|
var SCOPE_METHODS = [
|
|
|
|
'$on',
|
|
|
|
'$watch',
|
|
|
|
'$broadcast',
|
|
|
|
'$emit',
|
|
|
|
'$new',
|
|
|
|
'$destroy'
|
|
|
|
],
|
|
|
|
ELEMENT_METHODS = [
|
|
|
|
'hasClass',
|
|
|
|
'addClass',
|
2015-09-04 19:44:49 +00:00
|
|
|
'removeClass',
|
|
|
|
'css',
|
|
|
|
'after',
|
|
|
|
'remove'
|
2015-09-04 19:12:21 +00:00
|
|
|
];
|
|
|
|
|
2015-09-04 16:44:08 +00:00
|
|
|
describe("ConductorRepresenter", function () {
|
2015-10-02 23:38:32 +00:00
|
|
|
var mockThrottle,
|
|
|
|
mockConductorService,
|
2015-09-04 19:12:21 +00:00
|
|
|
mockCompile,
|
|
|
|
testViews,
|
|
|
|
mockScope,
|
|
|
|
mockElement,
|
|
|
|
mockConductor,
|
|
|
|
mockCompiledTemplate,
|
|
|
|
mockNewScope,
|
|
|
|
mockNewElement,
|
|
|
|
representer;
|
|
|
|
|
2015-09-04 19:44:49 +00:00
|
|
|
function fireWatch(scope, watch, value) {
|
|
|
|
scope.$watch.calls.forEach(function (call) {
|
|
|
|
if (call.args[0] === watch) {
|
|
|
|
call.args[1](value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-04 19:12:21 +00:00
|
|
|
beforeEach(function () {
|
2015-10-02 23:38:32 +00:00
|
|
|
mockThrottle = jasmine.createSpy('throttle');
|
2015-09-04 19:12:21 +00:00
|
|
|
mockConductorService = jasmine.createSpyObj(
|
|
|
|
'conductorService',
|
|
|
|
['getConductor']
|
|
|
|
);
|
|
|
|
mockCompile = jasmine.createSpy('$compile');
|
|
|
|
testViews = [ { someKey: "some value" } ];
|
|
|
|
mockScope = jasmine.createSpyObj('scope', SCOPE_METHODS);
|
|
|
|
mockElement = jasmine.createSpyObj('element', ELEMENT_METHODS);
|
|
|
|
mockConductor = jasmine.createSpyObj(
|
|
|
|
'conductor',
|
2015-10-02 23:05:23 +00:00
|
|
|
[ 'displayStart', 'displayEnd' ]
|
2015-09-04 19:12:21 +00:00
|
|
|
);
|
|
|
|
mockCompiledTemplate = jasmine.createSpy('template');
|
|
|
|
mockNewScope = jasmine.createSpyObj('newScope', SCOPE_METHODS);
|
2015-09-04 19:44:49 +00:00
|
|
|
mockNewElement = jasmine.createSpyObj('newElement', ELEMENT_METHODS);
|
|
|
|
mockNewElement[0] = mockNewElement;
|
|
|
|
|
|
|
|
mockConductorService.getConductor.andReturn(mockConductor);
|
|
|
|
mockCompile.andReturn(mockCompiledTemplate);
|
|
|
|
mockCompiledTemplate.andReturn(mockNewElement);
|
|
|
|
mockScope.$new.andReturn(mockNewScope);
|
2015-10-02 23:38:32 +00:00
|
|
|
mockThrottle.andCallFake(function (fn) {
|
|
|
|
return fn;
|
|
|
|
});
|
2015-09-04 19:44:49 +00:00
|
|
|
|
|
|
|
representer = new ConductorRepresenter(
|
2015-10-02 23:38:32 +00:00
|
|
|
mockThrottle,
|
2015-09-04 19:44:49 +00:00
|
|
|
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.displayStart.andReturn(1977);
|
|
|
|
mockConductor.displayEnd.andReturn(1984);
|
|
|
|
representer.represent(testViews[0], {});
|
|
|
|
|
|
|
|
expect(mockNewScope.conductor).toEqual({
|
2015-09-10 18:31:40 +00:00
|
|
|
inner: { start: 1977, end: 1984 },
|
2015-10-02 23:05:23 +00:00
|
|
|
outer: { start: 1977, end: 1984 }
|
2015-09-04 19:44:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates conductor state from scope", function () {
|
|
|
|
var testState = {
|
2015-09-10 18:31:40 +00:00
|
|
|
inner: { start: 42, end: 1984 },
|
|
|
|
outer: { start: -1977, end: 12321 }
|
2015-09-04 19:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
representer.represent(testViews[0], {});
|
|
|
|
|
|
|
|
mockNewScope.conductor = testState;
|
|
|
|
|
2015-09-10 18:31:40 +00:00
|
|
|
fireWatch(mockNewScope, 'conductor.inner.start', testState.inner.start);
|
2015-09-04 19:44:49 +00:00
|
|
|
expect(mockConductor.displayStart).toHaveBeenCalledWith(42);
|
|
|
|
|
2015-09-10 18:31:40 +00:00
|
|
|
fireWatch(mockNewScope, 'conductor.inner.end', testState.inner.end);
|
2015-09-04 19:44:49 +00:00
|
|
|
expect(mockConductor.displayEnd).toHaveBeenCalledWith(1984);
|
2015-09-04 19:12:21 +00:00
|
|
|
});
|
2015-09-04 16:44:08 +00:00
|
|
|
|
2015-10-02 23:38:32 +00:00
|
|
|
describe("when bounds are changing", function () {
|
|
|
|
var mockThrottledFn = jasmine.createSpy('throttledFn'),
|
|
|
|
testBounds;
|
|
|
|
|
|
|
|
function fireThrottledFn() {
|
|
|
|
mockThrottle.mostRecentCall.args[0]();
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockThrottle.andReturn(mockThrottledFn);
|
|
|
|
representer.represent(testViews[0], {});
|
|
|
|
testBounds = { start: 0, end: 1000 };
|
|
|
|
mockNewScope.conductor.inner = testBounds;
|
|
|
|
mockConductor.displayStart.andCallFake(function () {
|
|
|
|
return testBounds.start;
|
|
|
|
});
|
|
|
|
mockConductor.displayEnd.andCallFake(function () {
|
|
|
|
return testBounds.end;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not broadcast while bounds are changing", function () {
|
|
|
|
expect(mockScope.$broadcast).not.toHaveBeenCalled();
|
|
|
|
testBounds.start = 100;
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.start', testBounds.start);
|
|
|
|
testBounds.end = 500;
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.end', testBounds.end);
|
|
|
|
fireThrottledFn();
|
|
|
|
testBounds.start = 200;
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.start', testBounds.start);
|
|
|
|
testBounds.end = 400;
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.end', testBounds.end);
|
|
|
|
fireThrottledFn();
|
|
|
|
expect(mockScope.$broadcast).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does broadcast when bounds have stabilized", function () {
|
|
|
|
expect(mockScope.$broadcast).not.toHaveBeenCalled();
|
|
|
|
testBounds.start = 100;
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.start', testBounds.start);
|
|
|
|
testBounds.end = 500;
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.end', testBounds.end);
|
|
|
|
fireThrottledFn();
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.start', testBounds.start);
|
|
|
|
fireWatch(mockNewScope, 'conductor.inner.end', testBounds.end);
|
|
|
|
fireThrottledFn();
|
|
|
|
expect(mockScope.$broadcast).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-04 16:44:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|