-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(elements): add a test for standalone element creation (#46475)
This commit adds a test demonstrating how a standalone Angular component could be turned into a custom element. PR Close #46475
- Loading branch information
1 parent
4b377d3
commit 9285c66
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @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 {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; | ||
|
||
import {createCustomElement} from '../public_api'; | ||
|
||
if (browserDetection.supportsCustomElements) { | ||
describe('createCustomElement with env injector', () => { | ||
let testContainer: HTMLDivElement; | ||
|
||
beforeEach(() => { | ||
testContainer = document.createElement('div'); | ||
document.body.appendChild(testContainer); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(testContainer); | ||
(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(); | ||
} | ||
}); | ||
}); | ||
} |