Karma unit tests (typescript/angular)

Asked

Viewed 312 times

0

I am trying to perform a unit test for a method made in typescript, this is the method:

createTasksForms(): void {
    this.task.forEach(task => {
        task.form = this.form.group({ name: ['', [Validators.required, Validators.pattern(/^[^ ]/)]] });
        task.form.controls.name.valueChanges.subscribe(value => { value; });
        task.canEdit = false;
    });
}

I was able to test it until the subscribe (code coverage), but it does not enter the (value => { value; }), my test is like this for now:

 it('should create tasks forms', async () => {
    component.task = task;
    await component.createTasksForms();
    component.task = { form: {controls: { name: { valueChanges: { } } } } };
    expect(component.task).toBeDefined();
  });

Come some solution?

  • 1

    That’s very strange your code, Component. task should be an array to be traversed by for each task element, each task element has a form property like Formgroup. That your subscribe code now does nothing. What the point of it??

  • its function createTasksForms returns nothing pq vc put a await in it?

  • a tip would be to use map instead of foreach, in javascript and functional programming in general mutation is something very bad

  • I noticed, I was reviewing my code and it doesn’t really make sense this part of it, I think it was something after we readapted some functions. Vlw :D

1 answer

0

If your task is an array of type task would be something like this:

it('should create tasks forms', async () => {
    component.task = [...task];
    component.createTasksForms();
    expect(component.task[0].form).toBeDefined();
    expect(component.task[0].canEdit).toBeFalsy();
  });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.