I'm new with Angular test and I have a problem.
The first problem is with the variable username into a component that I use to show logged username into HTML page:
This is the test file:
describe('HeaderComponent', () => {
let component: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;
let app;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HeaderComponent, DialogListConfigurationComponent, DialogConfirmNewConfigurationComponent ],
providers: [ MessageService, JwtHelperService, AuthService ],
imports: [ DialogModule, MenubarModule, FontAwesomeModule, RouterTestingModule, TreeModule, HttpClientTestingModule, JwtModule.forRoot({
config: {
tokenGetter: () => {
return sessionStorage.getItem(environment.tokenName);
},
//Exclude this URL from JWT (doesn't add the authentication header)
blacklistedRoutes: [
'/api/login',
]
}
}), ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
The component is this:
export class HeaderComponent implements OnInit {
@Output() sidebarEvent = new EventEmitter();
@Output() clickOkButtonListConfigurationsEvent = new EventEmitter();
username: String = this.authService.decodeToken.username;
items = [] as MenuItem[];
dialogListConfigurationVisible: boolean = false;
faUserCircle = faUserCircle;
defaultConfigurations: Configuration[];
configurationCreated: boolean = false;
dialogConfirmCreationVisible: boolean = false;
constructor(private configurationService: ConfigurationService, private authService: AuthService) { }
ngOnInit() {
...
}
The auth service method:
get decodeToken(): Jwt {
return this.jwtHelper.decodeToken(this.getSessionToken);
}
The should create test fails with TypeError: Cannot read property 'username' of null