Hello! I'm a newcomer to Angular testing and I've hit a roadblock. The issue lies with the username
variable within a component that is responsible for displaying the logged-in username on an HTML page.
Take a look at the test file below:
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);
},
blacklistedRoutes: [
'/api/login',
]
}
}), ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
The relevant component code is as follows:
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() {
...
}
You can find the auth service method here:
get decodeToken(): Jwt {
return this.jwtHelper.decodeToken(this.getSessionToken);
}
The current issue causing the should create
test to fail is TypeError: Cannot read property 'username' of null
. Any ideas?