jeudi 7 juillet 2016

angular2 junit test compoent

I have a component that receives input by a user . in ngOnInit of this component , I call a service to get information about that user

export class UserComponent implements OnInit {
   @Input()
   user: string;

   dataUser : Array....
   ngOnInit() {
                  this._service.getInfoUser(user)
        .subscribe(data => {
            dataUser = data ;
        },
        error => {
            // handle error


        }
}

i try to write a junit test for this component

 it('should update text', inject([], () => {
    return builder.createAsync(UserComponent )
        .then((fixture) => {

            let user = new User();
            user = '0192';
            fixture.componentInstance.user = user;
            fixture.detectChanges();
                                                                          expect(fixture.componentInstance.dataUser.length).toBe(2);
            let user2 = new User();
            user.inss = '6901';
            fixture.componentInstance.user = user2;
            fixture.detectChanges();
            expect(fixture.componentInstance..length).toBe(1);
        });

At the fist detectChanges , that works fine (ngOnInit méthod is call) but not the second time Thus dataUser has the date filled at first call

Aucun commentaire:

Enregistrer un commentaire