forked from axa-ch-webhub-cloud/pattern-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.test.js
74 lines (63 loc) · 2.79 KB
/
unit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import AXAInputText from './index';
describe('InputText', () => {
describe('_formatCurrency', () => {
beforeEach(() => {
AXAInputText.prototype.currency = 'chf';
AXAInputText.prototype.type = 'text';
AXAInputText.prototype.currencyFormatter = null;
AXAInputText.prototype.invalidFormat = null;
});
it('should create new NumberFormat object', () => {
AXAInputText.prototype._formatCurrency('1');
expect(AXAInputText.prototype.currencyFormatter).not.toBe(null);
});
it('should set invalid to false (if input contains a number)', () => {
AXAInputText.prototype._formatCurrency('1');
expect(AXAInputText.prototype.invalidFormat).toBe(false);
});
it('should set invalid to false (if input is empty)', () => {
AXAInputText.prototype._formatCurrency('');
expect(AXAInputText.prototype.invalidFormat).toBe(false);
});
it('should set invalid to true (if input contains just no-digits and is not empty)', () => {
AXAInputText.prototype._formatCurrency('nodigits');
expect(AXAInputText.prototype.invalidFormat).toBe(true);
});
it('should return formatted number', () => {
const returnValue = AXAInputText.prototype._formatCurrency('1000');
expect(returnValue).toBe('CHF 1’000.00'); // be aware: there is no simple empty-space between CHF and 1! Its a HTML entity NBSP.
});
it('should return inputted value', () => {
const inputValue = 'this is my input';
AXAInputText.prototype.currency = null;
expect(AXAInputText.prototype._formatCurrency(inputValue)).toBe(
inputValue
);
});
// false positive tests
it('should not create a NumberFormat object (wrong type)', () => {
AXAInputText.prototype.type = 'password';
AXAInputText.prototype._formatCurrency('1');
expect(AXAInputText.prototype.currencyFormatter).toBe(null);
});
it('should not create a new NumberFormat object (already exists)', () => {
const alreadyExistingNumberFormatObject = { format: () => {} };
AXAInputText.prototype.currencyFormatter =
alreadyExistingNumberFormatObject;
AXAInputText.prototype._formatCurrency('1');
expect(AXAInputText.prototype.currencyFormatter).toBe(
alreadyExistingNumberFormatObject
);
});
it('should not create a NumberFormat object (no currency set)', () => {
AXAInputText.prototype.currency = '';
AXAInputText.prototype._formatCurrency('1');
expect(AXAInputText.prototype.currencyFormatter).toBe(null);
});
it('should set invalid state if too many dots are present', () => {
const returnValue = AXAInputText.prototype._formatCurrency('n.2.3.5');
expect(returnValue).toBe('n.2.3.5');
expect(AXAInputText.prototype.invalidFormat).toBe(true);
});
});
});