Skip to content

Commit

Permalink
fix(money-mask): fixing format for number with one decimal place
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-hur Santos Ott committed Nov 11, 2017
1 parent 2fb3e6b commit b486262
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 138 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ var money = MaskService.toMask('money', '123', {


# Changelog
## 1.6.4
* Fixing wrong format when use money mask and values with only one decimal place (`1.9`) (thanks to [Pablo](https://github.com/rochapablo)).

## 1.6.3
* Fixing moment version (thanks to [Edward Coleridge Smith](https://github.com/edcs))
* Adding pre-builded lib to improve build phase (thanks to [Giorgi Bagdavadze](https://github.com/notgiorgi))
Expand Down
256 changes: 144 additions & 112 deletions __tests__/money.mask.test.js
Original file line number Diff line number Diff line change
@@ -1,198 +1,230 @@
import { MoneyMask } from '../lib/masks';
import { MoneyMask } from '../lib/masks'

test('getType results money', () => {
var expected = 'money';
var received = MoneyMask.getType();
var expected = 'money'
var received = MoneyMask.getType()

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('1 results R$0,01', () => {
var mask = new MoneyMask();
var expected = 'R$0,01';
var received = mask.getValue('1');
var mask = new MoneyMask()
var expected = 'R$0,01'
var received = mask.getValue('1')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('111 results R$1,11', () => {
var mask = new MoneyMask();
var expected = 'R$1,11';
var received = mask.getValue('111');
var mask = new MoneyMask()
var expected = 'R$1,11'
var received = mask.getValue('111')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('1111 results R$11,11', () => {
var mask = new MoneyMask();
var expected = 'R$11,11';
var received = mask.getValue('1111');
var mask = new MoneyMask()
var expected = 'R$11,11'
var received = mask.getValue('1111')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('11111 results R$111,11', () => {
var mask = new MoneyMask();
var expected = 'R$111,11';
var received = mask.getValue('11111');
var mask = new MoneyMask()
var expected = 'R$111,11'
var received = mask.getValue('11111')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('111111 results R$1.111,11', () => {
var mask = new MoneyMask();
var expected = 'R$1.111,11';
var received = mask.getValue('111111');
var mask = new MoneyMask()
var expected = 'R$1.111,11'
var received = mask.getValue('111111')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('111111111 results R$1.111.111,11', () => {
var mask = new MoneyMask();
var expected = 'R$1.111.111,11';
var received = mask.getValue('111111111');
var mask = new MoneyMask()
var expected = 'R$1.111.111,11'
var received = mask.getValue('111111111')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test(' results R$0,00', () => {
var mask = new MoneyMask();
var expected = 'R$0,00';
var received = mask.getValue('');
var mask = new MoneyMask()
var expected = 'R$0,00'
var received = mask.getValue('')

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('11111 precision 3 results R$11,111', () => {
var mask = new MoneyMask();
var expected = 'R$11,111';
var mask = new MoneyMask()
var expected = 'R$11,111'
var received = mask.getValue('11111', {
precision: 3
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('111 separator . results R$1.11', () => {
var mask = new MoneyMask();
var expected = 'R$1.11';
var mask = new MoneyMask()
var expected = 'R$1.11'
var received = mask.getValue('111', {
separator: '.'
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('111111 delimiter , results R$1,111,11', () => {
var mask = new MoneyMask();
var expected = 'R$1,111,11';
var mask = new MoneyMask()
var expected = 'R$1,111,11'
var received = mask.getValue('111111', {
delimiter: ','
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('1 unit US$ results US$0,01', () => {
var mask = new MoneyMask();
var expected = 'US$0,01';
var mask = new MoneyMask()
var expected = 'US$0,01'
var received = mask.getValue('1', {
unit: 'US$'
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('1 suffixUnit $$$ results R$0,01', () => {
var mask = new MoneyMask();
var expected = 'R$0,01 $$$';
var mask = new MoneyMask()
var expected = 'R$0,01 $$$'
var received = mask.getValue('1', {
suffixUnit: '$$$'
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('1 zeroCents results R$1,00', () => {
var mask = new MoneyMask();
var expected = 'R$1,00';
var mask = new MoneyMask()
var expected = 'R$1,00'
var received = mask.getValue('1', {
zeroCents: true
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('US$ config with value 1234567 results US$12,345.67', () => {
var mask = new MoneyMask();
var expected = 'US$12,345.67';
var mask = new MoneyMask()
var expected = 'US$12,345.67'
var received = mask.getValue('1234567', {
unit: 'US$',
delimiter: ',',
separator: '.'
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('1 results R$0,01 and raw value 0.01', () => {
var mask = new MoneyMask();
var expected = 'R$0,01';
var received = mask.getValue('1');
var mask = new MoneyMask()
var expected = 'R$0,01'
var received = mask.getValue('1')

var expectedRawValue = 0.01;
var receivedRawValue = mask.getRawValue(received);
var expectedRawValue = 0.01
var receivedRawValue = mask.getRawValue(received)

expect(received).toBe(expected);
expect(receivedRawValue).toBe(expectedRawValue);
});
expect(received).toBe(expected)
expect(receivedRawValue).toBe(expectedRawValue)
})

test('111111 results R$1.111,11 and raw value 1111.11', () => {
var mask = new MoneyMask();
var expected = 'R$1.111,11';
var received = mask.getValue('111111');
var mask = new MoneyMask()
var expected = 'R$1.111,11'
var received = mask.getValue('111111')

var expectedRawValue = 1111.11;
var receivedRawValue = mask.getRawValue(received);
var expectedRawValue = 1111.11
var receivedRawValue = mask.getRawValue(received)

expect(received).toBe(expected);
expect(receivedRawValue).toBe(expectedRawValue);
});
expect(received).toBe(expected)
expect(receivedRawValue).toBe(expectedRawValue)
})

test('1 zeroCents results R$1,00 and raw value 1', () => {
var mask = new MoneyMask();
var expected = 'R$1,00';
var mask = new MoneyMask()
var expected = 'R$1,00'
var received = mask.getValue('1', {
zeroCents: true
});
})

var expectedRawValue = 1;
var receivedRawValue = mask.getRawValue(received);
var expectedRawValue = 1
var receivedRawValue = mask.getRawValue(received)

expect(received).toBe(expected);
expect(receivedRawValue).toBe(expectedRawValue);
});
expect(received).toBe(expected)
expect(receivedRawValue).toBe(expectedRawValue)
})

test('111111 delimiter , results R$1,111,11 and raw value 1111.11', () => {
var mask = new MoneyMask();
var expected = 'R$1,111,11';
var mask = new MoneyMask()
var expected = 'R$1,111,11'
var received = mask.getValue('111111', {
delimiter: ','
});
})

var expectedRawValue = 1111.11;
var receivedRawValue = mask.getRawValue(received);
var expectedRawValue = 1111.11
var receivedRawValue = mask.getRawValue(received)

expect(received).toBe(expected);
expect(receivedRawValue).toBe(expectedRawValue);
});
expect(received).toBe(expected)
expect(receivedRawValue).toBe(expectedRawValue)
})

test('1 unit US$ results US$ 0,01', () => {
var mask = new MoneyMask();
var expected = 'US$ 0,01';
var mask = new MoneyMask()
var expected = 'US$ 0,01'
var received = mask.getValue('1', {
unit: 'US$ '
});
})

expect(received).toBe(expected);
});
expect(received).toBe(expected)
})

test('number 1.9 must return R$1,90', () => {
var mask = new MoneyMask()
const expected = 'R$1,90'
const received = mask.getValue(1.9)

expect(received).toBe(expected)
})

test('number 0.05 must return R$0,05', () => {
var mask = new MoneyMask()
const expected = 'R$0,05'
const received = mask.getValue(0.05)

expect(received).toBe(expected)
})

test('number 0.85 must return R$0,85', () => {
var mask = new MoneyMask()
const expected = 'R$0,85'
const received = mask.getValue(0.85)

expect(received).toBe(expected)
})

test('number 1111.2 must return R$1.111,20', () => {
var mask = new MoneyMask()
const expected = 'R$1.111,20'
const received = mask.getValue(1111.2)

expect(received).toBe(expected)
})
Loading

0 comments on commit b486262

Please sign in to comment.