Skip to content

Commit

Permalink
Prevent option change event firing when changes occur in component (#712
Browse files Browse the repository at this point in the history
) (#750)

* Prevent option change event firing when changes occur in component

* Update event lockers

* Fix typo

* Add test for bound options

* Simplify lockers

* Refactor test

* Prevent option change event firing when nested option was changed

* Move event locker to writeValue method

* Rework test

* Fix typo
  • Loading branch information
dxvladislavvolkov committed Apr 13, 2018
1 parent d8b49f5 commit 850bcd7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/events-strategy.ts
Expand Up @@ -65,10 +65,14 @@ export class NgEventsStrategy {
}

export class EmitterHelper {
lockedValueChangeEvent = false;

constructor(private component: DxComponent) { }

fireNgEvent(eventName: string, eventArgs: any) {
if (this.lockedValueChangeEvent && eventName === 'valueChange') {
return;
}
let emitter = this.component[eventName];
if (emitter) {
emitter.next(eventArgs && eventArgs[0]);
Expand Down
2 changes: 2 additions & 0 deletions templates/component.tst
Expand Up @@ -149,7 +149,9 @@ export class <#= it.className #>Component extends <#= baseClass #> <#? implement
}
<#? it.isEditor #>
writeValue(value: any): void {
this.eventHelper.lockedValueChangeEvent = true;
this.value = value;
this.eventHelper.lockedValueChangeEvent = false;
}
<#? it.widgetName !== "dxRangeSelector" #>
setDisabledState(isDisabled: boolean): void {
Expand Down
21 changes: 21 additions & 0 deletions tests/src/ui/custom-value-accessor-implementation.spec.ts
Expand Up @@ -78,6 +78,7 @@ describe('DxTextBox value accessor', () => {

expect(instance.option('disabled')).toBe(false);
}));

it('should change the value', async(() => {
let fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();
Expand All @@ -89,6 +90,7 @@ describe('DxTextBox value accessor', () => {

expect(instance.option('value')).toBe('text');
}));

it('should change touched option', async(() => {
let fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();
Expand All @@ -102,4 +104,23 @@ describe('DxTextBox value accessor', () => {

expect(fixture.componentInstance.formControl.touched).toBe(true);
}));

it('should not fire valueChanges event when patchValue method is used with emitEvent=false (T614207)', () => {
let fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();

let component = fixture.componentInstance,
form = component.form,
testSpy = jasmine.createSpy('testSpy');

form.valueChanges.subscribe(testSpy);

form.controls['formControl'].patchValue('text', { emitEvent: false });
fixture.detectChanges();
expect(testSpy).toHaveBeenCalledTimes(0);

form.controls['formControl'].patchValue('text2');
fixture.detectChanges();
expect(testSpy).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 850bcd7

Please sign in to comment.