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(kit): Number should drop decimal separator if all digits are erased #1211

Merged
merged 1 commit into from
Apr 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {maskitoNumberOptionsGenerator} from '@maskito/kit';

import {TestInput} from '../utils';

describe('Number | should drop decimal separator if all digits are erased', () => {
beforeEach(() => {
cy.mount(TestInput, {
componentProperties: {
maskitoOptions: maskitoNumberOptionsGenerator({
precision: 2,
minusSign: '-',
}),
},
});
cy.get('input').focus().should('have.value', '');
});

it('empty integer part & NOT empty decimal part => keeps decimal separator untouched', () => {
cy.get('input')
.type('0.12')
.type('{backspace}'.repeat(2))
.should('have.value', '0.');
});

it('NOT empty integer part & empty decimal part => keeps decimal separator untouched', () => {
cy.get('input')
.type('0.12')
.type('{moveToStart}')
.type('{del}')
.should('have.value', '.12');
});

describe('empty integer part & empty decimal part => drops decimal separator', () => {
[
{minusSign: '', testTitle: 'Without minus'},
{minusSign: '-', testTitle: 'With minus'},
].forEach(({testTitle, minusSign}) => {
it(testTitle, () => {
cy.get('input')
.type(`${minusSign}0.12`)
.type('{backspace}'.repeat(2))
.type(`{moveToStart}${'{rightArrow}'.repeat(minusSign.length)}`)
.type('{del}')
.should('have.value', minusSign)
// and then repeat everything in reversed order
.type('0.12')
.type(`{moveToStart}${'{rightArrow}'.repeat(minusSign.length)}`)
.type('{del}')
.type('{moveToEnd}')
.type('{backspace}'.repeat(2))
.should('have.value', minusSign);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ export function emptyPostprocessor({
cleanValue,
decimalSeparator,
);
const aloneDecimalSeparator =
!integerPart && !decimalPart && cleanValue.includes(decimalSeparator);

if (
!integerPart &&
!Number(decimalPart) &&
caretIndex === (minus + extractedPrefix).length
(!integerPart &&
!Number(decimalPart) &&
caretIndex === (minus + extractedPrefix).length) ||
aloneDecimalSeparator
) {
return {
selection,
Expand Down