From 14177f537e5e25288245ac4744343a2ec21c53e7 Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:20:13 +0200 Subject: [PATCH] docs: reproduce 397 --- ...irective-overrides-component-input.spec.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 projects/testing-library/tests/issues/issue-397-directive-overrides-component-input.spec.ts diff --git a/projects/testing-library/tests/issues/issue-397-directive-overrides-component-input.spec.ts b/projects/testing-library/tests/issues/issue-397-directive-overrides-component-input.spec.ts new file mode 100644 index 0000000..c2a02a8 --- /dev/null +++ b/projects/testing-library/tests/issues/issue-397-directive-overrides-component-input.spec.ts @@ -0,0 +1,67 @@ +import { Component, Directive, Input, OnInit } from '@angular/core'; +import { render, screen } from '../../src/public_api'; + +test('the value set in the directive constructor is overriden by the input binding', async () => { + await render(``, { + imports: [FixtureComponent, InputOverrideViaConstructorDirective], + }); + + expect(screen.getByText('set by test')).toBeInTheDocument(); +}); + +test('the value set in the directive onInit is used instead of the input binding', async () => { + await render(``, { + imports: [FixtureComponent, InputOverrideViaOnInitDirective], + }); + + expect(screen.getByText('set by directive ngOnInit')).toBeInTheDocument(); +}); + +test('the value set in the directive constructor is used instead of the input value', async () => { + await render(``, { + imports: [FixtureComponent, InputOverrideViaConstructorDirective], + }); + + expect(screen.getByText('set by directive constructor')).toBeInTheDocument(); +}); + +test('the value set in the directive ngOnInit is used instead of the input value and the directive constructor', async () => { + await render(``, { + imports: [FixtureComponent, InputOverrideViaConstructorDirective, InputOverrideViaOnInitDirective], + }); + + expect(screen.getByText('set by directive ngOnInit')).toBeInTheDocument(); +}); + +@Component({ + standalone: true, + selector: 'atl-fixture', + template: `{{ input }}`, +}) +class FixtureComponent { + @Input() public input = 'default value'; +} + +@Directive({ + // eslint-disable-next-line @angular-eslint/directive-selector + selector: 'atl-fixture', + standalone: true, +}) +class InputOverrideViaConstructorDirective { + constructor(private fixture: FixtureComponent) { + this.fixture.input = 'set by directive constructor'; + } +} + +@Directive({ + // eslint-disable-next-line @angular-eslint/directive-selector + selector: 'atl-fixture', + standalone: true, +}) +class InputOverrideViaOnInitDirective implements OnInit { + constructor(private fixture: FixtureComponent) {} + + ngOnInit(): void { + this.fixture.input = 'set by directive ngOnInit'; + } +}