Skip to content

Commit

Permalink
fix(core): overwriteMode: replace has incorrect behavior on attempt…
Browse files Browse the repository at this point in the history
… to insert invalid characters (#1772)
  • Loading branch information
nsbarsukov authored Oct 9, 2024
1 parent 3758b61 commit 5aeb074
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
3 changes: 1 addition & 2 deletions projects/core/src/lib/classes/mask-model/mask-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MaskitoOptions> = {
...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);
});
});
});
});

0 comments on commit 5aeb074

Please sign in to comment.