lundi 24 juillet 2017

Angular 2 / 4 - How to test Directive @Input values?

So I have a Directive that takes an input:

@Directive({
    selector: '[my-directive]'
})
export class MyDirective {

    @Input('some-input')
    public someInput: string;
}

As you can see, the input should be a string value. Now, I want to write a test for this Directive, and I want to test if the input satisfies the string type:

describe('MyDirective', () => {

    let fixture: ComponentFixture<DummyComponent>;
    let dummy: DummyComponent;
    let directive: DebugElement;

    beforeEach(async(() => {

        TestBed
            .configureTestingModule({
                imports: [
                    MyModule.forRoot()
                ],
                declarations: [
                    DummyComponent
                ]
            })
            .compileComponents();

        fixture = TestBed.createComponent(DummyComponent);
        dummy = fixture.componentInstance;
        directive = fixture.debugElement.query(By.directive(MyDirective));

        fixture.detectChanges();
    }));

    it('should satisfy only a string input and error on other inputs', () => {
        // How to test?
    });
});

@Component({
    selector: 'dummy',
    template: `
        <div my-directive [some-input]="'just-a-string-value'"></div>
    `
})
export class DummyComponent implements OnInit {
}

How can I test whether the @Input value(s) are of the proper type?

Aucun commentaire:

Enregistrer un commentaire