Hi guys, so I'm trying to write tests with Angular signals and can't find any examples in internet.
If I mock todosSig with Jest then obviously it's not a signal anymore and computed is not working on it.
So noTodosClass doesn't update it's value.
Another way is to create signals inside test and check them but I'm not sure if it's a good idea.
Any links to source code with testing signals is appreciated.
// todos.service.ts
export class TodosService {
todosSig = signal<TodoInterface[]>([]);
}
// footer.component.ts
export class FooterComponent {
todosService = inject(TodosService);
noTodosClass = computed(() => this.todosService.todosSig().length === 0);
}
// footer.component.spec.ts
describe('FooterComponent', () => {
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
const todosServiceMock = {
todosSig: jest.fn().mockReturnValue([])
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FooterComponent],
providers: [{ provide: TodosService, useValue: todosServiceMock }],
}).compileComponents();
fixture = TestBed.createComponent(FooterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be visible with todos', () => {
todosServiceMock.todosSig.mockReturnValue([
{ id: 1, text: 'Sample Todo', isCompleted: false },
]);
fixture.detectChanges();
const footer = fixture.debugElement.query(
By.css('[data-testid="footer"]')
);
expect(footer.classes['hidden']).not.toBeDefined();
});
})