diff --git a/modules/@angular/examples/upgrade/static/ts/e2e_test/static_spec.ts b/modules/@angular/examples/upgrade/static/ts/e2e_test/static_spec.ts new file mode 100644 index 0000000000..ed780162e6 --- /dev/null +++ b/modules/@angular/examples/upgrade/static/ts/e2e_test/static_spec.ts @@ -0,0 +1,54 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {browser, by, element} from 'protractor'; +import {verifyNoBrowserErrors} from '../../../../_common/e2e_util'; + +function loadPage(url: string) { + browser.ng12Hybrid = true; + browser.rootEl = 'example-app'; + browser.get(url); +} + +describe('upgrade(static)', () => { + beforeEach(() => { loadPage('/upgrade/static/ts/'); }); + afterEach(verifyNoBrowserErrors); + + it('should render the `ng2-heroes` component', () => { + expect(element(by.css('h1')).getText()).toEqual('Heroes'); + expect(element.all(by.css('p')).get(0).getText()).toEqual('There are 3 heroes.'); + }); + + it('should render 3 ng1-hero components', () => { + const heroComponents = element.all(by.css('ng1-hero')); + expect(heroComponents.count()).toEqual(3); + }); + + it('should add a new hero when the "Add Hero" button is pressed', () => { + const addHeroButton = element.all(by.css('button')).last(); + expect(addHeroButton.getText()).toEqual('Add Hero'); + addHeroButton.click(); + const heroComponents = element.all(by.css('ng1-hero')); + expect(heroComponents.last().element(by.css('h2')).getText()).toEqual('Kamala Khan'); + }); + + it('should remove a hero when the "Remove" button is pressed', () => { + let firstHero = element.all(by.css('ng1-hero')).get(0); + expect(firstHero.element(by.css('h2')).getText()).toEqual('Superman'); + + const removeHeroButton = firstHero.element(by.css('button')); + expect(removeHeroButton.getText()).toEqual('Remove'); + removeHeroButton.click(); + + const heroComponents = element.all(by.css('ng1-hero')); + expect(heroComponents.count()).toEqual(2); + + firstHero = element.all(by.css('ng1-hero')).get(0); + expect(firstHero.element(by.css('h2')).getText()).toEqual('Wonder Woman'); + }); +}); \ No newline at end of file diff --git a/modules/@angular/examples/upgrade/static/ts/module.ts b/modules/@angular/examples/upgrade/static/ts/module.ts new file mode 100644 index 0000000000..20dd59871e --- /dev/null +++ b/modules/@angular/examples/upgrade/static/ts/module.ts @@ -0,0 +1,184 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ +import {Component, Directive, DoCheck, ElementRef, EventEmitter, Inject, Injectable, Injector, Input, NgModule, OnChanges, OnDestroy, OnInit, Output, SimpleChanges} from '@angular/core'; +import {BrowserModule} from '@angular/platform-browser'; +import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; +import {UpgradeComponent, UpgradeModule, downgradeComponent, downgradeInjectable} from '@angular/upgrade/static'; + +interface Hero { + name: string; + description: string; +} + +// #docregion Angular 2 Stuff +// #docregion ng2-heroes +// This Angular 2 component will be "downgraded" to be used in Angular 1 +@Component({ + selector: 'ng2-heroes', + // This template uses the upgraded `ng1-hero` component + // Note that because its element is compiled by Angular 2+ we must use camelCased attribute names + template: `
{{ $ctrl.hero.description }}
+ ` +}); +// #enddocregion + +// #docregion ng1-title-case-service +// This Angular 1 service will be "upgraded" to be used in Angular 2+ +ng1AppModule.factory( + 'titleCase', + () => (value: string) => value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase())); +// #enddocregion + +// #docregion downgrade-ng2-heroes-service +// Register an Angular 1 service, whose value is the "downgraded" Angular 2+ injectable. +ng1AppModule.factory('heroesService', downgradeInjectable(HeroesService)); +// #enddocregion + +// #docregion ng2-heroes-wrapper +// This is directive will act as the interface to the "downgraded" Angular 2+ component +ng1AppModule.directive( + 'ng2Heroes', + downgradeComponent( + // The inputs and outputs here must match the relevant names of the properties on the + // "downgraded" component + {component: Ng2HeroesComponent, inputs: ['heroes'], outputs: ['addHero', 'removeHero']})); +// #enddocregion + +// #docregion example-app +// This is our top level application component +ng1AppModule.component('exampleApp', { + // We inject the "downgraded" HeroesService into this Angular 1 component + // (We don't need the `HeroesService` type for Angular 1 DI - it just helps with TypeScript + // compilation) + controller: [ + 'heroesService', function(heroesService: HeroesService) { this.heroesService = heroesService; } + ], + // This template make use of the downgraded `ng2-heroes` component + // Note that because its element is compiled by Angular 1 we must use kebab-case attributes for + // inputs and outputs + template: ` +