From 817c005845bcd5f8ea185937a4684d7f607eb697 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sun, 28 Sep 2014 20:02:32 -0700 Subject: [PATCH] test: added simple View test --- karma-mock-annotations.js | 5 ++- modules/change_detection/src/record.js | 8 ++-- modules/core/src/compiler/view.js | 40 ++++++++++--------- modules/core/test/compiler/view_spec.js | 16 ++++++++ modules/facade/src/collection.es6 | 7 +++- modules/facade/src/dom.dart | 3 ++ modules/facade/src/dom.es6 | 8 ++++ modules/rtts_assert/src/rtts_assert.es6 | 1 + modules/rtts_assert/test/rtts_assert_spec.es6 | 4 +- 9 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 modules/core/test/compiler/view_spec.js diff --git a/karma-mock-annotations.js b/karma-mock-annotations.js index c59b287bac..76291bd251 100644 --- a/karma-mock-annotations.js +++ b/karma-mock-annotations.js @@ -1,3 +1,6 @@ // TODO: Remove these annotations in the JS traceur build as they are only needed in Dart -window.FIELD = function() {}; \ No newline at end of file +window.FIELD = function() {}; +window.IMPLEMENTS = function() {}; +window.CONST = function() {}; +window.List = Array; diff --git a/modules/change_detection/src/record.js b/modules/change_detection/src/record.js index 7a353e48f3..b2e8cc6159 100644 --- a/modules/change_detection/src/record.js +++ b/modules/change_detection/src/record.js @@ -1,4 +1,4 @@ -import {ProtoWatchGroup, WatchGroup} from './watch_group'; +//import {ProtoWatchGroup, WatchGroup} from './watch_group'; export class ProtoRecord { @@ -20,7 +20,7 @@ export class ProtoRecord { // May be removed if we don't support coelsence. @FIELD('_updateContextNext:ProtoRecord') @FIELD('_clone') - constructor(watchGroup:ProtoWatchGroup, fieldName:String) { + constructor(watchGroup/*:ProtoWatchGroup*/, fieldName:String) { this.watchGroup = watchGroup; this.fieldName = fieldName; this.next = null; @@ -34,7 +34,7 @@ export class ProtoRecord { this._clone = null; } - instantiate(watchGroup:WatchGroup):Record { + instantiate(watchGroup/*:WatchGroup*/):Record { var record = this._clone = new Record(watchGroup, this); record.prev = this.prev._clone; record._checkPrev = this._checkPrev._clone; @@ -94,7 +94,7 @@ export class Record { @FIELD('_arguments') @FIELD('currentValue') @FIELD('previousValue') - constructor(watchGroup:WatchGroup, protoRecord:ProtoRecord) { + constructor(watchGroup/*:WatchGroup*/, protoRecord:ProtoRecord) { this.protoRecord = protoRecord; this.watchGroup = watchGroup; this.next = null; diff --git a/modules/core/src/compiler/view.js b/modules/core/src/compiler/view.js index 7e51af4f9b..d7032b4149 100644 --- a/modules/core/src/compiler/view.js +++ b/modules/core/src/compiler/view.js @@ -6,24 +6,6 @@ import {Module} from 'di/di'; import {ProtoElementInjector, ElementInjector} from './element_injector'; import {SetterFn} from 'change_detection/facade'; -export class ProtoView { - @FIELD('final _template:TemplateElement') - @FIELD('final _module:Module') - @FIELD('final _protoElementInjectors:List') - @FIELD('final _protoWatchGroup:ProtoWatchGroup') - constructor( - template:TemplateElement, - module:Module, - protoElementInjector:ProtoElementInjector, - protoWatchGroup:ProtoWatchGroup) - { - this._template = template; - this._module = module; - this._protoElementInjectors = protoElementInjector; - this._protoWatchGroup = protoWatchGroup; - } -} - @IMPLEMENTS(WatchGroupDispatcher) export class View { @FIELD('final _fragment:DocumentFragment') @@ -59,6 +41,28 @@ export class View { } } +export class ProtoView { +@FIELD('final _template:TemplateElement') +@FIELD('final _module:Module') +@FIELD('final _protoElementInjectors:List') +@FIELD('final _protoWatchGroup:ProtoWatchGroup') + constructor( + template:TemplateElement, + module:Module, + protoElementInjector:ProtoElementInjector, + protoWatchGroup:ProtoWatchGroup) + { + this._template = template; + this._module = module; + this._protoElementInjectors = protoElementInjector; + this._protoWatchGroup = protoWatchGroup; + } + + instantiate():View { + return new View(DOM.clone(this._template.content)); + } +} + export class ElementInjectorTarget { @FIELD('final _elementInjectorIndex:int') diff --git a/modules/core/test/compiler/view_spec.js b/modules/core/test/compiler/view_spec.js new file mode 100644 index 0000000000..4c86e413cd --- /dev/null +++ b/modules/core/test/compiler/view_spec.js @@ -0,0 +1,16 @@ +import {describe, id} from 'test_lib/test_lib'; +import {ProtoView, View} from './view'; +import {DOM} from 'facade/dom'; + +export function main() { + describe('view', () => { + describe('ProtoView', () => { + it('should create an instance of view', () => { + var template = DOM.createTemplate('Hello world!'); + var pv = new ProtoView(template, null, null, null); + var view:View = pv.instantiate(); + expect(view instanceof View).toBe(true); + }); + }); + }); +} diff --git a/modules/facade/src/collection.es6 b/modules/facade/src/collection.es6 index d80c3eb8e2..b0199a2148 100644 --- a/modules/facade/src/collection.es6 +++ b/modules/facade/src/collection.es6 @@ -11,5 +11,8 @@ export class MapWrapper { export class ListWrapper { static create():List { return new List(); } static get(m, k) { return m[k]; } - static set(m, k, v) { m[k] = v; } -} \ No newline at end of file + static set(m, k, v) { m[k] = v; } + static clone(array) { + return Array.prototype.slice.call(array, 0); + } +} diff --git a/modules/facade/src/dom.dart b/modules/facade/src/dom.dart index 2ee48f555e..39c9907cc2 100644 --- a/modules/facade/src/dom.dart +++ b/modules/facade/src/dom.dart @@ -20,4 +20,7 @@ class DOM { static setText(Text text, String value) { text.text = value; } + static clone(Node node) { + return node.clone(true); + } } diff --git a/modules/facade/src/dom.es6 b/modules/facade/src/dom.es6 index b6d593bd6e..519c9dfee6 100644 --- a/modules/facade/src/dom.es6 +++ b/modules/facade/src/dom.es6 @@ -20,4 +20,12 @@ export class DOM { static setText(text:Text, value:String) { text.nodeValue = value; } + static createTemplate(html) { + var t = document.createElement('template'); + t.innerHTML = html; + return t; + } + static clone(node:Node) { + return node.cloneNode(true); + } } diff --git a/modules/rtts_assert/src/rtts_assert.es6 b/modules/rtts_assert/src/rtts_assert.es6 index da6ea51a64..c035a498ba 100644 --- a/modules/rtts_assert/src/rtts_assert.es6 +++ b/modules/rtts_assert/src/rtts_assert.es6 @@ -162,6 +162,7 @@ function type(actual, T) { throw new Error(msg); } + return actual; } function returnType(actual, T) { diff --git a/modules/rtts_assert/test/rtts_assert_spec.es6 b/modules/rtts_assert/test/rtts_assert_spec.es6 index 12be7cd645..368f380e2c 100644 --- a/modules/rtts_assert/test/rtts_assert_spec.es6 +++ b/modules/rtts_assert/test/rtts_assert_spec.es6 @@ -147,8 +147,8 @@ describe('primitive value check', function() { describe('boolean', function() { it('should pass', function() { - assert.type(true, primitive.boolean); - assert.type(false, primitive.boolean); + expect(assert.type(true, primitive.boolean)).toBe(true); + expect(assert.type(false, primitive.boolean)).toBe(false); });