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

Unable to mock a signal in a property of a mocked class. #85

Open
jaytonic opened this issue Sep 9, 2024 · 0 comments
Open

Unable to mock a signal in a property of a mocked class. #85

jaytonic opened this issue Sep 9, 2024 · 0 comments

Comments

@jaytonic
Copy link

jaytonic commented Sep 9, 2024

Describe the bug
One dependency of a tested class has a getter of a signal. I need to mock it.
When following the guideline, it doesn't seems to compile because its a getter of a signal, which is a method.

To Reproduce
Steps to reproduce the behavior:

  1. Create a class with a signal property:
export class DemoClass {
  public get someSignal() {
    return signal(10);
  }
  public get someOtherSignal() {
    return computed(() => this.someSignal() * 2);
  }
}
  1. Create some tests for it, like described in the doc
import { computed, signal } from '@angular/core';
import { createSpyFromClass } from 'jest-auto-spies';

describe('permissions tests', () => {
  it('should compile when trying to mock signal', () => {
    const classSpy = createSpyFromClass(DemoClass, { gettersToSpyOn: ['someSignal'] });
    classSpy.accessorSpies.getters.someSignal.mockReturnValue(10);
  });
  it('should compile when trying to mock computed signal', () => {
    const classSpy = createSpyFromClass(DemoClass, { gettersToSpyOn: ['someOtherSignal'] });
    classSpy.accessorSpies.getters.someOtherSignal.mockReturnValue(20);
  });
});

Expected behavior
It should compiles and the signal should return the mocked value

Desktop (please complete the following information):

  • OS: [e.g. iOS] Windows 11 pro
  • Browser [e.g. chrome, safari]: Chrome(but not executed in browser)
  • Version [e.g. 22]: 128

Additional context
The whole point is to be able to mock signal and/or computed signals. Ideally, we should have something similar than for the observable properties.

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

1 participant