Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setInput doesn't respect the alias names #648

Closed
profanis opened this issue Mar 15, 2024 · 7 comments
Closed

setInput doesn't respect the alias names #648

profanis opened this issue Mar 15, 2024 · 7 comments

Comments

@profanis
Copy link
Contributor

Is this a regression?

Yes

Description

An alias name of the input is not a valid option in the setInput function. This causes the test to fail

@Component({
  selector: 'app-root',
  standalone: true,
  template: ``,
  styles: ``,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
  @Input({alias: 'isVisibleAlias'})
  isVisible: boolean = false;

  @Input('isEnabledAlias')
  isEnabled: boolean = false;
}
describe('AppComponent', () => {
  let spectator: Spectator<AppComponent>;
  const createComponent = createComponentFactory(AppComponent);

  it('respect the alias input names', () => {
    spectator = createComponent({
        props: {
          isVisible: true,
          isEnabled: true,
         // isVisibleAlias: true <-- compilation error
         // isEnabledAlias: true <-- compilation error
       }
    });

    const component = spectator.component;
    expect(component.isVisible).toBeTruthy(); // this test fails
    expect(component.isEnabled).toBeTruthy(); // this test fails
  });
});

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-vn8kfk?file=src%2Ftest.spec.ts

Please provide the exception or error you saw

The `setInput` should also accept the alias names

Please provide the environment you discovered this bug in

No response

Anything else?

No response

Do you want to create a pull request?

Yes

Copy link

stackblitz bot commented Mar 15, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@profanis
Copy link
Contributor Author

How about if we provide a more loose function overload?

https://github.com/ngneat/spectator/blob/master/projects/spectator/src/lib/spectator/spectator.ts#L44

  public setInput(input: string, inputValue: unknown): void;

I think something similar exists also in Angular repo
https://github.com/angular/angular/blob/da906fdafcbb302fa280a162d1c1f04369be2efa/packages/core/src/linker/component_factory.ts#L34

@profanis
Copy link
Contributor Author

How about if we provide a more loose function overload?

https://github.com/ngneat/spectator/blob/master/projects/spectator/src/lib/spectator/spectator.ts#L44

  public setInput(input: string, inputValue: unknown): void;

I think something similar exists also in Angular repo https://github.com/angular/angular/blob/da906fdafcbb302fa280a162d1c1f04369be2efa/packages/core/src/linker/component_factory.ts#L34

@kfrancois @NetanelBasal what do you think?

@kfrancois
Copy link
Contributor

kfrancois commented Mar 19, 2024

Hi @profanis! Thank you for logging this issue.

I think what you propose is the best possible option at the moment. Unfortunately, input aliases are defined as string inside the Angular repo:

interface InputOptions<ReadT, WriteT> {
    alias?: string;
    transform?: (v: WriteT) => ReadT;

If this were a generic type (or rather, if alias was generic instead of string), we would at least be able to infer the correct field name and use it for setInput (at least for signal inputs, traditional inputs we wouldn't be able to do anything), but for now it feels safer to lose type info here and go with input: string, inputValue: unknown.

@NetanelBasal
Copy link
Member

I trust you to reach a solution and open a PR

@NetanelBasal
Copy link
Member

Can we close it?

@profanis
Copy link
Contributor Author

Sorry for the delay.
This is the relevant PR #652

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants