From 60f837e995917d01275bc86ee12858da9dc65a2f Mon Sep 17 00:00:00 2001 From: thediaval Date: Tue, 19 Nov 2019 16:20:35 +0300 Subject: [PATCH 1/3] improvement(core): add debounce time input --- .../core/src/lib/directives/form-submit.directive.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/directives/form-submit.directive.ts b/npm/ng-packs/packages/core/src/lib/directives/form-submit.directive.ts index 5d0c6ef7a1..4d6c1014db 100644 --- a/npm/ng-packs/packages/core/src/lib/directives/form-submit.directive.ts +++ b/npm/ng-packs/packages/core/src/lib/directives/form-submit.directive.ts @@ -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), ) From 75e7746a84b16bb86341654a9783868da3ed895b Mon Sep 17 00:00:00 2001 From: thediaval Date: Tue, 19 Nov 2019 16:21:02 +0300 Subject: [PATCH 2/3] test(core): add missing form-submit directive test --- .../lib/tests/form-submit.directive.spec.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts diff --git a/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts new file mode 100644 index 0000000000..a1858cc34b --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts @@ -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; + let directive: FormSubmitDirective; + + let formGroup = new FormGroup({}); + const submitEventFn = jest.fn(() => {}); + + const createDirective = createDirectiveFactory({ + directive: FormSubmitDirective, + imports: [FormsModule, ReactiveFormsModule], + }); + + beforeEach(() => { + spectator = createDirective( + '
form content
', + { + 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 + 1).subscribe(() => { + expect(submitEventFn).toHaveBeenCalled(); + done(); + }); + }); +}); From 89c30a615d6c5d6ba1456eb1613a9d29e7469416 Mon Sep 17 00:00:00 2001 From: thediaval Date: Tue, 19 Nov 2019 17:17:43 +0300 Subject: [PATCH 3/3] test(core): change debounce time --- .../packages/core/src/lib/tests/form-submit.directive.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts index a1858cc34b..094a41d4a6 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/form-submit.directive.spec.ts @@ -7,7 +7,7 @@ describe('FormSubmitDirective', () => { let spectator: SpectatorDirective; let directive: FormSubmitDirective; - let formGroup = new FormGroup({}); + const formGroup = new FormGroup({}); const submitEventFn = jest.fn(() => {}); const createDirective = createDirectiveFactory({ @@ -38,7 +38,7 @@ describe('FormSubmitDirective', () => { test('should dispatch submit event on keyup event triggered after given debounce time', done => { spectator.dispatchKeyboardEvent('form', 'keyup', 'Enter'); - timer(directive.debounce + 1).subscribe(() => { + timer(directive.debounce + 10).subscribe(() => { expect(submitEventFn).toHaveBeenCalled(); done(); });