Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Unit tests for _renderMachineToken.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcsackett committed May 20, 2014
1 parent 438bc40 commit e9e4f9d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
11 changes: 6 additions & 5 deletions app/widgets/machine-view-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ YUI.add('machine-view-panel', function(Y) {
_bindEvents: function() {
var db = this.get('db');
this.addEvent(db.machines.after(
['add', 'remove', '*:change'], this._onMachinesChange, this)
['add', 'remove', '*:change'], this._onMachinesChange, this)
);

this.addEvent(db.units.after(
['add', 'remove', '*:change'], this._onUnitsChange, this));
['add', 'remove', '*:change'], this._onUnitsChange, this));

this.on('*:unit-token-drag-start', this._showDraggingUI, this);
this.on('*:unit-token-drag-end', this._hideDraggingUI, this);
Expand Down Expand Up @@ -370,14 +370,15 @@ YUI.add('machine-view-panel', function(Y) {
* @param {Node} list the list node to append the machine to.
*/
_renderMachineToken: function(machineOrId) {
var machine;
var db = this.get('db'),
machine;
if (typeof machineOrId === 'string') {
machine = this.get('db').machines.getById(machineOrId);
machine = db.machines.getById(machineOrId);
} else {
machine = machineOrId;
}
var node = Y.Node.create('<li></li>'),
units = this.get('db').units.filterByMachine(machine.id, true);
units = db.units.filterByMachine(machine.id, true);
this._updateMachineWithUnitData(machine, units);
var token = new views.MachineToken({
container: node,
Expand Down
87 changes: 85 additions & 2 deletions test/test_machine_view_panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('machine view panel view', function() {
});

describe('_smartUpdateList', function() {
// _smartUpdateList use two private methods, _addNewTOkens and
// _smartUpdateList use two private methods, _addNewTokens and
// _removeOldTokens that do all the work--we can just test those.
it('can determine if a model is new and needs token', function() {
var models = [{id: 1}];
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('machine view panel view', function() {
});

it('can determine if a token has no model and remove it', function() {
container.append(Y.Node.create('<li></li>'));
container.append(Y.Node.create('<li data-id="2"></li>'));
var models = [{id: 1}],
elements = container.all('li');
assert.equal(elements.size(), 1);
Expand Down Expand Up @@ -403,6 +403,61 @@ describe('machine view panel view', function() {


describe('machine column', function() {
it('can create machine tokens from a machine', function() {
var updateStub = utils.makeStubMethod(
view, '_updateMachineWithUnitData');
this._cleanups.push(updateStub.reset);
var db = view.get('db'),
filterStub = utils.makeStubMethod(db.units, 'filterByMachine', []),
getByIdStub = utils.makeStubMethod(db.machines, 'getById', machine);
this._cleanups.push(filterStub.reset);
this._cleanups.push(getByIdStub.reset);
var rendered = false,
target;
var viewStub = utils.makeStubMethod(views, 'MachineToken', {
render: function() { rendered = true; },
addTarget: function(t) { target = t; }
});
this._cleanups.push(viewStub.reset);
view._renderMachineToken(machine);

assert.equal(updateStub.calledOnce(), true);
assert.equal(viewStub.calledOnce(), true);
// GetById should not be called, as we have provided a machine.
assert.equal(getByIdStub.callCount(), 0);
// Verify token is rendered and has the view added as a target.
assert.equal(rendered, true);
assert.equal(target, view);
});

it('can create machine tokens from the ID as a string', function() {
var updateStub = utils.makeStubMethod(
view, '_updateMachineWithUnitData');
this._cleanups.push(updateStub.reset);
var db = view.get('db'),
filterStub = utils.makeStubMethod(db.units, 'filterByMachine', []),
getByIdStub = utils.makeStubMethod(db.machines, 'getById', machine);
this._cleanups.push(filterStub.reset);
this._cleanups.push(getByIdStub.reset);
var rendered = false,
target;
var viewStub = utils.makeStubMethod(views, 'MachineToken', {
render: function() { rendered = true; },
addTarget: function(t) { target = t; }
});
this._cleanups.push(viewStub.reset);
view._renderMachineToken('1');

assert.equal(updateStub.calledOnce(), true);
assert.equal(viewStub.calledOnce(), true);
// GetById should be called, so we can look up the machine.
assert.equal(getByIdStub.calledOnce(), 1);
assert.equal(getByIdStub.lastArguments()[0], '1');
// Verify token is rendered and has the view added as a target.
assert.equal(rendered, true);
assert.equal(target, view);
});

it('should render a list of machines', function() {
view.render();
var list = container.all('.machines .content li');
Expand All @@ -422,6 +477,31 @@ describe('machine view panel view', function() {
assert.equal(view._machinesHeader.get(
'container').one('.label').get('text'), label);
});

it('can update a machine via an object', function() {
machine = {id: 1};
container = view.get('container');
var oneStub = utils.makeStubMethod(container, 'one', null);
this._cleanups.push(oneStub.reset);
view._updateMachine(machine);
assert.equal(
'.machines .content .machine-token[data-id="1"]',
oneStub.lastArguments()[0] ,
'Selector created with wrong id');
});

it('can update a machine via a string', function() {
machine = '1';
container = view.get('container');
var oneStub = utils.makeStubMethod(container, 'one', null);
this._cleanups.push(oneStub.reset);
view._updateMachine(machine);
assert.equal(
'.machines .content .machine-token[data-id="1"]',
oneStub.lastArguments()[0] ,
'Selector created with wrong id');
});

/// XXX Jeff May 15 2014 - drop handlers no longer update UI. Fix once
// handlers update the UI.
it.skip('updates and re-renders a specific machine', function() {
Expand All @@ -437,6 +517,7 @@ describe('machine view panel view', function() {
assert.equal(node.all('.service-icons .unit').size(),
machine.units.length, 'icons not updated along with units');
});

/// XXX Jeff May 15 2014 - drop handlers no longer update UI. Fix once
// handlers update the UI.
it.skip('should add new tokens when machines are added', function() {
Expand Down Expand Up @@ -568,6 +649,7 @@ describe('machine view panel view', function() {
render: function() { rendered = true; },
addTarget: function(t) { target = t; }
});
this._cleanups.push(viewStub.reset);
var containerParent = utils.makeContainer(this, 'machine-view-panel'),
container = {};
view._createContainerToken(containerParent, container);
Expand All @@ -593,6 +675,7 @@ describe('machine view panel view', function() {
render: function() { rendered = true; },
addTarget: function(t) { target = t; }
});
this._cleanups.push(viewStub.reset);
var containerParent = utils.makeContainer(this, 'machine-view-panel'),
units = [{}],
container = {};
Expand Down

0 comments on commit e9e4f9d

Please sign in to comment.