Skip to content

Commit d1e53b0

Browse files
committed
wip: test fixes?
1 parent 4eb737b commit d1e53b0

File tree

7 files changed

+41
-3
lines changed

7 files changed

+41
-3
lines changed

goldens/public-api/core/index.api.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,21 @@ export interface SelfDecorator {
17671767
// @public
17681768
export function setTestabilityGetter(getter: GetTestability): void;
17691769

1770+
// @public
1771+
export const SHARED_STYLES_HOST: InjectionToken<SharedStylesHost>;
1772+
1773+
// @public
1774+
export interface SharedStylesHost {
1775+
// (undocumented)
1776+
addHost(hostNode: Node): void;
1777+
// (undocumented)
1778+
addStyles(styles: string[]): void;
1779+
// (undocumented)
1780+
removeHost(hostNode: Node): void;
1781+
// (undocumented)
1782+
removeStyles(styles: string[]): void;
1783+
}
1784+
17701785
// @public
17711786
export type Signal<T> = (() => T) & {
17721787
[SIGNAL]: unknown;

packages/core/src/render3/component_ref.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ export class ComponentRef<T> extends AbstractComponentRef<T> {
566566
}
567567

568568
override destroy(): void {
569+
if (this.hostView.destroyed) {
570+
return;
571+
}
569572
// Verify if the host element is referenced in the SharedStylesHost and remove it if so.
570573
const rootNode = this.location.nativeElement.getRootNode?.();
571574
const sharedStylesHost = this._rootLView[INJECTOR].get(SHARED_STYLES_HOST);

packages/core/src/render3/hmr.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
getInitialLViewFlagsFromDef,
4545
getOrCreateComponentTView,
4646
} from './view/construction';
47+
import {SHARED_STYLES_HOST} from '../core';
4748

4849
/** Represents `import.meta` plus some information that's not in the built-in types. */
4950
type ImportMetaExtended = ImportMeta & {
@@ -249,9 +250,19 @@ function recreateLView(
249250
oldDef.encapsulation === ViewEncapsulation.ShadowDom ||
250251
oldDef.encapsulation === ViewEncapsulation.ExperimentalIsolatedShadowDom
251252
) {
253+
const sharedStylesHost = lView[INJECTOR].get(SHARED_STYLES_HOST);
254+
if (oldDef.encapsulation === ViewEncapsulation.ShadowDom) {
255+
// Only legacy shadow DOM uses SSH.
256+
sharedStylesHost.removeHost(host.shadowRoot!);
257+
}
258+
252259
const newHost = host.cloneNode(false) as HTMLElement;
253260
host.replaceWith(newHost);
254261
host = newHost;
262+
263+
// if (oldDef.encapsulation === ViewEncapsulation.ShadowDom) {
264+
// sharedStylesHost.addHost(host.shadowRoot!);
265+
// }
255266
}
256267

257268
// Recreate the TView since the template might've changed.

packages/core/test/acceptance/hmr_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('hot module replacement', () => {
251251
);
252252
});
253253

254-
it('should replace a component using shadow DOM encapsulation', () => {
254+
fit('should replace a component using shadow DOM encapsulation', () => {
255255
// Domino doesn't support shadow DOM.
256256
if (isNode) {
257257
return;

packages/core/test/acceptance/view_container_ref_spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {
5050
ɵsetDocument,
5151
} from '../../src/core';
5252
import {ComponentFixture, TestBed, TestComponentRenderer} from '../../testing';
53-
import {SharedStylesHost} from '@angular/platform-browser/src/dom/shared_styles_host';
53+
import {ɵSharedStylesHost as SharedStylesHost} from '@angular/platform-browser';
5454

5555
describe('ViewContainerRef', () => {
5656
/**
@@ -1412,7 +1412,8 @@ describe('ViewContainerRef', () => {
14121412
{provide: ErrorHandler, useValue: TestBed.inject(ErrorHandler)},
14131413
{provide: RendererFactory2, useValue: TestBed.inject(RendererFactory2)},
14141414
{provide: ANIMATION_QUEUE, useValue: TestBed.inject(ANIMATION_QUEUE)},
1415-
{provide: SHARED_STYLES_HOST, useExisting: SharedStylesHost},
1415+
{provide: SHARED_STYLES_HOST, useValue: TestBed.inject(SHARED_STYLES_HOST)},
1416+
{provide: DOCUMENT, useValue: TestBed.inject(DOCUMENT)},
14161417
],
14171418
})
14181419
class MyAppModule {}

packages/core/test/test_bed_spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ import {TestBed, TestBedImpl} from '../testing/src/test_bed';
5757
import {NgModuleType} from '../src/render3';
5858
import {depsTracker} from '../src/render3/deps_tracker/deps_tracker';
5959
import {setClassMetadataAsync} from '../src/render3/metadata';
60+
import {ɵSharedStylesHost as SharedStylesHost} from '@angular/platform-browser';
61+
import {SHARED_STYLES_HOST} from '../src/render3/interfaces/shared_styles_host';
62+
import {APP_ID} from '../src/core';
6063
import {
6164
ComponentFixtureAutoDetect,
6265
TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT,
@@ -2700,6 +2703,10 @@ describe('TestBed module teardown', () => {
27002703
TestBed.configureTestingModule({
27012704
declarations: [StyledComp1, StyledComp2],
27022705
teardown: {destroyAfterEach: true},
2706+
// providers: [
2707+
// {provide: SHARED_STYLES_HOST, useClass: SharedStylesHost},
2708+
// {provide: APP_ID, useValue: 'ng'},
2709+
// ],
27032710
});
27042711

27052712
const fixtures = [TestBed.createComponent(StyledComp1), TestBed.createComponent(StyledComp2)];

packages/platform-browser/src/dom/shared_styles_host.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export class SharedStylesHost implements OnDestroy {
204204
for (const [, {elements}] of [...this.inline, ...this.external]) {
205205
removeElements(elements);
206206
}
207+
// TODO: Seems to be getting called too early? Why is the timing changing at all?
207208
this.hosts.clear();
208209
}
209210

0 commit comments

Comments
 (0)