I have trouble with unit test in Angular 2+. And now I am trying the smaller example (similar to my component) to learn how to test a input checkbox with ngmodel. But it does not run.
Here my smaller example:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-foobar',
template: `
<label>
<input type="checkbox" [ngModel]="foo" />
Checkbox
</label>
`,
styleUrls: []
})
export class FooBarComponent implements OnInit {
private _foo: boolean;
public get foo(): boolean {
return this._foo;
}
ngOnInit() {
this._foo = true;
}
}
And the unit test:
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FooBarComponent} from "./foobar.component";
import {FormsModule} from "@angular/forms";
describe('FooBarComponent', () => {
let component: FooBarComponent;
let fixture: ComponentFixture<FooBarComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [FooBarComponent],
imports: [FormsModule,]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(FooBarComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should start with the checkbox checked', () => {
const checkboxElement: HTMLInputElement = fixture.nativeElement.querySelector('input[type="checkbox"]');
expect(checkboxElement.checked).toBe(true);
});
});
And the component rendered by karma in the browser has the checkbox unchecked, but when this smaller example is rendered in real app, it shows the checkbox checked:
I don't know what I need to do to make it runs.
