Skip to content

Commit

Permalink
Merge pull request #3 from PolymerLabs/testy
Browse files Browse the repository at this point in the history
Start of unit tests (via WCT), and a bug fix
  • Loading branch information
Scott J. Miles committed Oct 27, 2014
2 parents 1fd3877 + 3121779 commit 23b085e
Show file tree
Hide file tree
Showing 8 changed files with 23,918 additions and 20 deletions.
8 changes: 4 additions & 4 deletions perf/baseline/x-div-base-1-callbacks.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
attachedCallback: function() {
},

dettachedCallback: function() {
detachedCallback: function() {
},

attributeChangedCallback: function() {
Expand All @@ -35,11 +35,11 @@
prototype.__proto__ = Base;
document.registerElement(prototype.name, {prototype: prototype});
};

Polymer({
name: 'x-div'
});

</script>

<body>
Expand All @@ -58,4 +58,4 @@

<script>console.perfEnd();</script>

</body>
</body>
12 changes: 6 additions & 6 deletions perf/baseline/x-div-base-2-featureless.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
registerCallback: function() {
// `this` context is a prototype, not an instance
var prototype = this;
prototype._template =
prototype._template =
document.currentScript.ownerDocument.querySelector('template');
this.registerFeatures(prototype);
this.registered(prototype);
Expand Down Expand Up @@ -80,12 +80,12 @@
// for overriding
},

dettachedCallback: function() {
detachedCallback: function() {
// reserved for canonical behavior
this.dettached();
this.detached();
},

dettached: function() {
detached: function() {
// for overriding
},

Expand All @@ -97,7 +97,7 @@
attributeChanged: function() {
// for overriding
}

};

Base.__proto__ = HTMLElement.prototype;
Expand Down Expand Up @@ -129,4 +129,4 @@

<script>console.perfEnd();</script>

</body>
</body>
12 changes: 6 additions & 6 deletions perf/baseline/x-div-base-3-imports.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
registerCallback: function() {
// `this` context is a prototype, not an instance
var prototype = this;
prototype._template =
prototype._template =
document.currentScript.ownerDocument.querySelector('template');
this.registerFeatures(prototype);
this.registered(prototype);
Expand Down Expand Up @@ -82,12 +82,12 @@
// for overriding
},

dettachedCallback: function() {
detachedCallback: function() {
// reserved for canonical behavior
this.dettached();
this.detached();
},

dettached: function() {
detached: function() {
// for overriding
},

Expand All @@ -99,7 +99,7 @@
attributeChanged: function() {
// for overriding
}

};

Base.__proto__ = HTMLElement.prototype;
Expand Down Expand Up @@ -145,4 +145,4 @@

<script>console.perfEnd();</script>

</body>
</body>
8 changes: 4 additions & 4 deletions polymer/src/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@
// for overriding
},

dettachedCallback: function() {
detachedCallback: function() {
// reserved for canonical behavior
this.dettached();
this.detached();
},

dettached: function() {
detached: function() {
// for overriding
},

Expand All @@ -86,4 +86,4 @@

};

</script>
</script>
Empty file added polymer/src/minimal.html
Empty file.
14 changes: 14 additions & 0 deletions polymer/test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="../../web-component-tester/browser.js"></script>
</head>
<body>
<script>
WCT.loadSuites([
'unit/base.html',
]);
</script>
</body>
</html>
171 changes: 171 additions & 0 deletions polymer/test/unit/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../src/lang.html">
<link rel="import" href="../../src/base.html">
</head>
<body>
<script>

var OrigBase = window.Base;
var Base;
var Child;
var instance;

beforeEach(function() {
// Ensure a clean environment for each test.
Base = extend({}, OrigBase);
Base._features = [];
Child = Object.create(Base);
instance = Object.create(Child);
});

suite('addFeature', function() {

test('mixes the feature into Base', function() {
assert.notOk(Base.someProperty);
Base.addFeature({someProperty: 123});
assert.equal(Base.someProperty, 123);
});

// TODO(nevir): What's up with this behavior?
test('kills init', function() {
Base.init = function() {};
Base.addFeature({});
assert.notOk(Base.init);
});

// TODO(nevir): Ditto above.
test('kills register', function() {
Base.register = function() {};
Base.addFeature({});
assert.notOk(Base.register);
});

});

suite('registerCallback', function() {

test('calls register() for any features', function() {
var called = [];
Base.addFeature({register: function() {called.push('one')}});
Base.addFeature({register: function() {called.push('two')}});
assert.deepEqual(called, []);

Child.registerCallback();
assert.includeMembers(called, ['one', 'two']);
});

test('passes the prototype to feature register()', function() {
Base.addFeature({register: function(prototype) {
assert.equal(prototype, Child);
}});
Child.registerCallback();
});

// TODO(nevir): Pull sinon into WCT to make this sort of test cleaner.
test('calls registered() after features', function() {
var lastCalled = null;
Base.addFeature({register: function() {
assert.equal(lastCalled, null);
lastCalled = 'feature';
}});
Child.registered = function() {
assert.equal(lastCalled, 'feature');
lastCalled = 'registered';
};

Child.registerCallback(Base);
assert.equal(lastCalled, 'registered');
});

});

suite('createdCallback', function() {

// TODO(nevir): sinonify.
test('calls lifecycle events in the proper order', function() {
var lastCalled = null;
Child.beforeCreated = function() {
assert.equal(lastCalled, null);
lastCalled = 'beforeCreated';
};
Base.addFeature({init: function() {
assert.equal(lastCalled, 'beforeCreated');
lastCalled = 'feature';
}});
Child.created = function() {
assert.equal(lastCalled, 'feature');
lastCalled = 'created';
};
Child.afterCreated = function() {
assert.equal(lastCalled, 'created');
lastCalled = 'afterCreated';
};

instance.createdCallback();
assert.equal(lastCalled, 'afterCreated');
});

test('calls init() for any features', function() {
var called = [];
Base.addFeature({init: function() {called.push('one')}});
Base.addFeature({init: function() {called.push('two')}});
assert.deepEqual(called, []);

instance.createdCallback();
assert.includeMembers(called, ['one', 'two']);
});

test('calls feature init() with the correct `this`', function() {
Base.addFeature({init: function() {
assert.equal(this, instance);
}});
instance.createdCallback();
});

});

suite('attachedCallback', function() {

test('calls attached()', function() {
var called = false;
Child.attached = function() {called = true};
instance.attachedCallback();
assert.isTrue(called);
});

});

suite('detachedCallback', function() {

test('calls detached()', function() {
var called = false;
Child.detached = function() {called = true};
instance.detachedCallback();
assert.isTrue(called);
});

});

suite('attributeChangedCallback', function() {

test('calls attributeChanged()', function() {
var args = null;
Child.attributeChanged = function() {args = arguments};
instance.attributeChangedCallback('attr', null, 1, 'stuff');

assert.equal(args.length, 4);
assert.equal(args[0], 'attr');
assert.equal(args[1], null);
assert.equal(args[2], 1);
assert.equal(args[3], 'stuff');
});

});

</script>
</body>
</html>
Loading

0 comments on commit 23b085e

Please sign in to comment.