Merge pull request #2215 from abpframework/test/form-submit-directive

Test/form submit directive
pull/2225/head
Mehmet Erim 6 years ago committed by GitHub
commit 7b91ec6019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,9 @@ type Controls = { [key: string]: FormControl } | FormGroup[];
selector: 'form[ngSubmit][formGroup]',
})
export class FormSubmitDirective implements OnInit, OnDestroy {
@Input()
debounce = 200;
@Input()
notValidateOnSubmit: string | boolean;
@ -42,7 +45,7 @@ export class FormSubmitDirective implements OnInit, OnDestroy {
fromEvent(this.host.nativeElement as HTMLElement, 'keyup')
.pipe(
debounceTime(200),
debounceTime(this.debounce),
filter((key: KeyboardEvent) => key && key.key === 'Enter'),
takeUntilDestroy(this),
)

@ -0,0 +1,46 @@
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator/jest';
import { FormSubmitDirective } from '../directives/form-submit.directive';
import { FormsModule, ReactiveFormsModule, FormGroup } from '@angular/forms';
import { timer } from 'rxjs';
describe('FormSubmitDirective', () => {
let spectator: SpectatorDirective<FormSubmitDirective>;
let directive: FormSubmitDirective;
const formGroup = new FormGroup({});
const submitEventFn = jest.fn(() => {});
const createDirective = createDirectiveFactory({
directive: FormSubmitDirective,
imports: [FormsModule, ReactiveFormsModule],
});
beforeEach(() => {
spectator = createDirective(
'<form [formGroup]="formGroup" (ngSubmit)="submitEventFn()" [debounce]="20">form content</form>',
{
hostProps: {
submitEventFn,
formGroup,
},
},
);
directive = spectator.directive;
});
test('should be created', () => {
expect(directive).toBeTruthy();
});
test('should have 20ms debounce time', () => {
expect(directive.debounce).toBe(20);
});
test('should dispatch submit event on keyup event triggered after given debounce time', done => {
spectator.dispatchKeyboardEvent('form', 'keyup', 'Enter');
timer(directive.debounce + 10).subscribe(() => {
expect(submitEventFn).toHaveBeenCalled();
done();
});
});
});
Loading…
Cancel
Save