mardi 20 juin 2017

Angular 4 - karma testing

I am trying to test routing. I have a function which checks if I was logged in, which is given as statement in the Login/Logout button hidden attribute.

<a [hidden]="!setLoggedOut()" routerLink="/login" 
(click)="setIsItLogin()">Login</a>
            <a [hidden]="setLoggedOut()" routerLink="/login" 
(click)="clearSessionStorage()">Logout</a>



setLoggedOut = () => {
    let loggedOut = true;
    const currentUser = sessionStorage.Status;
    if (currentUser === 'ok') {
        loggedOut = false;
    }
    return loggedOut;
 }

During the testing I recieve the following error message: TypeError: compo.setLOggedOut is not a function

This is the test component:

import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { CanActivate, Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { async, inject, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { Http, ConnectionBackend, RequestOptions } from '@angular/http';

import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { RoutingService } from './routing.service';
import { AppComponent } from './app.component';

class requestoptions {
    public requestoption: RequestOptions
}

@Component ({
    templateUrl: './app.component.html'
})
class RoutingComponent {  }

describe('component: RoutingComponent', () => {
    let location, routing, compo;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                FormsModule,
                RouterTestingModule.withRoutes([
                    { path: 'login', component: LoginComponent },
                    { path: '', component: HomeComponent, canActivate: 
[RoutingService] }
                ])
            ],
            declarations: [
                AppComponent,
                RoutingComponent,
                HomeComponent,
                LoginComponent
            ], 
            providers: [
                 RoutingService,
                 {provide: Http, useClass: requestoptions},
                 ConnectionBackend
            ]
        });
    });

    beforeEach(inject([Router, Location], (_router: Router, _location: 
Location) => {
        location = _location;
        routing = _router;
    }));

    it('should go home', async(() => {
        const fixture = TestBed.createComponent(RoutingComponent);
        fixture.detectChanges();
        routing.navigate(['']).then(() => {
            expect(location.path()).toBe('/');
            console.log('after expect');
        });
    }));


    it('should go to the login page', async(() => {
        const fixture = TestBed.createComponent(RoutingComponent);
        fixture.detectChanges();
        routing.navigate(['login']).then(() => {
            expect(location.path()).toBe('/login');
            console.log('after expect');
        });
    }));
});

Aucun commentaire:

Enregistrer un commentaire