Skip to content

Commit 492514d

Browse files
author
Duncan Lock
committed
Added tests for abstractField.getFieldID()
Added some tests for the getFieldID function - and tightened up it's slugification. - Tests that values are correctly slugified - Tests that schema properties are returned in the expected order of preference
1 parent 425faa2 commit 492514d

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

src/fields/abstractField.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,21 @@ export default {
169169
// If an ID's been explicitly set, use it unchanged
170170
return schema.id;
171171
} else {
172+
// Return the slugified version of either:
172173
return (schema.inputName || schema.label || schema.model)
174+
// NB: This is a very simple, conservative, slugify function,
175+
// avoiding extra dependencies.
173176
.toString()
174177
.trim()
175178
.toLowerCase()
176-
// Spaces to dashes
177-
.replace(/ /g, "-")
179+
// Spaces & underscores to dashes
180+
.replace(/ |_/g, "-")
178181
// Multiple dashes to one
179182
.replace(/-{2,}/g, "-")
180183
// Remove leading & trailing dashes
181184
.replace(/^-+|-+$/g, "")
182-
// Remove anything that isn't a (English/ASCII) letter or number.
183-
.replace(/([^a-zA-Z0-9\._-]+)/g, "")
185+
// Remove anything that isn't a (English/ASCII) letter, number or dash.
186+
.replace(/([^a-zA-Z0-9-]+)/g, "")
184187
;
185188
}
186189
}

test/unit/specs/fields/abstractField.spec.js

+60-30
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Vue.component("AbstractField", AbstractField);
1010
let el, vm, field;
1111

1212
function createField(test, schema = {}, model = null, disabled = false, options) {
13-
let elm = document.createElement("div");
13+
let elm = document.createElement("div");
1414

1515
vm = new Vue({
1616
// eslint-disable-next-line quotes
@@ -55,16 +55,16 @@ describe("abstractField.vue", function() {
5555
});
5656

5757
});
58-
58+
5959
describe("check nested value", () => {
6060
let schema = {
6161
type: "text",
6262
label: "Name",
6363
model: "user.name"
6464
};
65-
let model = {
65+
let model = {
6666
user: {
67-
name: "John Doe"
67+
name: "John Doe"
6868
}
6969
};
7070

@@ -90,7 +90,7 @@ describe("abstractField.vue", function() {
9090
label: "Name",
9191
model: "user.name.first"
9292
};
93-
let model = {
93+
let model = {
9494
user: {
9595
}
9696
};
@@ -169,7 +169,7 @@ describe("abstractField.vue", function() {
169169
expect(model.name).to.be.equal("!!Foo Bar!!");
170170
});
171171

172-
});
172+
});
173173

174174
describe("check schema onChanged event", () => {
175175
let schema = {
@@ -194,12 +194,12 @@ describe("abstractField.vue", function() {
194194
});
195195
});
196196

197-
});
197+
});
198198

199199
describe("check validateAfterChanged option", () => {
200200
let schema = {
201-
type: "text",
202-
label: "Name",
201+
type: "text",
202+
label: "Name",
203203
model: "name"
204204
};
205205

@@ -230,13 +230,13 @@ describe("abstractField.vue", function() {
230230
});
231231
});
232232

233-
});
233+
});
234234

235235
describe("check validate function with one validator", () => {
236236
let schema = {
237-
type: "text",
238-
label: "Name",
239-
model: "name",
237+
type: "text",
238+
label: "Name",
239+
model: "name",
240240
validator: sinon.spy()
241241
};
242242

@@ -253,13 +253,13 @@ describe("abstractField.vue", function() {
253253
expect(schema.validator.calledWith(field.value, schema, model)).to.be.true;
254254
});
255255

256-
});
256+
});
257257

258258
describe("check validate function if field is disabled", () => {
259259
let schema = {
260-
type: "text",
261-
label: "Name",
262-
model: "name",
260+
type: "text",
261+
label: "Name",
262+
model: "name",
263263
validator: sinon.spy()
264264
};
265265

@@ -275,13 +275,13 @@ describe("abstractField.vue", function() {
275275
expect(schema.validator.callCount).to.be.equal(0);
276276
});
277277

278-
});
278+
});
279279

280280
describe("check validate function if field is readonly", () => {
281281
let schema = {
282-
type: "text",
283-
label: "Name",
284-
model: "name",
282+
type: "text",
283+
label: "Name",
284+
model: "name",
285285
readonly: true,
286286
validator: sinon.spy()
287287
};
@@ -298,15 +298,15 @@ describe("abstractField.vue", function() {
298298
expect(schema.validator.callCount).to.be.equal(0);
299299
});
300300

301-
});
301+
});
302302

303303
describe("check validate function with validator array", () => {
304304
let spy1 = sinon.spy();
305305
let spy2 = sinon.spy();
306306
let schema = {
307-
type: "text",
308-
label: "Name",
309-
model: "name",
307+
type: "text",
308+
label: "Name",
309+
model: "name",
310310
validator: [spy1, spy2]
311311
};
312312

@@ -355,7 +355,7 @@ describe("abstractField.vue", function() {
355355
expect(schema.onValidated.calledWith(model, field.errors, schema)).to.be.true;
356356
});
357357

358-
});
358+
});
359359

360360
describe("check schema onValidated event", () => {
361361
let schema = {
@@ -369,7 +369,7 @@ describe("abstractField.vue", function() {
369369
let onValidated = sinon.spy();
370370

371371
beforeEach( () => {
372-
let elm = document.createElement("div");
372+
let elm = document.createElement("div");
373373

374374
vm = new Vue({
375375
// eslint-disable-next-line quotes
@@ -384,7 +384,7 @@ describe("abstractField.vue", function() {
384384
}).$mount(elm);
385385
el = vm.$el;
386386

387-
field = vm.$refs.field;
387+
field = vm.$refs.field;
388388
});
389389

390390
it("should return empty array", () => {
@@ -448,6 +448,36 @@ describe("abstractField.vue", function() {
448448
expect(field.errors[0]).to.be.equal("Validation error!");
449449
});
450450

451-
});
451+
});
452+
453+
describe("check getFieldID function", () => {
454+
455+
let schema = {
456+
type: "text",
457+
label: "First Name",
458+
model: "user__model",
459+
inputName: "input_name"
460+
};
461+
let model = {};
462+
463+
before( () => {
464+
createField(this, schema, model);
465+
});
466+
467+
it("should return slugified inputName, if available", () => {
468+
expect(field.getFieldID(schema)).to.be.equal("input-name");
469+
});
470+
471+
it("should return slugified label, if no inputName", () => {
472+
delete(schema.inputName);
473+
expect(field.getFieldID(schema)).to.be.equal("first-name");
474+
});
475+
476+
it("should return slugified model name, if no inputName or label", () => {
477+
delete(schema.label);
478+
expect(field.getFieldID(schema)).to.be.equal("user-model");
479+
});
480+
481+
});
452482

453-
});
483+
});

0 commit comments

Comments
 (0)