Completed
Pull Request — master (#1089)
by Gabriel
115:44 queued 50:34
created

test_form_data_extractor.js ➔ ... ➔ createElement   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 6
rs 9.4285
nop 1
1
'use strict';
2
3
var test = require( 'tape' ),
4
	sinon = require( 'sinon' ),
5
	extractor = require( '../lib/form_data_extractor' )
6
;
7
8
test( 'Map can be built from select options', function ( t ) {
9
	var createOption = function () {
10
			return {
11
				attr: sinon.stub(),
12
				text: sinon.stub()
13
			}
14
		},
15
		optionOne = createOption(),
16
		optionTwo = createOption(),
17
		options = {
18
			get: sinon.stub()
19
		},
20
		container = {
21
			find: sinon.stub()
22
		}
23
	;
24
25
	options.get.returns( [ optionOne, optionTwo ] );
26
	container.find.withArgs( 'option' ).returns( options );
27
28
	optionOne.attr.withArgs( 'value' ).returns( 'onekey' );
29
	optionOne.text.returns( 'hello' );
30
	optionTwo.attr.withArgs( 'value' ).returns( 'anotherkey' );
31
	optionTwo.text.returns( 'world' );
32
33
	global.$ = sinon.stub();
34
	global.$.returnsArg( 0 ); // pretend to extend the DOM element given to jQuery. We don't but have all methods stubbed
35
36
	t.deepEqual(
37
		extractor.mapFromSelectOptions( container ),
38
		{ 'onekey': 'hello', 'anotherkey': 'world' }
39
	);
40
41
	delete global.$;
42
43
	t.end();
44
} );
45
46
test( 'Map can be built from radio button labels', function ( t ) {
47
	var createLabel = function ( labelText ) {
48
			return {
49
				text: sinon.stub().returns( labelText )
50
			}
51
		},
52
		createElement = function ( labelText ) {
53
			return {
54
				attr: sinon.stub(),
55
				next: sinon.stub().withArgs( 'label' ).returns( createLabel( labelText ) )
56
			}
57
		},
58
		radioOne = createElement( 'alpha' ),
59
		radioTwo = createElement( 'beta' ),
60
		radios = {
61
			get: sinon.stub()
62
		},
63
		container = {
64
			find: sinon.stub()
65
		}
66
	;
67
68
	radios.get.returns( [ radioOne, radioTwo ] );
69
	container.find.withArgs( 'input[type="radio"]' ).returns( radios );
70
71
	radioOne.attr.withArgs( 'value' ).returns( 'a' );
72
	radioTwo.attr.withArgs( 'value' ).returns( 'b' );
73
74
	global.$ = sinon.stub();
75
	global.$.returnsArg( 0 ); // pretend to extend the DOM element given to jQuery. We don't but have all methods stubbed
76
77
	t.deepEqual(
78
		extractor.mapFromRadioLabels( container ),
79
		{ 'a': 'alpha', 'b': 'beta' }
80
	);
81
82
	delete global.$;
83
84
	t.end();
85
} );
86
87
test( 'Map can be built from radio button label\'s data attributes', function ( t ) {
88
	var createLabel = function ( labelText ) {
89
			return {
90
				data: sinon.stub().withArgs( 'short-text' ).returns( labelText )
91
			}
92
		},
93
		createElement = function ( labelText ) {
94
			return {
95
				attr: sinon.stub(),
96
				next: sinon.stub().withArgs( 'label' ).returns( createLabel( labelText ) )
97
			}
98
		},
99
		radioOne = createElement( 'alpha' ),
100
		radioTwo = createElement( 'beta' ),
101
		radios = {
102
			get: sinon.stub()
103
		},
104
		container = {
105
			find: sinon.stub()
106
		}
107
	;
108
109
	radios.get.returns( [ radioOne, radioTwo ] );
110
	container.find.withArgs( 'input[type="radio"]' ).returns( radios );
111
112
	radioOne.attr.withArgs( 'value' ).returns( 'a' );
113
	radioTwo.attr.withArgs( 'value' ).returns( 'b' );
114
115
	global.$ = sinon.stub();
116
	global.$.returnsArg( 0 ); // pretend to extend the DOM element given to jQuery. We don't but have all methods stubbed
117
118
	t.deepEqual(
119
		extractor.mapFromRadioLabelsShort( container ),
120
		{ 'a': 'alpha', 'b': 'beta' }
121
	);
122
123
	delete global.$;
124
125
	t.end();
126
} );
127
128
test( 'Map can be built from radio button info texts', function ( t ) {
129
	var createLabel = function ( infoText ) {
130
			return {
131
				data: sinon.stub().withArgs( 'info-text' ) .returns( infoText )
132
			}
133
		},
134
		createElement = function ( infoText ) {
135
			return {
136
				attr: sinon.stub(),
137
				parents: sinon.stub().withArgs( '.wrap-field' ).returns( createLabel( infoText ) )
138
			}
139
		},
140
		radioOne = createElement( 'my longer text' ),
141
		radioTwo = createElement( 'to someone' ),
142
		radioThree = createElement( 'who reads it' ),
143
		radios = {
144
			get: sinon.stub()
145
		},
146
		container = {
147
			find: sinon.stub()
148
		}
149
	;
150
151
	radios.get.returns( [ radioOne, radioTwo, radioThree ] );
152
	container.find.withArgs( '.wrap-input input[type="radio"]' ).returns( radios );
153
154
	radioOne.attr.withArgs( 'value' ).returns( 'uno' );
155
	radioTwo.attr.withArgs( 'value' ).returns( 'dos' );
156
	radioThree.attr.withArgs( 'value' ).returns( 'tres' );
157
158
	global.$ = sinon.stub();
159
	global.$.returnsArg( 0 ); // pretend to extend the DOM element given to jQuery. We don't but have all methods stubbed
160
161
	t.deepEqual(
162
		extractor.mapFromRadioInfoTexts( container ),
163
		{ 'uno': 'my longer text', 'dos': 'to someone', 'tres': 'who reads it' }
164
	);
165
166
	delete global.$;
167
168
	t.end();
169
} );
170