angular/packages/elements/test/create-custom-element-env_spec.ts
Kristiyan Kostadinov 513a4fe05e refactor(core): replace usages of removeChild (#57203)
These changes replace most usages of `removeChild` with `remove`. The latter has the advantage of not having to look up the `parentNode` and ensure that the child being removed actually belongs to the specific parent.

The refactor should be fairly safe since all the browsers we cover support `remove`. [Something similar was done in Components](https://github.com/angular/components/pull/23592) some time ago and there haven't been any bug reports as a result.

PR Close #57203
2024-08-07 16:46:09 +00:00

48 lines
1.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC 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} from '@angular/core';
import {createApplication} from '@angular/platform-browser';
import {createCustomElement} from '../public_api';
describe('createCustomElement with env injector', () => {
let testContainer: HTMLDivElement;
beforeEach(() => {
testContainer = document.createElement('div');
document.body.appendChild(testContainer);
});
afterEach(() => {
testContainer.remove();
(testContainer as any) = null;
});
it('should use provided EnvironmentInjector to create a custom element', async () => {
@Component({
standalone: true,
template: `Hello, standalone element!`,
})
class TestStandaloneCmp {}
const appRef = await createApplication();
try {
const NgElementCtor = createCustomElement(TestStandaloneCmp, {injector: appRef.injector});
customElements.define('test-standalone-cmp', NgElementCtor);
const customEl = document.createElement('test-standalone-cmp');
testContainer.appendChild(customEl);
expect(testContainer.innerText).toBe('Hello, standalone element!');
} finally {
appRef.destroy();
}
});
});