Permalink
Browse files

added Declaration tests.

  • Loading branch information...
dglazkov committed Mar 19, 2012
1 parent 6d2bba2 commit 3378fddcbeb896523fb537a8aede8bb06bf42339
Showing with 111 additions and 2 deletions.
  1. +0 −1 src/components-polyfill.js
  2. +1 −1 tests/declaration-factory.js
  3. +109 −0 tests/declaration.js
  4. +1 −0 tests/index.html
@@ -39,7 +39,6 @@ scope.Declaration = function(name, tagName)
scope.Declaration.prototype = {
generateConstructor: function()
{
- // FIXME: Test this.
var tagName = this.element.extends;
var create = this.create;
var extended = function()
@@ -47,10 +47,10 @@ test('.createDeclaration must require "extends" attribute', function() {
});
test('.createDeclaration must create new Declaration instance', function() {
- this.declaration = null;
var element = document.createElement('div');
element.setAttribute('name', 'foo');
element.setAttribute('extends', 'div');
+ this.declaration = null;
this.declarationFactory.createDeclaration(element);
equal(this.declaration.constructor, polyfill.Declaration);
});
View
@@ -0,0 +1,109 @@
+module('Declaration', {
+ setup: function() {
+ this.actualEval = window.eval;
+ window.eval = function(code) {
+ this.codeEvaluated = code;
+ }.bind(this);
+ this.actualHTMLElementElement = polyfill.HTMLElementElement;
+ polyfill.HTMLElementElement = function(name, tagName, declaration) {
+ this.name = name;
+ this.extends = tagName;
+ this.declaration = declaration;
+ }
+ },
+ teardown: function() {
+ polyfill.HTMLElementElement = this.actualHTMLElementElement;
+ window.eval = this.actualEval;
+ }
+});
+
+test('.generateConstructor must create a swizzled-prototype, HTMLElement-derived object', function() {
+ var mockElement = function() {}
+ var count = 0;
+ var result = new (polyfill.Declaration.prototype.generateConstructor.call({
+ element: {
+ extends: 'div'
+ },
+ create: function() {
+ count = 0;
+ },
+ elementPrototype: mockElement.prototype
+ }));
+ equal(result.__proto__.constructor, mockElement);
+ equal(result.__proto__.__proto__.constructor, HTMLDivElement);
+});
+
+test('.evalScript must attempt to evaluate script, wrapped in a shim', function() {
+ polyfill.Declaration.prototype.evalScript.call({}, {
+ textContent: 'foo'
+ });
+ equal(this.codeEvaluated, '(function(){\nfoo\n}).call(this.element);');
+});
+
+test('.addTemplate must set the this.template value', function() {
+ var mockDeclaration = {};
+ polyfill.Declaration.prototype.addTemplate.call(mockDeclaration, 'foo');
+ equal(mockDeclaration.template, 'foo');
+});
+
+test('.morph must swizzle prototype of an existing object', 4, function() {
+ var mockElementPrototype = {};
+ var element = {};
+ var shadowRootCreated = false;
+ polyfill.Declaration.prototype.morph.call({
+ elementPrototype: mockElementPrototype,
+ createShadowRoot: function(e) {
+ strictEqual(e.__proto__, mockElementPrototype);
+ strictEqual(e, element);
+ return 'foo';
+ },
+ create: function(shadowRoot) {
+ strictEqual(this, element);
+ equal(shadowRoot, 'foo');
+ }
+ }, element);
+});
+
+test('.createShadowRoot must exit early if there is no this.template', function() {
+ var result = polyfill.Declaration.prototype.createShadowRoot.call({});
+ ok(!result);
+});
+
+test('.createShadowRoot must create a WebKitShadowRoot instance', function() {
+ var host = document.createElement('div');
+ var result = polyfill.Declaration.prototype.createShadowRoot.call({
+ template: { childNodes: [] }
+ }, host);
+ equal(result.__proto__.constructor, WebKitShadowRoot);
+ deepEqual(Object.keys(result), [ 'nextSibling', 'childNodes', 'nodeType', 'host', 'prefix', 'parentElement', 'nodeName',
+ 'activeElement', 'textContent', 'namespaceURI', 'firstChild', 'innerHTML', 'localName', 'lastChild', 'baseURI',
+ 'previousSibling', 'ownerDocument', 'nodeValue', 'parentNode', 'attributes' ]);
+});
+
+test('.createShadowRoot must clone template child nodes into the newly created WebKitShadowRoot instance', function() {
+ var host = document.createElement('div');
+ var span = host.appendChild(document.createElement('span'));
+ var b = host.appendChild(document.createElement('b'));
+ equal(host.childNodes.length, 2);
+ var result = polyfill.Declaration.prototype.createShadowRoot.call({
+ template: { childNodes: [ span, b ] }
+ }, host);
+ equal(result.firstChild.tagName, 'SPAN');
+ equal(result.lastChild.tagName, 'B');
+ equal(host.childNodes.length, 2);
+});
+
+test('.prototypeFromTagName must return correct HTML element prototype', function() {
+ equal(polyfill.Declaration.prototype.prototypeFromTagName.call({}, 'div').constructor, HTMLDivElement);
+ equal(polyfill.Declaration.prototype.prototypeFromTagName.call({}, 'span').constructor, HTMLSpanElement);
+ equal(polyfill.Declaration.prototype.prototypeFromTagName.call({}, 'table').constructor, HTMLTableElement);
+});
+
+test('Declaration constructor must correctly initialize instance members', function() {
+ var declaration = new polyfill.Declaration('scones', 'div');
+ equal(declaration.elementPrototype.constructor, HTMLDivElement);
+ equal(declaration.element.name, 'scones');
+ equal(declaration.element.extends, 'div');
+ strictEqual(declaration.element.declaration, declaration);
+ ok(!!declaration.element.generatedConstructor);
+});
View
@@ -12,6 +12,7 @@
<script src="loader.js"></script>
<script src="parser.js"></script>
<script src="declaration-factory.js"></script>
+ <script src="declaration.js"></script>
<script> module(); </script>
</head>
<body>

0 comments on commit 3378fdd

Please sign in to comment.