|
| 1 | +import { Component, Directive, Input, OnInit } from '@angular/core'; |
| 2 | +import { render, screen } from '../../src/public_api'; |
| 3 | + |
| 4 | +test('the value set in the directive constructor is overriden by the input binding', async () => { |
| 5 | + await render(`<atl-fixture [input]="'set by test'" />`, { |
| 6 | + imports: [FixtureComponent, InputOverrideViaConstructorDirective], |
| 7 | + }); |
| 8 | + |
| 9 | + expect(screen.getByText('set by test')).toBeInTheDocument(); |
| 10 | +}); |
| 11 | + |
| 12 | +test('the value set in the directive onInit is used instead of the input binding', async () => { |
| 13 | + await render(`<atl-fixture [input]="'set by test'" />`, { |
| 14 | + imports: [FixtureComponent, InputOverrideViaOnInitDirective], |
| 15 | + }); |
| 16 | + |
| 17 | + expect(screen.getByText('set by directive ngOnInit')).toBeInTheDocument(); |
| 18 | +}); |
| 19 | + |
| 20 | +test('the value set in the directive constructor is used instead of the input value', async () => { |
| 21 | + await render(`<atl-fixture input='set by test' />`, { |
| 22 | + imports: [FixtureComponent, InputOverrideViaConstructorDirective], |
| 23 | + }); |
| 24 | + |
| 25 | + expect(screen.getByText('set by directive constructor')).toBeInTheDocument(); |
| 26 | +}); |
| 27 | + |
| 28 | +test('the value set in the directive ngOnInit is used instead of the input value and the directive constructor', async () => { |
| 29 | + await render(`<atl-fixture input='set by test' />`, { |
| 30 | + imports: [FixtureComponent, InputOverrideViaConstructorDirective, InputOverrideViaOnInitDirective], |
| 31 | + }); |
| 32 | + |
| 33 | + expect(screen.getByText('set by directive ngOnInit')).toBeInTheDocument(); |
| 34 | +}); |
| 35 | + |
| 36 | +@Component({ |
| 37 | + standalone: true, |
| 38 | + selector: 'atl-fixture', |
| 39 | + template: `{{ input }}`, |
| 40 | +}) |
| 41 | +class FixtureComponent { |
| 42 | + @Input() public input = 'default value'; |
| 43 | +} |
| 44 | + |
| 45 | +@Directive({ |
| 46 | + // eslint-disable-next-line @angular-eslint/directive-selector |
| 47 | + selector: 'atl-fixture', |
| 48 | + standalone: true, |
| 49 | +}) |
| 50 | +class InputOverrideViaConstructorDirective { |
| 51 | + constructor(private fixture: FixtureComponent) { |
| 52 | + this.fixture.input = 'set by directive constructor'; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +@Directive({ |
| 57 | + // eslint-disable-next-line @angular-eslint/directive-selector |
| 58 | + selector: 'atl-fixture', |
| 59 | + standalone: true, |
| 60 | +}) |
| 61 | +class InputOverrideViaOnInitDirective implements OnInit { |
| 62 | + constructor(private fixture: FixtureComponent) {} |
| 63 | + |
| 64 | + ngOnInit(): void { |
| 65 | + this.fixture.input = 'set by directive ngOnInit'; |
| 66 | + } |
| 67 | +} |
0 commit comments