Skip to content

Commit

Permalink
✅ test: schema test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 9, 2016
1 parent 4cd7bc6 commit 540eb63
Show file tree
Hide file tree
Showing 7 changed files with 681 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/fields/fieldSelectEx.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template lang="jade">
select.selectpicker(v-model="value", :disabled="disabled", :multiple="schema.multiSelect", :title="schema.placeholder", data-width="100%")
select.selectpicker(v-model="value", :disabled="disabled", :multiple="schema.multiSelect", :title="schema.placeholder", data-width="100%")
option(v-for="item in items", :value="getItemID(item)") {{ getItemName(item) }}
</template>

Expand Down
2 changes: 1 addition & 1 deletion src/utils/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {get, set, each} from "lodash";

module.exports.createDefaultObject = function (schema, obj = {}){
each(schema.fields, (field) => {
if (get(obj, field.model) === undefined)
if (get(obj, field.model) === undefined && field.default !== undefined)
set(obj, field.model, field.default);
});
return obj;
Expand Down
352 changes: 352 additions & 0 deletions test/unit/specs/fields/fieldChecklist.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
import { expect } from "chai";
import { createVueField, trigger } from "../util";

import Vue from "vue";
import FieldChecklist from "src/fields/fieldChecklist.vue";

Vue.component("FieldChecklist", FieldChecklist);

let el, vm, field;

function createField(schema = {}, model = null, disabled = false, options) {
[ el, vm, field ] = createVueField("fieldChecklist", schema, model, disabled, options);
}

describe("fieldChecklist.vue", () => {

describe("check listbox template", () => {

describe("check template with static string array", () => {
let schema = {
type: "checklist",
label: "Skills",
model: "skills",
listBox: true,
values: [
"HTML5",
"Javascript",
"CSS3",
"CoffeeScript",
"AngularJS",
"ReactJS",
"VueJS"
]
};
let model = { skills: ["Javascript", "VueJS"] };
let listbox;
let checkboxes;

function isChecked(idx) {
return(checkboxes[idx].checked);
}

before( () => {
createField(schema, model, false);
listbox = el.querySelector(".listbox");
checkboxes = listbox.querySelectorAll("input[type=checkbox]");
});

it("should contain a .listbox element", () => {
expect(field).to.be.exist;
expect(field.$el).to.be.exist;

expect(listbox).to.be.defined;
expect(listbox.classList.contains("form-control")).to.be.true;
});

it("should contain 7 items", () => {
expect(checkboxes.length).to.be.equal(7);
});

it("should checked the values", () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.true;
expect(isChecked(6)).to.be.true;
});

/*it("should set disabled", (done) => {
field.disabled = true;
vm.$nextTick( () => {
expect(listbox.disabled).to.be.true;
done();
});
});*/

it("listbox value should be the model value after changed", (done) => {
model.skills = ["ReactJS"];
vm.$nextTick( () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.false;
expect(isChecked(6)).to.be.false;
expect(isChecked(5)).to.be.true;
done();
});

});

it("model value should be the listbox value if changed", (done) => {
checkboxes[0].checked = true;
trigger(checkboxes[0], "change");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal(["ReactJS", "HTML5"]);
done();
});

});

});

describe("check static values with { id, name } objects", () => {
let schema = {
type: "checklist",
label: "Skills",
model: "skills",
listBox: true,
values: [
{ id: 1, name: "HTML5" },
{ id: 2, name: "Javascript" },
{ id: 3, name: "CSS3" },
{ id: 4, name: "CoffeeScript" },
{ id: 5, name: "AngularJS" },
{ id: 6, name: "ReactJS" },
{ id: 7, name: "VueJS" }
]
};
let model = { skills: [2, 7] };
let listbox;
let checkboxes;

function isChecked(idx) {
return(checkboxes[idx].checked);
}

before( () => {
createField(schema, model, false);
listbox = el.querySelector(".listbox");
checkboxes = listbox.querySelectorAll("input[type=checkbox]");
});

it("should contain items", () => {
expect(checkboxes.length).to.be.equal(7);
});

it("should checked the values", () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.true;
expect(isChecked(6)).to.be.true;
});

it("listbox value should be the model value after changed", (done) => {
model.skills = [3];
vm.$nextTick( () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.false;
expect(isChecked(2)).to.be.true;
done();
});

});

it("model value should be the listbox value if changed", (done) => {
checkboxes[0].checked = true;
trigger(checkboxes[0], "change");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal([3, 1]);
done();
});

});

});

describe("check function values", () => {
let schema = {
type: "checklist",
label: "Skills",
model: "skills",
listBox: true,
values() {
return [
{ id: 1, name: "HTML5" },
{ id: 2, name: "Javascript" },
{ id: 3, name: "CSS3" },
{ id: 4, name: "CoffeeScript" },
{ id: 5, name: "AngularJS" },
{ id: 6, name: "ReactJS" },
{ id: 7, name: "VueJS" }
]
}
};
let model = { skills: [2, 7] };
let listbox;
let checkboxes;

function isChecked(idx) {
return(checkboxes[idx].checked);
}

before( () => {
createField(schema, model, false);
listbox = el.querySelector(".listbox");
checkboxes = listbox.querySelectorAll("input[type=checkbox]");
});

it("should contain items", () => {
expect(checkboxes.length).to.be.equal(7);
});

it("should checked the values", () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.true;
expect(isChecked(6)).to.be.true;
});

it("listbox value should be the model value after changed", (done) => {
model.skills = [3];
vm.$nextTick( () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.false;
expect(isChecked(2)).to.be.true;
done();
});

});

it("model value should be the listbox value if changed", (done) => {
checkboxes[0].checked = true;
trigger(checkboxes[0], "change");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal([3, 1]);
done();
});

});

});

});

describe("check combobox template", () => {

describe("check template", () => {
let schema = {
type: "checklist",
label: "Skills",
model: "skills",
values: [
"HTML5",
"Javascript",
"CSS3",
"CoffeeScript",
"AngularJS",
"ReactJS",
"VueJS"
]
};
let model = { skills: ["Javascript", "VueJS"] };
let combobox;
let dropList;
let mainRow;
let checkboxes;

function isChecked(idx) {
return(checkboxes[idx].checked);
}

before( () => {
createField(schema, model, false);
combobox = el.querySelector(".combobox");
dropList = combobox.querySelector(".dropList");
mainRow = combobox.querySelector(".mainRow");
});

it("should contain a .combobox element", () => {
expect(field).to.be.exist;
expect(field.$el).to.be.exist;

expect(combobox).to.be.defined;
expect(combobox.classList.contains("form-control")).to.be.true;
});

it("should contain a .dropList element", () => {
expect(dropList).to.be.defined;
checkboxes = dropList.querySelectorAll("input[type=checkbox]");
expect(checkboxes).to.be.length(0); // collapsed
});

it("should contain a .mainRow element", () => {
expect(mainRow).to.be.defined;
expect(mainRow.querySelector(".info")).to.be.defined;
expect(mainRow.querySelector(".info").textContent).to.be.equal("2 selected");
expect(mainRow.querySelector(".arrow")).to.be.defined;
});

it("should contain 7 checkbox it expanded ", (done) => {
mainRow.click();
vm.$nextTick( () => {
checkboxes = dropList.querySelectorAll("input[type=checkbox]");
expect(checkboxes.length).to.be.equal(7);
done();
});
});

it("should checked the values", () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.true;
expect(isChecked(6)).to.be.true;
});

it("dropList value should be the model value after changed", (done) => {
model.skills = ["ReactJS"];
vm.$nextTick( () => {
expect(isChecked(0)).to.be.false;
expect(isChecked(1)).to.be.false;
expect(isChecked(6)).to.be.false;
expect(isChecked(5)).to.be.true;
done();
});

});

it("model value should be the dropList value if changed (add)", (done) => {
checkboxes[0].checked = true;
trigger(checkboxes[0], "change");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal(["ReactJS", "HTML5"]);
done();
});

});

it("model value should be the checklist value if changed (remove)", (done) => {
checkboxes[0].checked = false;
trigger(checkboxes[0], "change");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal(["ReactJS"]);
done();
});

});

it("model value should be the dropList value if changed (null)", (done) => {
model.skills = null;
checkboxes[0].checked = true;
trigger(checkboxes[0], "change");

vm.$nextTick( () => {
expect(model.skills).to.be.deep.equal(["HTML5"]);
done();
});

});

});

});

});
2 changes: 1 addition & 1 deletion test/unit/specs/fields/fieldDateTime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createField(schema = {}, model = null, disabled = false, options) {
[ el, vm, field ] = createVueField("fieldDateTime", schema, model, disabled, options);
}

describe.only("fieldDateTime.vue", () => {
describe("fieldDateTime.vue", () => {

describe("check template", () => {
let schema = {
Expand Down
Loading

0 comments on commit 540eb63

Please sign in to comment.