Skip to content

Commit 029af4d

Browse files
authored
🔀 Merge pull request #71 from codermarcos/fix/#70/problem-with-space-in-prefix
🐛 Fix problem with space in prefix
2 parents 2c61dab + fb40f7c commit 029af4d

File tree

5 files changed

+206
-75
lines changed

5 files changed

+206
-75
lines changed

cypress/e2e/v4.x.x/set-mask.cy.ts

+158-71
Original file line numberDiff line numberDiff line change
@@ -257,80 +257,167 @@ describe(
257257
describe(
258258
'prefix and suffix',
259259
() => {
260-
beforeEach(
261-
() => {
262-
cy.visit(getUrl({ prefix: '$', suffix: 'CAD' }));
263-
cy.reload();
264-
}
265-
);
266-
267-
it(
268-
'should format when input was created',
269-
() => {
270-
cy.get('input').should('have.value', '$0,00CAD');
271-
}
272-
);
273-
274-
it(
275-
'shouldn\'t allow type a letter',
276-
() => {
277-
cy.get('input').type('abc');
278-
cy.get('input').should('have.value', '$0,00CAD');
279-
}
280-
);
281-
282-
it(
283-
'should format when type only cents',
284-
() => {
285-
cy.get('input').type('1');
286-
cy.get('input').should('have.value', '$0,01CAD');
287-
288-
cy.get('input').type('{backspace}');
289-
cy.get('input').should('have.value', '$0,00CAD');
290-
291-
cy.get('input').type('12');
292-
cy.get('input').should('have.value', '$0,12CAD');
293-
294-
cy.get('input').type('{backspace}'.repeat(2));
295-
cy.get('input').should('have.value', '$0,00CAD');
296-
297-
cy.get('input').type('66');
298-
cy.get('input').should('have.value', '$0,66CAD');
299-
}
300-
);
301-
302-
it(
303-
'should format dozens',
260+
describe(
261+
'common behaviour',
304262
() => {
305-
cy.get('input').type('123');
306-
cy.get('input').should('have.value', '$1,23CAD');
307-
308-
309-
cy.get('input').type('{backspace}'.repeat(3));
310-
cy.get('input').should('have.value', '$0,00CAD');
311-
312-
cy.get('input').type('1234');
313-
cy.get('input').should('have.value', '$12,34CAD');
314-
315-
cy.get('input').type('{backspace}'.repeat(4));
316-
cy.get('input').should('have.value', '$0,00CAD');
317-
318-
cy.get('input').type('6666');
319-
cy.get('input').should('have.value', '$66,66CAD');
263+
beforeEach(
264+
() => {
265+
cy.visit(getUrl({ prefix: '$', suffix: 'CAD' }));
266+
cy.reload();
267+
}
268+
);
269+
270+
it(
271+
'should format when input was created',
272+
() => {
273+
cy.get('input').should('have.value', '$0,00CAD');
274+
}
275+
);
276+
277+
it(
278+
'shouldn\'t allow type a letter',
279+
() => {
280+
cy.get('input').type('abc');
281+
cy.get('input').should('have.value', '$0,00CAD');
282+
}
283+
);
284+
285+
it(
286+
'should format when type only cents',
287+
() => {
288+
cy.get('input').type('1');
289+
cy.get('input').should('have.value', '$0,01CAD');
290+
291+
cy.get('input').type('{backspace}');
292+
cy.get('input').should('have.value', '$0,00CAD');
293+
294+
cy.get('input').type('12');
295+
cy.get('input').should('have.value', '$0,12CAD');
296+
297+
cy.get('input').type('{backspace}'.repeat(2));
298+
cy.get('input').should('have.value', '$0,00CAD');
299+
300+
cy.get('input').type('66');
301+
cy.get('input').should('have.value', '$0,66CAD');
302+
}
303+
);
304+
305+
it(
306+
'should format dozens',
307+
() => {
308+
cy.get('input').type('123');
309+
cy.get('input').should('have.value', '$1,23CAD');
310+
311+
312+
cy.get('input').type('{backspace}'.repeat(3));
313+
cy.get('input').should('have.value', '$0,00CAD');
314+
315+
cy.get('input').type('1234');
316+
cy.get('input').should('have.value', '$12,34CAD');
317+
318+
cy.get('input').type('{backspace}'.repeat(4));
319+
cy.get('input').should('have.value', '$0,00CAD');
320+
321+
cy.get('input').type('6666');
322+
cy.get('input').should('have.value', '$66,66CAD');
323+
}
324+
);
325+
326+
it(
327+
'should format hundreds',
328+
() => {
329+
cy.get('input').type('12345');
330+
cy.get('input').should('have.value', '$123,45CAD');
331+
332+
cy.get('input').type('{backspace}'.repeat(5));
333+
cy.get('input').should('have.value', '$0,00CAD');
334+
335+
cy.get('input').type('66699');
336+
cy.get('input').should('have.value', '$666,99CAD');
337+
}
338+
);
320339
}
321340
);
322-
323-
it(
324-
'should format hundreds',
325-
() => {
326-
cy.get('input').type('12345');
327-
cy.get('input').should('have.value', '$123,45CAD');
328-
329-
cy.get('input').type('{backspace}'.repeat(5));
330-
cy.get('input').should('have.value', '$0,00CAD');
331-
332-
cy.get('input').type('66699');
333-
cy.get('input').should('have.value', '$666,99CAD');
341+
342+
describe(
343+
'wrong behaviour prefix and suffix with space',
344+
() => {
345+
beforeEach(
346+
() => {
347+
cy.visit(getUrl({ prefix: '$ ', suffix: ' CAD' }));
348+
cy.reload();
349+
}
350+
);
351+
352+
it(
353+
'should format when input was created',
354+
() => {
355+
cy.get('input').should('have.value', '$ 0,00 CAD');
356+
}
357+
);
358+
359+
it(
360+
'shouldn\'t allow type a letter',
361+
() => {
362+
cy.get('input').type('abc');
363+
cy.get('input').should('have.value', '$ 0,00 CAD');
364+
}
365+
);
366+
367+
it(
368+
'should format when type only cents',
369+
() => {
370+
cy.get('input').type('1');
371+
cy.get('input').should('have.value', '$ 0,01 CAD');
372+
373+
cy.get('input').type('{backspace}');
374+
cy.get('input').should('have.value', '$ 0,00 CAD');
375+
376+
cy.get('input').type('12');
377+
cy.get('input').should('have.value', '$ 0,12 CAD');
378+
379+
cy.get('input').type('{backspace}'.repeat(2));
380+
cy.get('input').should('have.value', '$ 0,00 CAD');
381+
382+
cy.get('input').type('66');
383+
cy.get('input').should('have.value', '$ 0,66 CAD');
384+
}
385+
);
386+
387+
it(
388+
'should format dozens',
389+
() => {
390+
cy.get('input').type('123');
391+
cy.get('input').should('have.value', '$ 1,23 CAD');
392+
393+
394+
cy.get('input').type('{backspace}'.repeat(3));
395+
cy.get('input').should('have.value', '$ 0,00 CAD');
396+
397+
cy.get('input').type('1234');
398+
cy.get('input').should('have.value', '$ 12,34 CAD');
399+
400+
cy.get('input').type('{backspace}'.repeat(4));
401+
cy.get('input').should('have.value', '$ 0,00 CAD');
402+
403+
cy.get('input').type('6666');
404+
cy.get('input').should('have.value', '$ 66,66 CAD');
405+
}
406+
);
407+
408+
it(
409+
'should format hundreds',
410+
() => {
411+
cy.get('input').type('12345');
412+
cy.get('input').should('have.value', '$ 123,45 CAD');
413+
414+
cy.get('input').type('{backspace}'.repeat(5));
415+
cy.get('input').should('have.value', '$ 0,00 CAD');
416+
417+
cy.get('input').type('66699');
418+
cy.get('input').should('have.value', '$ 666,99 CAD');
419+
}
420+
);
334421
}
335422
);
336423
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-mask-money",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"private": false,
55
"description": "Simple money mask developed with pure JavaScript. To run on Client Side and Server Side",
66
"types": "./lib/simple-mask-money.d.ts",

src/set-mask.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function setMask(
155155
continue;
156156
}
157157

158-
if (Number.isNaN(Number(character))) continue;
158+
if (Number.isNaN(Number(character)) || character === ' ') continue;
159159

160160
thousandsCounter -= 1;
161161

tests/set-mask.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ describe(
199199
expect(input.value).toBe('R$0,69BRL');
200200
},
201201
);
202+
203+
it(
204+
'should format when type cents only and prefix has space',
205+
() => {
206+
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });
207+
208+
write('69');
209+
210+
expect(input.value).toBe('R$ 0,69 BRL');
211+
},
212+
);
202213

203214
it(
204215
'should format when type cents and dozens',
@@ -210,6 +221,17 @@ describe(
210221
expect(input.value).toBe('R$6,66BRL');
211222
},
212223
);
224+
225+
it(
226+
'should format when type cents and dozens and prefix has space',
227+
() => {
228+
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });
229+
230+
write('666');
231+
232+
expect(input.value).toBe('R$ 6,66 BRL');
233+
},
234+
);
213235

214236
it(
215237
'should format when type thousands',
@@ -221,6 +243,17 @@ describe(
221243
expect(input.value).toBe('R$6.669,99BRL');
222244
},
223245
);
246+
247+
it(
248+
'should format when type thousands',
249+
() => {
250+
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });
251+
252+
write('666999');
253+
254+
expect(input.value).toBe('R$ 6.669,99 BRL');
255+
},
256+
);
224257

225258
it(
226259
'should format when type number sequentialy',
@@ -232,6 +265,17 @@ describe(
232265
expect(input.value).toBe('R$12.345,67BRL');
233266
},
234267
);
268+
269+
it(
270+
'should format when type number sequentialy',
271+
() => {
272+
clear = setMask(input, { prefix: 'R$ ', suffix: ' BRL' });
273+
274+
write('1234567');
275+
276+
expect(input.value).toBe('R$ 12.345,67 BRL');
277+
},
278+
);
235279
}
236280
);
237281

0 commit comments

Comments
 (0)