Skip to content

Commit 0b65bdb

Browse files
committed
test: unit test for fieldSpectrum
1 parent bb9410d commit 0b65bdb

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

src/fields/fieldDateTime.vue

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
4949
ready() {
5050
if ($.fn.datetimepicker) {
51-
let self = this;
5251
$(this.$el).datetimepicker(defaults(this.schema.dateTimePickerOptions || {}, {
5352
format: inputFormat
5453
}));

src/fields/fieldSlider.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
data() {
1414
return {
1515
slider: null
16-
}
16+
};
1717
},
1818
1919
watch: {

src/fields/fieldSpectrum.vue

+22-2
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@
99
export default {
1010
mixins: [ abstractField ],
1111
12+
data() {
13+
return {
14+
picker: null
15+
};
16+
},
17+
1218
watch: {
1319
model() {
1420
if ($.fn.spectrum) {
15-
$(this.$el).spectrum("set", this.value);
21+
this.picker.spectrum("set", this.value);
1622
}
23+
},
24+
25+
disabled(val) {
26+
if (val)
27+
this.picker.spectrum("disable");
28+
else
29+
this.picker.spectrum("enable");
1730
}
1831
},
1932
2033
ready() {
2134
if ($.fn.spectrum) {
22-
$(this.$el).spectrum("destroy").spectrum(defaults(this.schema.colorOptions || {}, {
35+
this.picker = $(this.$el).spectrum("destroy").spectrum(defaults(this.schema.colorOptions || {}, {
2336
showInput: true,
2437
showAlpha: true,
2538
disabled: this.schema.disabled,
@@ -29,9 +42,16 @@
2942
this.value = color ? color.toString() : null;
3043
}
3144
}));
45+
this.picker.spectrum("set", this.value);
46+
3247
} else {
3348
console.warn("Spectrum color library is missing. Please download from http://bgrins.github.io/spectrum/ and load the script and CSS in the HTML head section!");
3449
}
50+
},
51+
52+
beforeDestroy() {
53+
if (this.picker)
54+
this.picker.spectrum("destroy");
3555
}
3656
3757
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "chai";
2-
import { createVueField, trigger } from "../util";
2+
import { createVueField } from "../util";
33
import moment from "moment";
44

55
import Vue from "vue";

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "chai";
2-
import { createVueField } from "../util";
2+
import { createVueField, trigger } from "../util";
33

44
import Vue from "vue";
55
import FieldSpectrum from "src/fields/fieldSpectrum.vue";
@@ -38,10 +38,10 @@ describe("fieldSpectrum.vue", () => {
3838
//expect(input.classList.contains("form-control")).to.be.true;
3939
expect(input.disabled).to.be.false;
4040
});
41-
/* TODO
41+
4242
it("should contain the value", (done) => {
4343
vm.$nextTick( () => {
44-
expect(input.value).to.be.equal("#ff8822");
44+
expect(field.picker.spectrum("get").toHexString()).to.be.equal("#ff8822");
4545
done();
4646
});
4747
});
@@ -50,29 +50,31 @@ describe("fieldSpectrum.vue", () => {
5050
field.disabled = true;
5151
vm.$nextTick( () => {
5252
expect(input.disabled).to.be.true;
53+
expect(el.querySelectorAll(".sp-disabled").length).to.be.equal(1);
54+
field.disabled = false;
5355
done();
5456
});
5557
});
5658

5759
it("input value should be the model value after changed", (done) => {
58-
model.color = "#ffff00";
60+
field.model = { color: "#ffff00" };
5961
vm.$nextTick( () => {
60-
expect(input.value).to.be.equal("#ffff00");
62+
expect(field.picker.spectrum("get").toHexString()).to.be.equal("#ffff00");
6163
done();
6264
});
6365

6466
});
6567

6668
it("model value should be the input value if changed", (done) => {
67-
input.value = "#123456";
68-
trigger(input, "change");
69+
field.picker.spectrum("set", "#123456");
70+
trigger(document.querySelector(".sp-input"), "change");
6971

7072
vm.$nextTick( () => {
71-
expect(model.color).to.be.equal("#123456");
73+
expect(field.model.color).to.be.equal("#123456");
7274
done();
7375
});
7476

75-
});*/
77+
});
7678

7779
});
7880

0 commit comments

Comments
 (0)