Completed
Push — master ( c0f76f...37333c )
by Jeroen De
299:55 queued 234:55
created

is successful, related fields retrieve valid statusꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
nc 1
dl 8
loc 24
rs 8.9713
c 1
b 1
f 0
nop 1
1
'use strict';
2
3
var test = require( 'tape-catch' ),
4
	deepFreeze = require( 'deep-freeze' ),
5
	inputValidation = require( '../../lib/reducers/input_validation' ).inputValidation;
6
7
function newAction( value ) {
8
	return {
9
		type: 'VALIDATE_INPUT',
10
		payload: {
11
			contentName: 'testField',
12
			value: value,
13
			pattern: '^[a-z0-9]+$'
14
		}
15
	};
16
}
17
18
function newBankDataValidationAction( payload ) {
19
	return {
20
		type: 'FINISH_BANK_DATA_VALIDATION',
21
		payload: payload
22
	};
23
}
24
25
function newAddressValidationAction( returnedStatus ) {
26
	return {
27
		type: 'FINISH_ADDRESS_VALIDATION',
28
		payload: {
29
			status: returnedStatus
30
		}
31
	};
32
}
33
34
test( 'If no data was entered yet, validation returns previous state', function ( t ) {
35
	var stateBefore = { testField: { dataEntered: false, isValid: null } },
36
		expectedState = { testField: { dataEntered: false, isValid: null } };
37
38
	deepFreeze( stateBefore );
39
	t.deepEqual( inputValidation( stateBefore, newAction( '' ) ), expectedState );
40
	t.end();
41
} );
42
43
test( 'If invalid data was entered, validation returns invalid state', function ( t ) {
44
	var stateBefore = { testField: { dataEntered: false, isValid: null } },
45
		expectedState = { testField: { dataEntered: true, isValid: false } };
46
47
	deepFreeze( stateBefore );
48
	t.deepEqual( inputValidation( stateBefore, newAction( 'ShouldFail' ) ), expectedState );
49
	t.end();
50
} );
51
52
test( 'If valid data was entered, validation returns valid state', function ( t ) {
53
	var stateBefore = { testField: { dataEntered: false, isValid: null } },
54
		expectedState = { testField: { dataEntered: true, isValid: true } };
55
56
	deepFreeze( stateBefore );
57
	t.deepEqual( inputValidation( stateBefore, newAction( 'abc123' ) ), expectedState );
58
	t.end();
59
} );
60
61
test( 'If valid data was changed to invalid data, validation returns invalid state', function ( t ) {
62
	var stateBefore = { testField: { dataEntered: true, isValid: true } },
63
		expectedState = { testField: { dataEntered: true, isValid: false } };
64
65
	deepFreeze( stateBefore );
66
	t.deepEqual( inputValidation( stateBefore, newAction( '' ) ), expectedState );
67
	t.end();
68
} );
69
70
test( 'If bank data validation is successful, all fields in payload have valid status', function ( t ) {
71
	var stateBefore = {
72
			iban: { dataEntered: false, isValid: null },
73
			bic: { dataEntered: false, isValid: null },
74
			accountNumber: { dataEntered: false, isValid: null },
75
			bankCode: { dataEntered: false, isValid: null }
76
		},
77
		expectedState = {
78
			iban: { dataEntered: true, isValid: true },
79
			bic: { dataEntered: true, isValid: true },
80
			accountNumber: { dataEntered: true, isValid: true },
81
			bankCode: { dataEntered: true, isValid: true }
82
		};
83
84
	deepFreeze( stateBefore );
85
	t.deepEqual( inputValidation( stateBefore, newBankDataValidationAction( {
86
		status: 'OK',
87
		iban: 'DE12500105170648489890',
88
		bic: 'INGDDEFFXXX',
89
		bankCode: '50010517',
90
		account: '064847930'
91
	} ) ), expectedState );
92
	t.end();
93
} );
94
95
test( 'If bank data validation is successful, only fields in payload change status', function ( t ) {
96
	var stateBefore = {
97
			iban: { dataEntered: false, isValid: null },
98
			bic: { dataEntered: false, isValid: null },
99
			accountNumber: { dataEntered: false, isValid: null },
100
			bankCode: { dataEntered: false, isValid: null }
101
		},
102
		expectedState = {
103
			iban: { dataEntered: true, isValid: true },
104 View Code Duplication
			bic: { dataEntered: false, isValid: null },
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
105
			accountNumber: { dataEntered: false, isValid: null },
106
			bankCode: { dataEntered: false, isValid: null }
107
		};
108
109
	deepFreeze( stateBefore );
110
	t.deepEqual( inputValidation( stateBefore, newBankDataValidationAction( {
111
		status: 'OK',
112
		iban: 'DE12500105170648489890'
113
	} ) ), expectedState );
114
	t.end();
115
} );
116
117
test( 'If bank data validation fails, all fields retrieve invalid status', function ( t ) {
118
	var stateBefore = {
119
			iban: { dataEntered: false, isValid: null },
120
			bic: { dataEntered: false, isValid: null },
121
			accountNumber: { dataEntered: false, isValid: null },
122
			bankCode: { dataEntered: false, isValid: null }
123
		},
124
		expectedState = {
125
			iban: { dataEntered: true, isValid: false },
126 View Code Duplication
			bic: { dataEntered: true, isValid: false },
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
127
			accountNumber: { dataEntered: true, isValid: false },
128
			bankCode: { dataEntered: true, isValid: false }
129
		};
130
131
	deepFreeze( stateBefore );
132
	t.deepEqual( inputValidation( stateBefore, newBankDataValidationAction( { status: 'ERR' } ) ), expectedState );
133
	t.end();
134
} );
135
136
test( 'If address data validation is successful, related fields retrieve valid status', function ( t ) {
137
	var stateBefore = {
138
			firstName: { dataEntered: true, isValid: null },
139
			lastName: { dataEntered: true, isValid: null },
140
			street: { dataEntered: true, isValid: null },
141
			city: { dataEntered: true, isValid: null },
142
			postcode: { dataEntered: true, isValid: null },
143
			email: { dataEntered: true, isValid: null },
144
			iAmUnrelated: { dataEntered: true, isValid: null }
145
		},
146
		expectedState = {
147
			firstName: { dataEntered: true, isValid: true },
148
			lastName: { dataEntered: true, isValid: true },
149
			street: { dataEntered: true, isValid: true },
150
			city: { dataEntered: true, isValid: true },
151
			postcode: { dataEntered: true, isValid: true },
152
			email: { dataEntered: true, isValid: true },
153
			iAmUnrelated: { dataEntered: true, isValid: null }
154
		};
155
156
	deepFreeze( stateBefore );
157
	t.deepEqual( inputValidation( stateBefore, newAddressValidationAction( 'OK' ) ), expectedState );
158
	t.end();
159
} );
160
161
test( 'If address data validation is successful, related invalid fields remain invalid', function ( t ) {
162
	var stateBefore = {
163
			postcode: { dataEntered: true, isValid: false },
164
			city: { dataEntered: true, isValid: true },
165
			iAmUnrelated: { dataEntered: true, isValid: null }
166
		},
167
		expectedState = {
168
			postcode: { dataEntered: true, isValid: false },
169
			city: { dataEntered: true, isValid: true },
170
			iAmUnrelated: { dataEntered: true, isValid: null }
171
		};
172
173
	deepFreeze( stateBefore );
174
	t.deepEqual( inputValidation( stateBefore, newAddressValidationAction( 'OK' ) ), expectedState );
175
	t.end();
176
} );
177