|
| 1 | +import { expect } from "chai"; |
| 2 | +import { createVueField, trigger, checkAttribute } from "../util"; |
| 3 | + |
| 4 | +import Vue from "vue"; |
| 5 | +import fieldHtml5 from "src/fields/fieldHtml5.vue"; |
| 6 | + |
| 7 | +Vue.component("fieldHtml5", fieldHtml5); |
| 8 | + |
| 9 | +let el, vm, field; |
| 10 | + |
| 11 | +function createField(test, schema = {}, model = null, disabled = false, options) { |
| 12 | + [el, vm, field] = createVueField(test, "fieldHtml5", schema, model, disabled, options); |
| 13 | +} |
| 14 | + |
| 15 | +describe("fieldHtml5.vue", function() { |
| 16 | + |
| 17 | + describe("check template", () => { |
| 18 | + let schema = { |
| 19 | + type: "html5", |
| 20 | + inputType: "text", |
| 21 | + label: "Name", |
| 22 | + model: "name", |
| 23 | + autocomplete: "off", |
| 24 | + placeholder: "Field placeholder", |
| 25 | + readonly: false |
| 26 | + }; |
| 27 | + let model = { name: "John Doe" }; |
| 28 | + let input; |
| 29 | + |
| 30 | + before(() => { |
| 31 | + createField(this, schema, model, false); |
| 32 | + input = el.getElementsByTagName("input")[0]; |
| 33 | + }); |
| 34 | + |
| 35 | + it("should contain an input text element", () => { |
| 36 | + expect(field).to.be.exist; |
| 37 | + expect(field.$el).to.be.exist; |
| 38 | + |
| 39 | + expect(input).to.be.defined; |
| 40 | + expect(input.type).to.be.equal("text"); |
| 41 | + expect(input.classList.contains("form-control")).to.be.true; |
| 42 | + }); |
| 43 | + |
| 44 | + it("should contain the value", (done) => { |
| 45 | + vm.$nextTick(() => { |
| 46 | + expect(input.value).to.be.equal("John Doe"); |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + let inputTypes = new Map([ |
| 52 | + ["text", ["autocomplete", "disabled", "placeholder", "readonly"]], |
| 53 | + ["password", ["autocomplete", "disabled", "placeholder", "readonly"]], |
| 54 | + ["checkbox", ["autocomplete", "disabled"]], |
| 55 | + // ["radio", [] ], |
| 56 | + // ["button", [] ], |
| 57 | + // ["submit", [] ], |
| 58 | + // ["reset", [] ], |
| 59 | + // ["file", [] ], |
| 60 | + // ["hidden", [] ], |
| 61 | + // ["image", [] ], |
| 62 | + // ["datetime", ], |
| 63 | + // ["datetime", ], |
| 64 | + // ["date", ], |
| 65 | + // ["month", ], |
| 66 | + // ["time", ], |
| 67 | + // ["week", ], |
| 68 | + ["number", ["autocomplete", "disabled", "placeholder", "readonly"]], |
| 69 | + // ["range", ["autocomplete"]], |
| 70 | + ["email", ["autocomplete", "disabled", "placeholder", "readonly"]], |
| 71 | + ["url", ["autocomplete", "disabled", "placeholder", "readonly"]], |
| 72 | + // ["search", ], |
| 73 | + ["tel", ["autocomplete", "disabled", "placeholder", "readonly"]], |
| 74 | + ["color", ["autocomplete"]] |
| 75 | + ]); |
| 76 | + for (let [inputType, attributes] of inputTypes) { |
| 77 | + |
| 78 | + describe("change type of input", () => { |
| 79 | + |
| 80 | + it("should become a " + inputType, function(done) { |
| 81 | + field.schema.inputType = inputType; |
| 82 | + vm.$nextTick(() => { |
| 83 | + expect(input.type).to.be.equal(inputType); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + |
| 87 | + }); |
| 88 | + |
| 89 | + describe("check optional attribute", () => { |
| 90 | + |
| 91 | + attributes.forEach(function(name) { |
| 92 | + it("should set " + name, function(done) { |
| 93 | + checkAttribute(name, vm, input, field, schema, done); |
| 94 | + }); |
| 95 | + |
| 96 | + }); |
| 97 | + |
| 98 | + }); |
| 99 | + |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + it("input value should be the model value after changed", (done) => { |
| 104 | + model.name = "Jane Doe"; |
| 105 | + vm.$nextTick(() => { |
| 106 | + expect(input.value).to.be.equal("Jane Doe"); |
| 107 | + done(); |
| 108 | + }); |
| 109 | + |
| 110 | + }); |
| 111 | + |
| 112 | + it("model value should be the input value if changed", (done) => { |
| 113 | + input.value = "John Smith"; |
| 114 | + trigger(input, "input"); |
| 115 | + |
| 116 | + vm.$nextTick(() => { |
| 117 | + expect(model.name).to.be.equal("John Smith"); |
| 118 | + done(); |
| 119 | + }); |
| 120 | + |
| 121 | + }); |
| 122 | + |
| 123 | + }); |
| 124 | + |
| 125 | +}); |
0 commit comments