angular/packages/common/test/directives/non_bindable_spec.ts
Jaime Burgos c6d7500203
test(common): remove zone-based testing utilities
Removes usages of zone-based helpers such as `fakeAsync` , `tick`
`waitForAsync` as part of the migration to zoneless tests.

Completes the transition to zoneless.
2026-02-09 14:47:35 -08:00

75 lines
2.2 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.dev/license
*/
import {Component, Directive, ElementRef} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {hasClass} from '@angular/private/testing';
import {expect} from '@angular/private/testing/matchers';
describe('non-bindable', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent, TestDirective],
});
});
it('should not interpolate children', () => {
const template = '<div>{{text}}<span ngNonBindable>{{text}}</span></div>';
const fixture = createTestComponent(template);
fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('foo{{text}}');
});
it('should ignore directives on child nodes', () => {
const template = '<div ngNonBindable><span id=child test-dec>{{text}}</span></div>';
const fixture = createTestComponent(template);
fixture.detectChanges();
// We must use getDOM().querySelector instead of fixture.query here
// since the elements inside are not compiled.
const span = fixture.nativeElement.querySelector('#child');
expect(hasClass(span, 'compiled')).toBeFalsy();
});
it('should trigger directives on the same node', () => {
const template = '<div><span id=child ngNonBindable test-dec>{{text}}</span></div>';
const fixture = createTestComponent(template);
fixture.detectChanges();
const span = fixture.nativeElement.querySelector('#child');
expect(hasClass(span, 'compiled')).toBeTruthy();
});
});
@Directive({
selector: '[test-dec]',
standalone: false,
})
class TestDirective {
constructor(el: ElementRef) {
el.nativeElement.classList.add('compiled');
}
}
@Component({
selector: 'test-cmp',
template: '',
standalone: false,
})
class TestComponent {
text: string;
constructor() {
this.text = 'foo';
}
}
function createTestComponent(template: string): ComponentFixture<TestComponent> {
return TestBed.overrideComponent(TestComponent, {set: {template: template}}).createComponent(
TestComponent,
);
}