微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

测试自定义验证指令

如何解决测试自定义验证指令

我有一个自定义验证,其目的是确保时间在给定的时间范围内。该指令用于输入,可以接收 minTime 值、maxTime 值或两者。

用法示例:

<p-inputMask
    mask='99:99:99?.999'
    placeholder="hh:mm:ss.SSS"
    ...
    myTimeRangeValidator
    maxTime="10:00:00"
    ...
></p-inputMask>

我想测试指令,所以我做了一个测试组件来测试它

@Component({
    template: `<input [(ngModel)]="value" myTimeRangeValidator [maxTime]="max" [minTime]="min">
})
class TestComponent {
    max: string;
    min: string;
    value: string;
}

我也开始创建测试框架(我不确定是否正确完成)。我不确定应该如何访问测试中的输入。

我的骨架:

describe('myTimeRangeValidator ',() => { 
    let component: TestComponent;
    let fixture: ComponentFixture<TestComponent>;
    let input; // type?

    beforeEach(
        waitForAsync(()=>{
            Testbed.configureTestingModule({
                declarations: [TestComponent]
            }).compileComponents();
        })
    );

    beforeEach(() => {
        fixture = Testbed.createComponent(TestComponent);
        component = fixture.componentInstance;
        input = ???
    })
})

理想情况下,我希望能够编写如下测试并使其工作:

it('should invalidate if time is over max',() => {
    component.max = '10:00:00';
    component.value = '20:00:00';

    fixture.detectChanges();
    
    expect(input.valid).toBeFalse();
});

总而言之,我的问题是如何定义/检索 input 以便我可以根据需要对其进行测试?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。