测试自定义验证指令

问题描述

我有一个自定义验证,其目的是确保时间在给定的时间范围内。该指令用于输入,可以接收 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 (将#修改为@)