admin管理员组文章数量:1202390
I recently discovered the HostAttributeToken
in Angular (see docs) to provide static attributes for a component.
The following example works fine:
import { Component, HostAttributeToken, inject } from '@angular/core';
@Component({
selector: 'demo-component',
template: `<div [class]="styleClass">Hello world</div>`,
})
export class DemoComponent {
styleClass = inject(new HostAttributeToken('style-class'));
}
usage
<demo-component style-class="bla" />
My question: How can provide the HostAttributeToken
in a test with jasmine/karma?
I want to make this attribute required (there is also a flag to make the HostAttributeToken
optional).
It is of course easy if there is a host component, but how can I achieve the same thing with the TestBed
Api?
A basic test setup without providing an HostAttributeToken
would look like this:
import { TestBed } from '@angular/core/testing';
import { DemoComponent } from './demoponent';
describe('DemoComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [DemoComponent],
})pileComponents();
});
it('should create', () => {
const fixture = TestBed.createComponent(DemoComponent);
expect(fixtureponentInstance).toBeTruthy();
});
});
which errors with
Error: NG0201: No provider for HostAttributeToken style-class found. Find more at /errors/NG0201
I recently discovered the HostAttributeToken
in Angular (see docs) to provide static attributes for a component.
The following example works fine:
import { Component, HostAttributeToken, inject } from '@angular/core';
@Component({
selector: 'demo-component',
template: `<div [class]="styleClass">Hello world</div>`,
})
export class DemoComponent {
styleClass = inject(new HostAttributeToken('style-class'));
}
usage
<demo-component style-class="bla" />
My question: How can provide the HostAttributeToken
in a test with jasmine/karma?
I want to make this attribute required (there is also a flag to make the HostAttributeToken
optional).
It is of course easy if there is a host component, but how can I achieve the same thing with the TestBed
Api?
A basic test setup without providing an HostAttributeToken
would look like this:
import { TestBed } from '@angular/core/testing';
import { DemoComponent } from './demo.component';
describe('DemoComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [DemoComponent],
}).compileComponents();
});
it('should create', () => {
const fixture = TestBed.createComponent(DemoComponent);
expect(fixture.componentInstance).toBeTruthy();
});
});
which errors with
Share Improve this question asked Jan 21 at 7:53 kerosenekerosene 97316 silver badges37 bronze badges 1Error: NG0201: No provider for HostAttributeToken style-class found. Find more at https://angular.dev/errors/NG0201
- reference stackblitz – Naren Murali Commented Jan 21 at 8:20
1 Answer
Reset to default 1When you have that kind of question, it is often interesting how the feature itself is unit-testing inside the angular repo.
Here is the relevant test file.
Long story short, there is no other way than having a fake host component to test that feature.
// The directive you're testing
@Directive({selector: '[dir]', standalone: true})
class Dir {
value = inject(new HostAttributeToken('some-attr'));
}
// The "fake" host component
@Component({
standalone: true,
template: '<div dir some-attr="foo" other="ignore"></div>',
imports: [Dir],
})
class TestCmp {
@ViewChild(Dir) dir!: Dir;
}
const fixture = TestBed.createComponent(TestCmp);
fixture.detectChanges();
expect(fixture.componentInstance.dir.value).toBe('foo');
本文标签: htmlHow to provide a HostAttributeToken in Angular testsStack Overflow
版权声明:本文标题:html - How to provide a HostAttributeToken in Angular tests? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738647506a2104675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论