2016-10-19 16:41:04 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-10-19 16:41:04 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
import {Component, destroyPlatform, Directive, ElementRef, Injector, Input, NgModule} from '@angular/core';
|
2020-07-31 15:43:18 -04:00
|
|
|
import {waitForAsync} from '@angular/core/testing';
|
2016-10-19 16:41:04 -04:00
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {downgradeComponent, UpgradeComponent, UpgradeModule} from '@angular/upgrade/static';
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2019-03-22 05:42:52 -04:00
|
|
|
import * as angular from '../../../src/common/src/angular1';
|
|
|
|
import {html, multiTrim, withEachNg1Version} from '../../../src/common/test/helpers/common_test_helpers';
|
|
|
|
|
|
|
|
import {bootstrap} from './static_test_helpers';
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-02-15 12:21:18 -05:00
|
|
|
withEachNg1Version(() => {
|
2016-10-19 16:41:04 -04:00
|
|
|
describe('examples', () => {
|
|
|
|
beforeEach(() => destroyPlatform());
|
|
|
|
afterEach(() => destroyPlatform());
|
|
|
|
|
2019-10-04 13:44:11 -04:00
|
|
|
it('should have AngularJS loaded',
|
|
|
|
() => expect(angular.getAngularJSGlobal().version.major).toBe(1));
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2020-07-31 15:43:18 -04:00
|
|
|
it('should verify UpgradeAdapter example', waitForAsync(() => {
|
2018-12-22 11:02:34 -05:00
|
|
|
// This is wrapping (upgrading) an AngularJS component to be used in an Angular
|
|
|
|
// component
|
|
|
|
@Directive({selector: 'ng1'})
|
|
|
|
class Ng1Component extends UpgradeComponent {
|
|
|
|
// TODO(issue/24571): remove '!'.
|
2020-04-13 19:40:21 -04:00
|
|
|
@Input() title!: string;
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-12-22 11:02:34 -05:00
|
|
|
constructor(elementRef: ElementRef, injector: Injector) {
|
|
|
|
super('ng1', elementRef, injector);
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-12-22 11:02:34 -05:00
|
|
|
// This is an Angular component that will be downgraded
|
|
|
|
@Component({
|
|
|
|
selector: 'ng2',
|
|
|
|
template: 'ng2[<ng1 [title]="nameProp">transclude</ng1>](<ng-content></ng-content>)'
|
|
|
|
})
|
|
|
|
class Ng2Component {
|
|
|
|
// TODO(issue/24571): remove '!'.
|
2020-04-13 19:40:21 -04:00
|
|
|
@Input('name') nameProp!: string;
|
2018-12-22 11:02:34 -05:00
|
|
|
}
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-12-22 11:02:34 -05:00
|
|
|
// This module represents the Angular pieces of the application
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Ng1Component, Ng2Component],
|
|
|
|
entryComponents: [Ng2Component],
|
|
|
|
imports: [BrowserModule, UpgradeModule]
|
|
|
|
})
|
|
|
|
class Ng2Module {
|
|
|
|
ngDoBootstrap() { /* this is a placeholder to stop the bootstrapper from
|
|
|
|
complaining */
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-12-22 11:02:34 -05:00
|
|
|
// This module represents the AngularJS pieces of the application
|
|
|
|
const ng1Module =
|
|
|
|
angular
|
2019-04-23 11:00:04 -04:00
|
|
|
.module_('myExample', [])
|
2018-12-22 11:02:34 -05:00
|
|
|
// This is an AngularJS component that will be upgraded
|
|
|
|
.directive(
|
|
|
|
'ng1',
|
|
|
|
() => {
|
|
|
|
return {
|
|
|
|
scope: {title: '='},
|
|
|
|
transclude: true,
|
|
|
|
template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
|
|
|
|
};
|
|
|
|
})
|
|
|
|
// This is wrapping (downgrading) an Angular component to be used in
|
|
|
|
// AngularJS
|
|
|
|
.directive('ng2', downgradeComponent({component: Ng2Component}));
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-12-22 11:02:34 -05:00
|
|
|
// This is the (AngularJS) application bootstrap element
|
|
|
|
// Notice that it is actually a downgraded Angular component
|
|
|
|
const element = html('<ng2 name="World">project</ng2>');
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-12-22 11:02:34 -05:00
|
|
|
// Let's use a helper function to make this simpler
|
|
|
|
bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then(upgrade => {
|
|
|
|
expect(multiTrim(element.textContent))
|
|
|
|
.toBe('ng2[ng1[Hello World!](transclude)](project)');
|
|
|
|
});
|
|
|
|
}));
|
2016-10-19 16:41:04 -04:00
|
|
|
});
|
2018-02-15 12:21:18 -05:00
|
|
|
});
|