Skip to content

Commit

Permalink
πŸ”€ Merge pull request #71 from codermarcos/fix/#70/problem-with-space-…
Browse files Browse the repository at this point in the history
…in-prefix

πŸ› Fix problem with space in prefix
  • Loading branch information
codermarcos authored Mar 6, 2024
2 parents 2c61dab + fb40f7c commit 029af4d
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 75 deletions.
229 changes: 158 additions & 71 deletions cypress/e2e/v4.x.x/set-mask.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,80 +257,167 @@ describe(
describe(
'prefix and suffix',
() => {
beforeEach(
() => {
cy.visit(getUrl({ prefix: '$', suffix: 'CAD' }));
cy.reload();
}
);

it(
'should format when input was created',
() => {
cy.get('input').should('have.value', '$0,00CAD');
}
);

it(
'shouldn\'t allow type a letter',
() => {
cy.get('input').type('abc');
cy.get('input').should('have.value', '$0,00CAD');
}
);

it(
'should format when type only cents',
() => {
cy.get('input').type('1');
cy.get('input').should('have.value', '$0,01CAD');

cy.get('input').type('{backspace}');
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('12');
cy.get('input').should('have.value', '$0,12CAD');

cy.get('input').type('{backspace}'.repeat(2));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('66');
cy.get('input').should('have.value', '$0,66CAD');
}
);

it(
'should format dozens',
describe(
'common behaviour',
() => {
cy.get('input').type('123');
cy.get('input').should('have.value', '$1,23CAD');


cy.get('input').type('{backspace}'.repeat(3));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('1234');
cy.get('input').should('have.value', '$12,34CAD');

cy.get('input').type('{backspace}'.repeat(4));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('6666');
cy.get('input').should('have.value', '$66,66CAD');
beforeEach(
() => {
cy.visit(getUrl({ prefix: '$', suffix: 'CAD' }));
cy.reload();
}
);

it(
'should format when input was created',
() => {
cy.get('input').should('have.value', '$0,00CAD');
}
);

it(
'shouldn\'t allow type a letter',
() => {
cy.get('input').type('abc');
cy.get('input').should('have.value', '$0,00CAD');
}
);

it(
'should format when type only cents',
() => {
cy.get('input').type('1');
cy.get('input').should('have.value', '$0,01CAD');

cy.get('input').type('{backspace}');
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('12');
cy.get('input').should('have.value', '$0,12CAD');

cy.get('input').type('{backspace}'.repeat(2));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('66');
cy.get('input').should('have.value', '$0,66CAD');
}
);

it(
'should format dozens',
() => {
cy.get('input').type('123');
cy.get('input').should('have.value', '$1,23CAD');


cy.get('input').type('{backspace}'.repeat(3));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('1234');
cy.get('input').should('have.value', '$12,34CAD');

cy.get('input').type('{backspace}'.repeat(4));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('6666');
cy.get('input').should('have.value', '$66,66CAD');
}
);

it(
'should format hundreds',
() => {
cy.get('input').type('12345');
cy.get('input').should('have.value', '$123,45CAD');

cy.get('input').type('{backspace}'.repeat(5));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('66699');
cy.get('input').should('have.value', '$666,99CAD');
}
);
}
);

it(
'should format hundreds',
() => {
cy.get('input').type('12345');
cy.get('input').should('have.value', '$123,45CAD');

cy.get('input').type('{backspace}'.repeat(5));
cy.get('input').should('have.value', '$0,00CAD');

cy.get('input').type('66699');
cy.get('input').should('have.value', '$666,99CAD');

describe(
'wrong behaviour prefix and suffix with space',
() => {
beforeEach(
() => {
cy.visit(getUrl({ prefix: '$ ', suffix: ' CAD' }));
cy.reload();
}
);

it(
'should format when input was created',
() => {
cy.get('input').should('have.value', '$ 0,00 CAD');
}
);

it(
'shouldn\'t allow type a letter',
() => {
cy.get('input').type('abc');
cy.get('input').should('have.value', '$ 0,00 CAD');
}
);

it(
'should format when type only cents',
() => {
cy.get('input').type('1');
cy.get('input').should('have.value', '$ 0,01 CAD');

cy.get('input').type('{backspace}');
cy.get('input').should('have.value', '$ 0,00 CAD');

cy.get('input').type('12');
cy.get('input').should('have.value', '$ 0,12 CAD');

cy.get('input').type('{backspace}'.repeat(2));
cy.get('input').should('have.value', '$ 0,00 CAD');

cy.get('input').type('66');
cy.get('input').should('have.value', '$ 0,66 CAD');
}
);

it(
'should format dozens',
() => {
cy.get('input').type('123');
cy.get('input').should('have.value', '$ 1,23 CAD');


cy.get('input').type('{backspace}'.repeat(3));
cy.get('input').should('have.value', '$ 0,00 CAD');

cy.get('input').type('1234');
cy.get('input').should('have.value', '$ 12,34 CAD');

cy.get('input').type('{backspace}'.repeat(4));
cy.get('input').should('have.value', '$ 0,00 CAD');

cy.get('input').type('6666');
cy.get('input').should('have.value', '$ 66,66 CAD');
}
);

it(
'should format hundreds',
() => {
cy.get('input').type('12345');
cy.get('input').should('have.value', '$ 123,45 CAD');

cy.get('input').type('{backspace}'.repeat(5));
cy.get('input').should('have.value', '$ 0,00 CAD');

cy.get('input').type('66699');
cy.get('input').should('have.value', '$ 666,99 CAD');
}
);
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-mask-money",
"version": "4.1.2",
"version": "4.1.3",
"private": false,
"description": "Simple money mask developed with pure JavaScript. To run on Client Side and Server Side",
"types": "./lib/simple-mask-money.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/set-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function setMask(
continue;
}

if (Number.isNaN(Number(character))) continue;
if (Number.isNaN(Number(character)) || character === ' ') continue;

thousandsCounter -= 1;

Expand Down
44 changes: 44 additions & 0 deletions tests/set-mask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ describe(
expect(input.value).toBe('R$0,69BRL');
},
);

it(
'should format when type cents only and prefix has space',
() => {
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });

write('69');

expect(input.value).toBe('R$ 0,69 BRL');
},
);

it(
'should format when type cents and dozens',
Expand All @@ -210,6 +221,17 @@ describe(
expect(input.value).toBe('R$6,66BRL');
},
);

it(
'should format when type cents and dozens and prefix has space',
() => {
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });

write('666');

expect(input.value).toBe('R$ 6,66 BRL');
},
);

it(
'should format when type thousands',
Expand All @@ -221,6 +243,17 @@ describe(
expect(input.value).toBe('R$6.669,99BRL');
},
);

it(
'should format when type thousands',
() => {
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });

write('666999');

expect(input.value).toBe('R$ 6.669,99 BRL');
},
);

it(
'should format when type number sequentialy',
Expand All @@ -232,6 +265,17 @@ describe(
expect(input.value).toBe('R$12.345,67BRL');
},
);

it(
'should format when type number sequentialy',
() => {
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });

write('1234567');

expect(input.value).toBe('R$ 12.345,67 BRL');
},
);
}
);

Expand Down

0 comments on commit 029af4d

Please sign in to comment.