Completed
Pull Request — master (#1036)
by wiese
62:12
created

applies sub-elementsꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
dl 0
loc 24
rs 8.9713
c 1
b 0
f 0
nop 1
1
'use strict';
2
3
var test = require( 'tape' ),
4
	sinon = require( 'sinon' ),
5
	objectAssign = require( 'object-assign' ),
6
	SectionInfo = require( '../../lib/view_handler/section_info' ),
7
	// your typical jQuery extended DOM node
8
	createElement = function () {
9
		return {
10
			find: sinon.stub(),
11
			text: sinon.stub(),
12
			html: sinon.stub(),
13
			removeClass: sinon.stub(),
14
			addClass: sinon.stub(),
15
			data: sinon.stub()
16
		};
17
	},
18
	formattedAmount = '23,00 EUR',
19
	currencyFormatter = {
20
		format: sinon.stub().returns( formattedAmount )
21
	}
22
;
23
24
test( 'The amount is passed to the currency formatter', function ( t ) {
25
	var container = createElement(),
26
		icon = createElement(),
27
		text = createElement(),
28
		longText = createElement(),
29
		handler = objectAssign( Object.create( SectionInfo.AmountFrequencySectionInfo ), {
30
			container: container,
31
32
			icon: icon,
33
			text: text,
34
			longText: longText,
35
36
			valueIconMap: { '0': 'icon-0', '1': 'icon-1' },
37
			valueTextMap: { '0': 'lorem', '1': 'ipsum' },
38
			valueLongTextMap: { '0': 'lorem lorem', '1': 'ipsum ipsum' },
39
40
			currencyFormatter: currencyFormatter
41
		} );
42
43
	handler.update( 23.00, '0', true );
44
45
	t.ok( currencyFormatter.format.calledOnce, 'format is called' );
46
	t.equals( currencyFormatter.format.firstCall.args[ 0 ], 23.00, 'Amount is passed to formatter' );
47
	t.end();
48
} );
49
50
test( 'Formatted amount is set in amount element', function ( t ) {
51
	var container = createElement(),
52
		icon = createElement(),
53
		text = createElement(),
54
		longText = createElement(),
55
		handler = objectAssign( Object.create( SectionInfo.AmountFrequencySectionInfo ), {
56
			container: container,
57
58
			icon: icon,
59
			text: text,
60
			longText: longText,
61
62
			valueIconMap: { '0': 'icon-0', '1': 'icon-1' },
63
			valueTextMap: { '0': 'lorem', '1': 'ipsum' },
64
			valueLongTextMap: { '0': 'lorem lorem', '1': 'ipsum ipsum' },
65
66
			currencyFormatter: currencyFormatter
67
		} );
68
69
	handler.update( 23.00, '0', true );
70
71
	t.ok( text.text.calledOnce, 'Amount is set' );
72
	t.equals( text.text.firstCall.args[ 0 ], formattedAmount, 'amount is set' );
73
74
	t.end();
75
} );
76
77
test( 'Icon is set according to value', function ( t ) {
78
	var container = createElement(),
79
		icon = createElement(),
80
		text = createElement(),
81
		longText = createElement(),
82
		handler = objectAssign( Object.create( SectionInfo.AmountFrequencySectionInfo ), {
83
			container: container,
84
85
			icon: icon,
86
			text: text,
87
			longText: longText,
88
89
			valueIconMap: { '0': 'icon-0', '1': 'icon-1' },
90
			valueTextMap: { '0': 'lorem', '1': 'ipsum' },
91
			valueLongTextMap: { '0': 'lorem lorem', '1': 'ipsum ipsum' },
92
93
			currencyFormatter: currencyFormatter
94
		} );
95
96
	handler.update( 34.00, '1', true );
97
98
	t.ok( icon.removeClass.withArgs( 'icon-error' ).calledOnce );
99
	t.ok( icon.removeClass.withArgs( 'icon-0 icon-1' ).calledOnce );
100
	t.ok( icon.addClass.withArgs( 'icon-1' ).calledOnce );
101
102
	t.end();
103
} );
104
105
test( 'Icon is set to error if value out of bounds and error desired', function ( t ) {
106
	var container = createElement(),
107
		icon = createElement(),
108
		handler = objectAssign( Object.create( SectionInfo.AmountFrequencySectionInfo ), {
109
			container: container,
110
111
			icon: icon,
112
113
			valueIconMap: { '0': 'icon-0', '1': 'icon-1' },
114
115
			currencyFormatter: currencyFormatter
116
		} );
117
118
	icon.data.withArgs( 'display-error' ).returns( true );
119
120
	handler.update( 101, 'outOfBounds', true );
121
122
	t.ok( icon.removeClass.withArgs( 'icon-error' ).calledOnce );
123
	t.ok( icon.removeClass.withArgs( 'icon-0 icon-1' ).calledOnce );
124
	t.ok( icon.addClass.withArgs( 'icon-error' ).calledOnce );
125
126
	t.end();
127
} );
128
129
test( 'Icon is reset if value out of bounds and error not desired', function ( t ) {
130
	var container = createElement(),
131
		icon = createElement(),
132
		handler = objectAssign( Object.create( SectionInfo.AmountFrequencySectionInfo ), {
133
			container: container,
134
135
			icon: icon,
136
137
			valueIconMap: { '0': 'icon-0', '1': 'icon-1' },
138
139
			currencyFormatter: currencyFormatter
140
		} );
141
142
	icon.data.withArgs( 'display-error' ).returns( false );
143
144
	handler.update( 101, 'outOfBounds', true );
145
146
	t.ok( icon.removeClass.withArgs( 'icon-error' ).calledOnce );
147
	t.ok( icon.removeClass.withArgs( 'icon-0 icon-1' ).calledOnce );
148
	t.ok( icon.addClass.notCalled );
149
150
	t.end();
151
} );
152
153
test( 'Payment type is set in respective elements', function ( t ) {
154
	var container = createElement(),
155
		icon = createElement(),
156
		text = createElement(),
157
		longText = createElement(),
158
		handler = objectAssign( Object.create( SectionInfo.PaymentTypeSectionInfo ), {
159
			container: container,
160
161
			icon: icon,
162
			text: text,
163
			longText: longText,
164
165
			valueIconMap: { 'BEZ': 'icon-BEZ', 'PPL': 'icon-PPL' },
166
			valueTextMap: { 'BEZ': 'Lastschrift', 'PPL': 'Paypal' },
167
			valueLongTextMap: { 'BEZ': 'Will be deducted', 'PPL': 'Forward to PPL' }
168
		} );
169
170
	handler.update( 'PPL', '', '', true );
171
172
	t.ok( container.addClass.withArgs( 'completed' ).calledOnce );
173
	t.ok( icon.addClass.withArgs( 'icon-PPL' ).calledOnce );
174
	t.ok( text.text.withArgs( 'Paypal' ).calledOnce, 'Payment type is set' );
175
	t.ok( longText.text.withArgs( 'Forward to PPL' ).calledOnce, 'Long text is set' );
176
177
	t.end();
178
} );
179
180
test( 'Missing features are gently skipped', function ( t ) {
181
	var container = createElement(),
182
		handler = objectAssign( Object.create( SectionInfo.PaymentTypeSectionInfo ), {
183
			container: container,
184
185
			icon: null,
186
			text: null,
187
			longText: null,
188
189
			valueIconMap: { 'BEZ': 'icon-BEZ', 'PPL': 'icon-PPL' },
190
			valueTextMap: { 'BEZ': 'Lastschrift', 'PPL': 'Paypal' },
191
			valueLongTextMap: { 'BEZ': 'Will be deducted', 'PPL': 'Forward to PPL' }
192
		} );
193
194
	handler.update( 'BEZ', '', '', false );
195
196
	t.ok( container, 'elements injected as null have no methods called upon, cause no errors' );
197
198
	t.end();
199
} );
200
201
test( 'Instance correctly detects and applies sub-elements', function ( t ) {
202
	var container = createElement(),
203
		icon = createElement(),
204
		text = createElement(),
205
		longText = createElement()
206
	;
207
208
	container.find.withArgs( 'i' ).returns( icon );
209
	container.find.withArgs( '.text' ).returns( text );
210
	container.find.withArgs( '.info-detail' ).returns( longText );
211
212
	var handler = SectionInfo.createInstance( {}, container );
213
214
	t.deepEquals( handler.container, container );
215
	t.deepEquals( handler.icon, icon );
216
	t.deepEquals( handler.text, text );
217
	t.deepEquals( handler.longText, longText );
218
219
	t.ok( container.find.withArgs( 'i' ).calledOnce );
220
	t.ok( container.find.withArgs( '.text' ).calledOnce );
221
	t.ok( container.find.withArgs( '.info-detail' ).calledOnce );
222
223
	t.end();
224
} );
225
226
test( 'Instance is created with properties applied', function ( t ) {
227
	var container = createElement(),
228
		iconMap = { 'a': 1 },
229
		textMap = { 'a': 2 },
230
		longTextMap = { 'a': 3 },
231
		additionalProperties = { 'alpha': 'gamma' }
232
	;
233
234
	var handler = SectionInfo.createInstance( {}, container, iconMap, textMap, longTextMap, additionalProperties );
235
236
	t.deepEquals( handler.valueIconMap, iconMap );
237
	t.deepEquals( handler.valueTextMap, textMap );
238
	t.deepEquals( handler.valueLongTextMap, longTextMap );
239
240
	t.deepEquals( handler.alpha, 'gamma' );
241
242
	t.end();
243
} );
244
245
test( 'Proxy forwards calls and arguments', function ( t ) {
246
	var widgetOneDom = createElement(),
247
		widgetTwoDom = createElement(),
248
		fakeType = {
249
			update: sinon.stub()
250
		},
251
		// IRL a jQuery object that matched multiple DOM nodes
252
		containers = {
253
			get: sinon.stub().returns( [ widgetOneDom, widgetTwoDom ] )
254
		}
255
	;
256
257
	global.$ = sinon.stub();
258
	global.$.returnsArg( 0 ); // pretend to extend the DOM element given to jQuery. We don't but have all methods stubbed
259
260
	var proxy = SectionInfo.createProxy( fakeType, containers, {}, {}, {}, {} );
261
262
	proxy.update(  'a', 'b', 'c' );
263
264
	t.ok( proxy.widgets instanceof Array );
265
	t.equals( proxy.widgets.length, 2 );
266
	t.deepEquals( proxy.widgets[ 0 ].update.firstCall.args, [ 'a', 'b', 'c' ] );
267
	t.deepEquals( proxy.widgets[ 1 ].update.firstCall.args, [ 'a', 'b', 'c' ] );
268
269
	delete global.$;
270
	t.end();
271
} );
272