test(upgrade): run tests against multiple AngularJS versions (#22167)

Fixes #19332

PR Close #22167
This commit is contained in:
George Kalpakas
2018-02-15 19:21:18 +02:00
committed by Victor Berchet
parent 28240625e6
commit 8e1e040f72
17 changed files with 142 additions and 67 deletions

View File

@ -10,9 +10,9 @@ import {TestBed, getTestBed, inject} from '@angular/core/testing';
import * as angular from '@angular/upgrade/src/common/angular1';
import {DowngradeComponentAdapter, groupNodesBySelector} from '@angular/upgrade/src/common/downgrade_component_adapter';
import {nodes} from './test_helpers';
import {nodes, withEachNg1Version} from './test_helpers';
{
withEachNg1Version(() => {
describe('DowngradeComponentAdapter', () => {
describe('groupNodesBySelector', () => {
it('should return an array of node collections for each selector', () => {
@ -190,4 +190,4 @@ import {nodes} from './test_helpers';
});
});
}
});

View File

@ -5,6 +5,57 @@
* 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 {setAngularJSGlobal} from '@angular/upgrade/src/common/angular1';
const ng1Versions = [
{
label: '1.5',
files: ['angular-1.5/angular.js', 'angular-mocks-1.5/angular-mocks.js'],
},
{
label: '1.6',
files: ['angular/angular.js', 'angular-mocks/angular-mocks.js'],
},
];
export function createWithEachNg1VersionFn(setNg1: typeof setAngularJSGlobal) {
return (specSuite: () => void) => ng1Versions.forEach(({label, files}) => {
describe(`[AngularJS v${label}]`, () => {
beforeAll(done => {
// Load AngularJS before running tests.
files
.reduce(
(prev, file) => prev.then(() => new Promise<void>((resolve, reject) => {
const script = document.createElement('script');
script.src = `base/node_modules/${file}`;
script.onerror = reject;
script.onload = () => {
document.body.removeChild(script);
resolve();
};
document.body.appendChild(script);
})),
Promise.resolve())
.then(() => setNg1((window as any).angular))
.then(done, done.fail);
});
afterAll(() => {
// In these tests we are loading different versions of AngularJS on the same window.
// AngularJS leaves an "expandoId" property on `document`, which can trick subsequent
// `window.angular` instances into believing an app is already bootstrapped.
(window as any).angular.element(document).removeData();
// Remove AngularJS to leave a clean state for subsequent tests.
setNg1(undefined);
delete (window as any).angular;
});
specSuite();
});
});
}
export function html(html: string): Element {
// Don't return `body` itself, because using it as a `$rootElement` for ng1
@ -33,3 +84,5 @@ export function nodes(html: string) {
div.innerHTML = html.trim();
return Array.prototype.slice.call(div.childNodes);
}
export const withEachNg1Version = createWithEachNg1VersionFn(setAngularJSGlobal);