-
Notifications
You must be signed in to change notification settings - Fork 29
/
formatfield.spec.js
286 lines (243 loc) · 9.5 KB
/
formatfield.spec.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* eslint-env mocha */
import { before, beforeEach, after, afterEach, describe, it } from 'mocha';
import { assert, expect } from 'chai';
import sinon from 'sinon';
import requireUncached from 'require-uncached';
import jsdomify from 'jsdomify';
import {patchJsDom} from '../testutils/patch-jsdom';
import {removeChildElements} from '../../src/utils/dom-utils';
import {shouldBehaveLikeAMdlComponent} from '../testutils/shared-component-behaviours';
const JS_FORMAT_FIELD = 'mdlext-js-formatfield';
const FORMAT_FIELD_COMPONENT = 'MaterialExtFormatfield';
const fixture_textfield = `
<div class="mdl-textfield mdl-js-textfield mdlext-js-formatfield">
<input class="mdl-textfield__input" type="text" pattern="[0-9]*" id="phone">
<label class="mdl-textfield__label" for="phone">Phone</label>
<span class="mdl-textfield__error">Digits only</span>
</div>`;
const fixture = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Collapsible Fixture</title>
</head>
<body>
<main>
<div id="default-fixture">
${fixture_textfield}
</div>
<div id="mount">
</div>
</main>
</body>
</html>`;
describe('MaterialExtFormatfield', () => {
let mount;
before ( () => {
patchJsDom(fixture);
// Must load MDL after jsdom, see: https://github.com/mochajs/mocha/issues/1722
requireUncached( 'material-design-lite/material');
global.componentHandler = window.componentHandler;
expect(componentHandler, 'Expected global MDL component handler').to.be.an.object;
requireUncached('../../src/formatfield/formatfield');
expect(window.MaterialExtFormatfield, 'Expected MaterialExtFormatfield not to be null').to.not.null;
global.MaterialExtFormatfield = window.MaterialExtFormatfield;
});
after ( () => {
jsdomify.destroy();
});
beforeEach( () => {
mount = document.querySelector('#mount');
});
afterEach( () => {
const mdl = mount.querySelectorAll('.is-upgraded');
componentHandler.downgradeElements(mdl);
removeChildElements(mount);
});
describe('General behaviour', () => {
shouldBehaveLikeAMdlComponent({
componentName: FORMAT_FIELD_COMPONENT,
componentCssClass: JS_FORMAT_FIELD,
newComponenrMountNodeSelector: '#mount',
newComponentHtml: fixture_textfield
});
it('should have public methods available via widget', () => {
const {component} = createSingleLineTextfield();
mount.appendChild(component);
componentHandler.upgradeElement(component);
const methods = [
'getOptions',
'getUnformattedValue',
];
methods.forEach( fn => {
expect(component.MaterialExtFormatfield[fn]).to.be.a('function');
});
});
});
describe('Options', () => {
it('should have default options', () => {
const {component} = createSingleLineTextfield();
mount.appendChild(component);
componentHandler.upgradeElement(component);
const options = component.MaterialExtFormatfield.getOptions();
expect(options, 'Expected default options').to.be.defined;
expect(options.locales, 'Expected default options.locales').to.be.defined;
expect(options.groupSeparator, 'Expected default options.groupSeparator').to.be.defined;
expect(options.decimalSeparator, 'Expected default options.decimalSeparator').to.be.defined;
});
it('should set options via data attribute', () => {
const {component} = createSingleLineTextfield('{"locales": "nb-NO", "groupSeparator": ";", "decimalSeparator": "," }');
mount.appendChild(component);
expect(() => {
componentHandler.upgradeElement(component);
}).to.not.throw(Error);
const options = component.MaterialExtFormatfield.getOptions();
expect(options.locales).to.equal('nb-NO');
expect(options.groupSeparator).to.equal(';');
expect(options.decimalSeparator).to.equal(',');
});
it('should throw an error if data attribute is malformed', () => {
const {component} = createSingleLineTextfield('{"locales": ILLEGAL, "groupSeparator": VALUE, "decimalSeparator": "," }');
mount.appendChild(component);
expect(() => {
componentHandler.upgradeElement(component);
}).to.throw(Error);
});
it('should throw an error if options.groupSeparator === options.decimalSeparator', () => {
const {component} = createSingleLineTextfield('{"groupSeparator": ".", "decimalSeparator": "." }');
mount.appendChild(component);
expect(() => {
componentHandler.upgradeElement(component);
}).to.throw(Error);
});
});
describe('Format', () => {
it('should format input value when initialized', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": " ", "decimalSeparator": "," }',
'1234.5'
);
mount.appendChild(component);
componentHandler.upgradeElement(component);
expect(input.value).to.equal('1 234,5');
});
it('should not format input value if NaN', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": " ", "decimalSeparator": "," }',
'ABC1234.5'
);
mount.appendChild(component);
componentHandler.upgradeElement(component);
expect(input.value).to.equal('ABC1234.5');
});
it('should return the unformatted value', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": " ", "decimalSeparator": "," }',
'1234.5'
);
mount.appendChild(component);
componentHandler.upgradeElement(component);
expect(input.value).to.equal('1 234,5');
expect(component.MaterialExtFormatfield.getUnformattedValue()).to.equal('1234.5');
});
});
describe('Events', () => {
it('should strip group separator from value on focus, then format value on blur', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": ",", "decimalSeparator": "." }',
'1234567.8'
);
mount.appendChild(component);
componentHandler.upgradeElement(component);
dispatchEvent(input, 'focusin');
expect(input.value).to.equal('1234567.8');
dispatchEvent(input, 'focusout');
expect(input.value).to.equal('1,234,567.8');
});
it('should not alter value if input is readonly', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": " ", "decimalSeparator": "," }',
'1234.5'
);
mount.appendChild(component);
input.setAttribute('readonly', '');
componentHandler.upgradeElement(component);
dispatchEvent(input, 'focusin');
expect(input.value).to.equal('1 234,5');
dispatchEvent(input, 'foxusout');
expect(input.value).to.equal('1 234,5');
});
it('should not alter value if input is disabled', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": ",", "decimalSeparator": "." }',
'1234.5'
);
mount.appendChild(component);
input.setAttribute('disabled', '');
componentHandler.upgradeElement(component);
dispatchEvent(input, 'focusin');
expect(input.value).to.equal('1,234.5');
dispatchEvent(input, 'focusout');
expect(input.value).to.equal('1,234.5');
});
it('should cancel text selection when input is clicked', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": ",", "decimalSeparator": "." }',
'1234.5'
);
mount.appendChild(component);
componentHandler.upgradeElement(component);
const spy = sinon.spy(input, 'select');
dispatchEvent(input, 'focusin');
dispatchEvent(input, 'click');
sinon.restore(spy);
assert.isFalse(spy.called, 'Expected click to cancel select');
});
it('should call input.select() after a 200ms delay', () => {
const {component, input} = createSingleLineTextfield(
'{"locales": "nb-NO", "groupSeparator": ",", "decimalSeparator": "." }',
'1234.5'
);
mount.appendChild(component);
componentHandler.upgradeElement(component);
const clock = sinon.useFakeTimers();
const spy = sinon.spy(input, 'select');
dispatchEvent(input, 'focusin');
clock.tick(201);
sinon.restore(spy);
clock.restore();
assert.isTrue(spy.called, 'Expected select to be called after 200ms');
});
});
function dispatchEvent(target, eventName) {
target.dispatchEvent(new Event(eventName, {
bubbles: true,
cancelable: true,
view: window
}));
}
function createSingleLineTextfield(opts, value='') {
const component = document.createElement('div');
const input = document.createElement('input');
const label = document.createElement('label');
const errorMessage = document.createElement('span');
component.className = 'mdl-textfield mdl-js-textfield mdlext-js-formatfield';
input.className = 'mdl-textfield__input';
input.pattern = '[0-9]';
input.id = 'testInput';
input.value = value;
label.for = input.id;
label.className = 'mdl-textfield__label';
label.text = 'Number';
errorMessage.className = 'mdl-textfield__error';
errorMessage.text = 'Positive number only.';
component.appendChild(input);
component.appendChild(label);
component.appendChild(errorMessage);
if(opts) {
component.setAttribute('data-formatfield-options', opts);
}
return {component: component, input: input};
}
});