From 5aeb0741fa82ad6e43e862059a17b2e78ee9831b Mon Sep 17 00:00:00 2001 From: Nikita Barsukov Date: Wed, 9 Oct 2024 15:23:16 +0300 Subject: [PATCH] fix(core): `overwriteMode: replace` has incorrect behavior on attempt to insert invalid characters (#1772) --- .../src/lib/classes/mask-model/mask-model.ts | 3 +- .../tests/mask-model-fixed-characters.spec.ts | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/projects/core/src/lib/classes/mask-model/mask-model.ts b/projects/core/src/lib/classes/mask-model/mask-model.ts index 9ee6fae84..805b50151 100644 --- a/projects/core/src/lib/classes/mask-model/mask-model.ts +++ b/projects/core/src/lib/classes/mask-model/mask-model.ts @@ -56,8 +56,7 @@ export class MaskModel implements ElementState { initialElementState, ); const isInvalidCharsInsertion = - // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with - value.slice(0, unmaskedFrom) === + value.slice(0, from) === calibrateValueByMask( { value: newUnmaskedLeadingValuePart, diff --git a/projects/core/src/lib/classes/mask-model/tests/mask-model-fixed-characters.spec.ts b/projects/core/src/lib/classes/mask-model/tests/mask-model-fixed-characters.spec.ts index 58f10e661..e951a3beb 100644 --- a/projects/core/src/lib/classes/mask-model/tests/mask-model-fixed-characters.spec.ts +++ b/projects/core/src/lib/classes/mask-model/tests/mask-model-fixed-characters.spec.ts @@ -110,4 +110,44 @@ describe('MaskModel | Fixed characters', () => { }); }); }); + + describe('Attempt to insert invalid characters for `overwriteMode: replace`', () => { + describe('mask expression contains leading characters – ["$", /d/, /d/]', () => { + const options: Required = { + ...MASKITO_DEFAULT_OPTIONS, + mask: ['$', /\d/, /\d/], + overwriteMode: 'replace', + }; + + it('$1|2 => Type A => $1|2', () => { + const value = '$12'; + const selection = [2, 2] as const; + const maskModel = new MaskModel({value, selection}, options); + + expect(() => maskModel.addCharacters(selection, 'q')).toThrow(); + expect(maskModel.value).toBe(value); + expect(maskModel.selection).toEqual(selection); + }); + + it('$|12 => Type $ => $|12', () => { + const value = '$12'; + const selection = [1, 1] as const; + const maskModel = new MaskModel({value, selection}, options); + + expect(() => maskModel.addCharacters(selection, '$')).toThrow(); + expect(maskModel.value).toBe(value); + expect(maskModel.selection).toEqual(selection); + }); + + it('$|12 => Type X => $|12', () => { + const value = '$12'; + const selection = [1, 1] as const; + const maskModel = new MaskModel({value, selection}, options); + + expect(() => maskModel.addCharacters(selection, 'X')).toThrow(); + expect(maskModel.value).toBe(value); + expect(maskModel.selection).toEqual(selection); + }); + }); + }); });