Skip to content

Commit 6189945

Browse files
fix(ref: no-ref): Propagate masked value to form control value.
fix(forms): Propagate masked value to form control value.
2 parents 78a3771 + b3c218a commit 6189945

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
930930
/** It writes the value in the input */
931931
public async writeValue(controlValue: unknown): Promise<void> {
932932
let value = controlValue;
933-
const inputTransformFn = this.inputTransformFn();
933+
const inputTransformFn = this._maskService.inputTransformFn;
934934
if (typeof value === 'object' && value !== null && 'value' in value) {
935935
if ('disable' in value) {
936936
this.setDisabledState(Boolean(value.disable));

projects/ngx-mask-lib/src/lib/ngx-mask.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export class NgxMaskService extends NgxMaskApplierService {
266266
this.currentValue = result;
267267
this._emitValue =
268268
this.previousValue !== this.currentValue ||
269+
newInputValue !== this.currentValue ||
269270
this.maskChanged ||
270271
this.writingValue ||
271272
(this.previousValue === this.currentValue && justPasted);
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Component } from '@angular/core';
2+
import type { ComponentFixture } from '@angular/core/testing';
3+
import { TestBed } from '@angular/core/testing';
4+
import { FormControl, ReactiveFormsModule } from '@angular/forms';
5+
import { NgxMaskDirective, provideNgxMask } from 'ngx-mask';
6+
7+
@Component({
8+
selector: 'jsdaddy-open-source-test',
9+
imports: [ReactiveFormsModule, NgxMaskDirective],
10+
template: `<input mask="0000" [formControl]="form" />`,
11+
})
12+
class TestMaskComponent {
13+
public form: FormControl = new FormControl('');
14+
}
15+
16+
describe('Directive: Forms', () => {
17+
let fixture: ComponentFixture<TestMaskComponent>;
18+
let component: TestMaskComponent;
19+
20+
beforeEach(() => {
21+
TestBed.configureTestingModule({
22+
imports: [NgxMaskDirective],
23+
providers: [provideNgxMask()],
24+
});
25+
fixture = TestBed.createComponent(TestMaskComponent);
26+
component = fixture.componentInstance;
27+
fixture.detectChanges();
28+
});
29+
30+
it('should propagate masked value to the form control value', () => {
31+
component.form.setValue('A1234Z');
32+
expect(component.form.value).toBe('1234');
33+
});
34+
35+
it('should propagate masked value to the form control valueChanges observable', () => {
36+
component.form.valueChanges.subscribe((newValue) => {
37+
expect(newValue).toEqual('1234');
38+
});
39+
40+
component.form.setValue('A1234Z');
41+
});
42+
43+
it('should mask values when multiple calls to setValue() are made', () => {
44+
component.form.setValue('A1234Z');
45+
expect(component.form.value).toBe('1234');
46+
component.form.setValue('A1234Z');
47+
expect(component.form.value).toBe('1234');
48+
component.form.setValue('A1234Z');
49+
expect(component.form.value).toBe('1234');
50+
});
51+
52+
it('should propagate masked value to the form control valueChanges observable when multiple calls to setValue() are made', () => {
53+
component.form.valueChanges.subscribe((newValue) => {
54+
expect(newValue).toEqual('1234');
55+
});
56+
57+
component.form.setValue('A1234Z');
58+
component.form.setValue('A1234Z');
59+
component.form.setValue('A1234Z');
60+
});
61+
62+
it('should not emit to valueChanges if the masked value has not changed with emitEvent: true', () => {
63+
let emissionsToValueChanges = 0;
64+
65+
component.form.valueChanges.subscribe(() => {
66+
emissionsToValueChanges++;
67+
});
68+
69+
component.form.setValue('1234', { emitEvent: true });
70+
component.form.setValue('1234', { emitEvent: true });
71+
72+
// Expect to emit 3 times, once for the first setValue() call, once by ngx-mask, and once for the second setValue() call.
73+
// There is not fourth emission for when ngx-mask masks the value for a second time.
74+
expect(emissionsToValueChanges).toBe(3);
75+
});
76+
77+
it('should not emit to valueChanges if the masked value has not changed with emitEvent: false', () => {
78+
let emissionsToValueChanges = 0;
79+
80+
component.form.valueChanges.subscribe(() => {
81+
emissionsToValueChanges++;
82+
});
83+
84+
component.form.setValue('1234', { emitEvent: false });
85+
component.form.setValue('1234', { emitEvent: false });
86+
87+
// Expect to only have emitted once, only by ngx-mask.
88+
// There is no second emission for when ngx-mask masks the value for a second time.
89+
expect(emissionsToValueChanges).toBe(1);
90+
});
91+
});

0 commit comments

Comments
 (0)