8000 MockComponent function set Signal Inputs as Decorator Inputs · Issue #8887 · help-me-mom/ng-mocks · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

MockComponent function set Signal Inputs as Decorator Inputs #8887

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

Open
henriquecustodia opened this issue May 3, 2024 · 3 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@henriquecustodia
Copy link

I've noticed that Signal Inputs are just treated as Decorator Input.

ng-mocks@14.12.2

I have the following component:

@Component({
  selector: 'app-list-item',
  standalone: true,
  templateUrl: './list-item.component.html',
  styleUrl: './list-item.component.scss',
})
export class ListItemComponent { 
  task = input.required<Task>();
}

I mock the component using MockComponent function:

 await TestBed.configureTestingModule({
      imports: [ListComponent],
      providers: [
        {
          provide: TasksService,
          useValue: tasksServiceStub,
        },
      ],
    })
      .overrideComponent(ListComponent, {
        remove: {
          imports: [ListItemComponent],
        },
        add: {
          imports: [MockComponent(ListItemComponent)], // I mock here
        },
      })
      .compileComponents();

And my test try to access the Singal Input from ListItemComponent to assert its value:

 expect((<ListItemComponent>item.componentInstance).task()).toEqual(completedTasksStub[index]);

But the test breaks because the task property (it's a Signal Input) is not a function.

The complete error:
TypeError: completedItemDebugEl.componentInstance.task is not a function

Logging the <ListItemComponent>item.componentInstance).task in the test, I see that it's a property

task: { id: '1', name: 'Buy milk', completed: true }

Working with Signal Inputs, we need to test it as a Signal and not as a property as it was with Decorator Inputs.

Any news about that @satanTime?

Thank for your amazing work btw

@henriquecustodia henriquecustodia added the bug Something isn't working label May 3, 2024
@henriquecustodia henriquecustodia changed the title Bug: MockComponents set Signal Inputs as Decorator Inputs May 3, 2024
@henriquecustodia henriquecustodia changed the title MockComponents set Signal Inputs as Decorator Inputs MockComponent function set Signal Inputs as Decorator Inputs May 3, 2024
@gvlekke
Copy link 8000
gvlekke commented Aug 6, 2024

any news if this will be picked up?

@teowgh
Copy link
teowgh commented Oct 22, 2024

Anyone working on it?

@solovevserg
Copy link

I created a mapped type as a temporary workaround to handle this issue:

export type WithSignalsUnwrapped<TComponent extends object> = {
    [P in keyof TComponent]: TComponent[P] extends Signal<infer T> ? T : TComponent[P];
};

It doesn't cast inputs to signals in a mocked component, but aligns types with the actual mock behaviour. In your case it can be used like this:

 expect(
    (<WithSignalsUnwrapped<ListItemComponent>>item.componentInstance).task()
).toEqual(completedTasksStub[index]);

P.S. I believe the issue's auhor initial request requires discussion.

Working with Signal Inputs, we need to test it as a Signal and not as a property as it was with Decorator Inputs.

input() vs @Input is a child component implementation detail. From the parent's perspective, the contract (component interface) hasn't changed. I believe the child's input type shouldn't affect parent component tests. Migration to signals should remain transparent to component consumers, including test code. Coupling a mock's behaviour to internal implementation details leads to unexpected test failures during migration to signals and is generally considered a bad practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants
0