From 8ce0a67c8123e39ff0e72574d91fead401425c08 Mon Sep 17 00:00:00 2001 From: Ian Riley Date: Wed, 27 May 2015 13:48:24 -0700 Subject: [PATCH] refactor (test/services): Ts'ifying test/services Translates AtScript files in test/services to TypeScript Closes #2193 --- .../{rectangle_mock.es6 => rectangle_mock.ts} | 0 modules/angular2/test/services/ruler_spec.js | 68 ----------------- modules/angular2/test/services/ruler_spec.ts | 74 +++++++++++++++++++ .../services/{title_spec.js => title_spec.ts} | 10 +-- ..._resolver_spec.js => url_resolver_spec.ts} | 35 +++++---- .../angular2/test/services/xhr_impl_spec.js | 43 ----------- .../angular2/test/services/xhr_impl_spec.ts | 39 ++++++++++ tools/broccoli/trees/node_tree.ts | 2 +- 8 files changed, 138 insertions(+), 133 deletions(-) rename modules/angular2/test/services/{rectangle_mock.es6 => rectangle_mock.ts} (100%) delete mode 100644 modules/angular2/test/services/ruler_spec.js create mode 100644 modules/angular2/test/services/ruler_spec.ts rename modules/angular2/test/services/{title_spec.js => title_spec.ts} (79%) rename modules/angular2/test/services/{url_resolver_spec.js => url_resolver_spec.ts} (67%) delete mode 100644 modules/angular2/test/services/xhr_impl_spec.js create mode 100644 modules/angular2/test/services/xhr_impl_spec.ts diff --git a/modules/angular2/test/services/rectangle_mock.es6 b/modules/angular2/test/services/rectangle_mock.ts similarity index 100% rename from modules/angular2/test/services/rectangle_mock.es6 rename to modules/angular2/test/services/rectangle_mock.ts diff --git a/modules/angular2/test/services/ruler_spec.js b/modules/angular2/test/services/ruler_spec.js deleted file mode 100644 index a67d84d98c..0000000000 --- a/modules/angular2/test/services/ruler_spec.js +++ /dev/null @@ -1,68 +0,0 @@ -import { - AsyncTestCompleter, inject, ddescribe, describe, it, iit, xit, expect, SpyObject, - proxy -} from 'angular2/test_lib'; - -import {DOM, DomAdapter} from 'angular2/src/dom/dom_adapter'; -import {ElementRef} from 'angular2/src/core/compiler/element_ref'; - -import {Ruler, Rectangle} from 'angular2/src/services/ruler'; -import {createRectangle} from './rectangle_mock'; -import {IMPLEMENTS} from 'angular2/src/facade/lang'; - -function assertDimensions(rect: Rectangle, left, right, top, bottom, width, height) { - expect(rect.left).toEqual(left); - expect(rect.right).toEqual(right); - expect(rect.top).toEqual(top); - expect(rect.bottom).toEqual(bottom); - expect(rect.width).toEqual(width); - expect(rect.height).toEqual(height); -} - -export function main() { - - describe('ruler service', () => { - - it('should allow measuring ElementRefs', - inject([AsyncTestCompleter], (async) => { - var ruler = new Ruler(SpyObject.stub(new SpyDomAdapter(), { - 'getBoundingClientRect': createRectangle(10, 20, 200, 100) - })); - - var elRef = new SpyElementRef(); - ruler.measure(elRef).then((rect) => { - assertDimensions(rect, 10, 210, 20, 120, 200, 100); - async.done(); - }); - })); - - - it('should return 0 for all rectangle values while measuring elements in a document fragment', - inject([AsyncTestCompleter], (async) => { - var ruler = new Ruler(DOM); - var elRef = new SpyElementRef(); - elRef.domElement = DOM.createElement('div'); - ruler.measure(elRef).then((rect) => { - //here we are using an element created in a doc fragment so all the measures will come back as 0 - assertDimensions(rect, 0, 0, 0, 0, 0, 0); - async.done(); - }); - })); - - }); -} - -@proxy -@IMPLEMENTS(ElementRef) -class SpyElementRef extends SpyObject { - domElement; - constructor(){super(ElementRef);} - noSuchMethod(m){return super.noSuchMethod(m)} -} - -@proxy -@IMPLEMENTS(DomAdapter) -class SpyDomAdapter extends SpyObject { - constructor(){super(DomAdapter);} - noSuchMethod(m){return super.noSuchMethod(m)} -} diff --git a/modules/angular2/test/services/ruler_spec.ts b/modules/angular2/test/services/ruler_spec.ts new file mode 100644 index 0000000000..8aa7366851 --- /dev/null +++ b/modules/angular2/test/services/ruler_spec.ts @@ -0,0 +1,74 @@ +import { + AsyncTestCompleter, + inject, + ddescribe, + describe, + it, + iit, + xit, + expect, + SpyObject, + proxy +} from 'angular2/test_lib'; + +import {DOM, DomAdapter} from 'angular2/src/dom/dom_adapter'; +import {ElementRef} from 'angular2/src/core/compiler/element_ref'; + +import {Ruler, Rectangle} from 'angular2/src/services/ruler'; +import {createRectangle} from './rectangle_mock'; +import {IMPLEMENTS} from 'angular2/src/facade/lang'; + +function assertDimensions(rect: Rectangle, left, right, top, bottom, width, height) { + expect(rect.left).toEqual(left); + expect(rect.right).toEqual(right); + expect(rect.top).toEqual(top); + expect(rect.bottom).toEqual(bottom); + expect(rect.width).toEqual(width); + expect(rect.height).toEqual(height); +} + +export function main() { + describe('ruler service', () => { + + it('should allow measuring ElementRefs', inject([AsyncTestCompleter], (async) => { + var ruler = new Ruler(SpyObject.stub( + new SpyDomAdapter(), {'getBoundingClientRect': createRectangle(10, 20, 200, 100)})); + + var elRef = new SpyElementRef(); + ruler.measure(elRef).then((rect) => { + assertDimensions(rect, 10, 210, 20, 120, 200, 100); + async.done(); + }); + })); + + + it('should return 0 for all rectangle values while measuring elements in a document fragment', + inject([AsyncTestCompleter], (async) => { + var ruler = new Ruler(DOM); + var elRef = new SpyElementRef(); + elRef.domElement = DOM.createElement('div'); + ruler.measure(elRef).then((rect) => { + // here we are using an element created in a doc fragment so all the measures will come + // back as 0 + assertDimensions(rect, 0, 0, 0, 0, 0, 0); + async.done(); + }); + })); + + }); +} + +@proxy +@IMPLEMENTS(ElementRef) +class SpyElementRef extends SpyObject { + domElement; + constructor() { super(ElementRef); } + noSuchMethod(m) { return super.noSuchMethod(m) } +} + +@proxy +@IMPLEMENTS(DomAdapter) +class SpyDomAdapter extends SpyObject { + constructor() { super(DomAdapter); } + noSuchMethod(m) { return super.noSuchMethod(m) } +} diff --git a/modules/angular2/test/services/title_spec.js b/modules/angular2/test/services/title_spec.ts similarity index 79% rename from modules/angular2/test/services/title_spec.js rename to modules/angular2/test/services/title_spec.ts index ba2397be46..0d5b84a017 100644 --- a/modules/angular2/test/services/title_spec.js +++ b/modules/angular2/test/services/title_spec.ts @@ -4,18 +4,14 @@ import {DOM} from 'angular2/src/dom/dom_adapter'; import {Title} from 'angular2/src/services/title'; export function main() { - describe('title service', () => { var initialTitle = DOM.getTitle(); var titleService = new Title(); - afterEach(() => { - DOM.setTitle(initialTitle); - }); + afterEach(() => { DOM.setTitle(initialTitle); }); - it('should allow reading initial title', () => { - expect(titleService.getTitle()).toEqual(initialTitle); - }); + it('should allow reading initial title', + () => { expect(titleService.getTitle()).toEqual(initialTitle); }); it('should set a title on the injected document', () => { titleService.setTitle('test title'); diff --git a/modules/angular2/test/services/url_resolver_spec.js b/modules/angular2/test/services/url_resolver_spec.ts similarity index 67% rename from modules/angular2/test/services/url_resolver_spec.js rename to modules/angular2/test/services/url_resolver_spec.ts index a8c10f1b9d..314b0606d1 100644 --- a/modules/angular2/test/services/url_resolver_spec.js +++ b/modules/angular2/test/services/url_resolver_spec.ts @@ -18,25 +18,32 @@ export function main() { }); it('should append to the base path', () => { - expect(resolver.resolve('http://www.foo.com/baz/', 'bar')).toEqual('http://www.foo.com/baz/bar'); - expect(resolver.resolve('http://www.foo.com/baz/', './bar')).toEqual('http://www.foo.com/baz/bar'); + expect(resolver.resolve('http://www.foo.com/baz/', 'bar')) + .toEqual('http://www.foo.com/baz/bar'); + expect(resolver.resolve('http://www.foo.com/baz/', './bar')) + .toEqual('http://www.foo.com/baz/bar'); }); it('should support ".." in the path', () => { - expect(resolver.resolve('http://www.foo.com/baz/', '../bar')).toEqual('http://www.foo.com/bar'); - expect(resolver.resolve('http://www.foo.com/1/2/3/', '../../bar')).toEqual('http://www.foo.com/1/bar'); - expect(resolver.resolve('http://www.foo.com/1/2/3/', '../biz/bar')).toEqual('http://www.foo.com/1/2/biz/bar'); - expect(resolver.resolve('http://www.foo.com/1/2/baz', '../../bar')).toEqual('http://www.foo.com/bar'); + expect(resolver.resolve('http://www.foo.com/baz/', '../bar')) + .toEqual('http://www.foo.com/bar'); + expect(resolver.resolve('http://www.foo.com/1/2/3/', '../../bar')) + .toEqual('http://www.foo.com/1/bar'); + expect(resolver.resolve('http://www.foo.com/1/2/3/', '../biz/bar')) + .toEqual('http://www.foo.com/1/2/biz/bar'); + expect(resolver.resolve('http://www.foo.com/1/2/baz', '../../bar')) + .toEqual('http://www.foo.com/bar'); }); - it('should ignore the base path when the url has a scheme', () => { - expect(resolver.resolve('http://www.foo.com', 'http://www.bar.com')).toEqual('http://www.bar.com'); - }) + it('should ignore the base path when the url has a scheme', + () => + { + expect(resolver.resolve('http://www.foo.com', 'http://www.bar.com')) + .toEqual('http://www.bar.com'); + }) - it('should throw when the url start with "/"', () => { - expect(() => { - resolver.resolve('http://www.foo.com/1/2', '/test'); - }).toThrowError(); - }); + it('should throw when the url start with "/"', () => { + expect(() => { resolver.resolve('http://www.foo.com/1/2', '/test'); }).toThrowError(); + }); }); } diff --git a/modules/angular2/test/services/xhr_impl_spec.js b/modules/angular2/test/services/xhr_impl_spec.js deleted file mode 100644 index f0abb58527..0000000000 --- a/modules/angular2/test/services/xhr_impl_spec.js +++ /dev/null @@ -1,43 +0,0 @@ -import { - AsyncTestCompleter, - beforeEach, - ddescribe, - describe, - expect, - iit, - inject, - it, - xit -} from 'angular2/test_lib'; - -import {XHRImpl} from 'angular2/src/services/xhr_impl'; -import {PromiseWrapper} from 'angular2/src/facade/async'; - -export function main() { - describe('XHRImpl', () => { - var xhr; - var url200 = '/base/modules/angular2/test/services/static_assets/200.html'; - var url404 = '/base/modules/angular2/test/services/static_assets/404.html'; - - beforeEach(() => { - xhr = new XHRImpl(); - }); - - it('should resolve the Promise with the file content on success', inject([AsyncTestCompleter], (async) => { - xhr.get(url200).then((text) => { - expect(text.trim()).toEqual('

hey

'); - async.done(); - }); - })); - - it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => { - PromiseWrapper.catchError( - xhr.get(url404), - (e) => { - expect(e).toEqual(`Failed to load ${url404}`); - async.done(); - } - ); - })); - }); -} diff --git a/modules/angular2/test/services/xhr_impl_spec.ts b/modules/angular2/test/services/xhr_impl_spec.ts new file mode 100644 index 0000000000..a7908afdf5 --- /dev/null +++ b/modules/angular2/test/services/xhr_impl_spec.ts @@ -0,0 +1,39 @@ +import { + AsyncTestCompleter, + beforeEach, + ddescribe, + describe, + expect, + iit, + inject, + it, + xit +} from 'angular2/test_lib'; + +import {XHRImpl} from 'angular2/src/services/xhr_impl'; +import {PromiseWrapper} from 'angular2/src/facade/async'; + +export function main() { + describe('XHRImpl', () => { + var xhr; + var url200 = '/base/modules/angular2/test/services/static_assets/200.html'; + var url404 = '/base/modules/angular2/test/services/static_assets/404.html'; + + beforeEach(() => { xhr = new XHRImpl(); }); + + it('should resolve the Promise with the file content on success', + inject([AsyncTestCompleter], (async) => { + xhr.get(url200).then((text) => { + expect(text.trim()).toEqual('

hey

'); + async.done(); + }); + })); + + it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => { + PromiseWrapper.catchError(xhr.get(url404), (e) => { + expect(e).toEqual(`Failed to load ${url404}`); + async.done(); + }); + })); + }); +} diff --git a/tools/broccoli/trees/node_tree.ts b/tools/broccoli/trees/node_tree.ts index d9356a98f4..0b6601f9b8 100644 --- a/tools/broccoli/trees/node_tree.ts +++ b/tools/broccoli/trees/node_tree.ts @@ -23,7 +23,7 @@ module.exports = function makeNodeTree(destinationPath) { // the following code and tests are not compatible with CJS/node environment 'angular2/test/core/zone/**', 'angular2/test/test_lib/fake_async_spec.js', - 'angular2/test/services/xhr_impl_spec.js' + 'angular2/test/services/xhr_impl_spec.ts' ] });