Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions adev/src/content/guide/testing/attribute-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,35 @@ A few techniques are noteworthy:
The test for the default color uses the injector of the second `<h2>` to get its `Highlight` instance and its `defaultColor`.

- `DebugElement.properties` affords access to the artificial custom property that is set by the directive

## Testing a directive in isolation

A directive can't be constructed through TestBed; it must be rendered through a component's template to behave correctly.
The `Highlight` directive can be tested this way, using a local test component's input to control the directive.

```ts
@Component({
imports: [Highlight],
template: `<p [highlight]="color()">{{ color() }}</p>`,
})
class Test {
readonly color = input('');
}

describe('Highlight', () => {
let fixture: ComponentFixture<Test>;

beforeEach(async () => {
fixture = TestBed.createComponent(Test);
await fixture.whenStable();
});

it('should use the specified color once an input is provided', async () => {
fixture.componentRef.setInput('color', 'blue');
await fixture.whenStable();

const p = fixture.nativeElement.querySelector('p');
expect(p.style.backgroundColor).toBe('blue');
});
});
```
Loading