Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(1358): add trailing zero when mask="separator.1" and leadZero="true" #1359

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 17.0.8(2024-04-30)

### Fix

- Fix ([#1358](https://github.com/JsDaddy/ngx-mask/issues/1358))

# 17.0.7(2024-03-28)

### Fix
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-mask",
"version": "17.0.7",
"version": "17.0.8",
"description": "awesome ngx mask",
"keywords": [
"ng2-mask",
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/src/lib/ngx-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export class NgxMaskDirective implements ControlValueAccessor, OnChanges, Valida
maskExpression.length
)
);
if (precision > 1) {
if (precision > 0) {
el.value = this.suffix ? el.value.split(this.suffix).join('') : el.value;
const decimalPart = el.value.split(this.decimalMarker)[1] as string;
el.value = el.value.includes(this.decimalMarker)
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mask-lib/src/lib/ngx-mask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ export class NgxMaskService extends NgxMaskApplierService {
const separatorPrecision = separatorExpression.slice(10, 11);
if (
separatorExpression.indexOf('2') > 0 ||
(this.leadZero && Number(separatorPrecision) > 1)
(this.leadZero && Number(separatorPrecision) > 0)
) {
if (this.decimalMarker === MaskExpression.COMMA && this.leadZero) {
// eslint-disable-next-line no-param-reassign
Expand Down
24 changes: 24 additions & 0 deletions projects/ngx-mask-lib/src/test/emit-events.cy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ describe('Directive: Mask (emit-events)', () => {
cy.get('#pre').should('have.text', '7');
});

it('should add trailing zero when mask="separator.1" and leadZero="true"', () => {
cy.mount(CypressTestMaskComponent, {
componentProperties: {
mask: 'separator.1',
leadZero: true,
},
imports: [CypressTestMaskModule],
});

cy.get('#masked').type('9').blur().should('have.value', '9.0');
});

it('should keep trailing decimal when mask="separator.1" and leadZero="true"', () => {
cy.mount(CypressTestMaskComponent, {
componentProperties: {
mask: 'separator.1',
leadZero: true,
},
imports: [CypressTestMaskModule],
});

cy.get('#masked').type('7.7').blur().should('have.value', '7.7');
});

it('should emit event only when mask is correct with suffix separator2', () => {
cy.mount(CypressTestMaskComponent, {
componentProperties: {
Expand Down
51 changes: 51 additions & 0 deletions projects/ngx-mask-lib/src/test/separator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,57 @@ describe('Separator: Mask', () => {
equal('0@', '0', fixture);
});

it('should add trailing zero when separator.1 and leadZero = true', fakeAsync(() => {
component.mask = 'separator.1';
component.leadZero = true;
const debugElement: DebugElement = fixture.debugElement.query(By.css('input'));
const inputTarget: HTMLInputElement = debugElement.nativeElement as HTMLInputElement;
spyOnProperty(document, 'activeElement').and.returnValue(inputTarget);
fixture.detectChanges();

component.form.setValue(0);
tick();
expect(inputTarget.value).toBe('0.0');

component.form.setValue(1);
tick();
expect(inputTarget.value).toBe('1.0');

component.form.setValue(88);
tick();
expect(inputTarget.value).toBe('88.0');

component.form.setValue(99.);
tick();
expect(inputTarget.value).toBe('99.0');
}));

it('should not modify value with one decimal when separator.1 and leadZero = true', fakeAsync(() => {
component.mask = 'separator.1';
component.leadZero = true;
const debugElement: DebugElement = fixture.debugElement.query(By.css('input'));
const inputTarget: HTMLInputElement = debugElement.nativeElement as HTMLInputElement;
spyOnProperty(document, 'activeElement').and.returnValue(inputTarget);
fixture.detectChanges();

component.form.setValue(0.0);
tick();
expect(inputTarget.value).toBe('0.0');

component.form.setValue(1.0);
tick();
expect(inputTarget.value).toBe('1.0');

component.form.setValue(88.0);
tick();
expect(inputTarget.value).toBe('88.0');

component.form.setValue(99.9);
tick();
expect(inputTarget.value).toBe('99.9');
}));


it('should display zeros at the end separator2', fakeAsync(() => {
component.mask = 'separator.2';
component.leadZero = true;
Expand Down