Skip to content

Commit 2a49542

Browse files
committed
Add tests for dirty states.
1 parent 9df2706 commit 2a49542

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

tests/js/element/dirty.test.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import test from "ava";
2+
import { getElement } from "../utils.js";
3+
4+
test("dirty class", (t) => {
5+
const html = "<input u:model='name' u:dirty.class='dirty' />";
6+
const element = getElement(html);
7+
8+
t.is(element.dirty.classes.length, 1);
9+
t.is(element.dirty.classes[0], "dirty");
10+
});
11+
12+
test("dirty multiple classes", (t) => {
13+
const html = "<input u:model='name' u:dirty.class='dirty another' />";
14+
const element = getElement(html);
15+
16+
t.is(element.dirty.classes.length, 2);
17+
t.is(element.dirty.classes[0], "dirty");
18+
t.is(element.dirty.classes[1], "another");
19+
});
20+
21+
test("dirty remove class", (t) => {
22+
const html = "<input u:model='name' u:dirty.class.remove='clean' />";
23+
const element = getElement(html);
24+
25+
t.is(element.dirty.removeClasses.length, 1);
26+
t.is(element.dirty.removeClasses[0], "clean");
27+
});
28+
29+
test("dirty multiple remove classes", (t) => {
30+
const html = "<input u:model='name' u:dirty.class.remove='clean great' />";
31+
const element = getElement(html);
32+
33+
t.is(element.dirty.removeClasses.length, 2);
34+
t.is(element.dirty.removeClasses[0], "clean");
35+
t.is(element.dirty.removeClasses[1], "great");
36+
});
37+
38+
test("dirty attr", (t) => {
39+
const html = "<input u:model='name' u:dirty.attr='disabled' />";
40+
const element = getElement(html);
41+
42+
t.is(element.dirty.attr, "disabled");
43+
});
44+
45+
test("dirty attr handleDirty", (t) => {
46+
const html = "<input u:model='name' u:dirty.attr='disabled' />";
47+
const element = getElement(html);
48+
49+
t.is(element.el.getAttribute("disabled"), null);
50+
element.handleDirty();
51+
t.is(element.el.getAttribute("disabled"), "disabled");
52+
});
53+
54+
test("dirty attr handleDirty revert", (t) => {
55+
const html =
56+
"<input u:model='name' u:dirty.attr='disabled' disabled='disabled' />";
57+
const element = getElement(html);
58+
59+
t.is(element.el.getAttribute("disabled"), "disabled");
60+
element.handleDirty(true);
61+
t.is(element.el.getAttribute("disabled"), null);
62+
});
63+
64+
test("dirty class handleDirty", (t) => {
65+
const html = "<input u:model='name' u:dirty.class='dirty' />";
66+
const element = getElement(html);
67+
68+
t.is(element.el.classList.length, 0);
69+
element.handleDirty();
70+
t.is(element.el.classList.length, 1);
71+
t.is(element.el.classList[0], "dirty");
72+
});
73+
74+
test("dirty multiple classes handleDirty", (t) => {
75+
const html = "<input u:model='name' u:dirty.class='dirty dirtier' />";
76+
const element = getElement(html);
77+
78+
t.is(element.el.classList.length, 0);
79+
element.handleDirty();
80+
t.is(element.el.classList.length, 2);
81+
t.is(element.el.classList[0], "dirty");
82+
t.is(element.el.classList[1], "dirtier");
83+
});
84+
85+
test("dirty class handleDirty revert", (t) => {
86+
const html = "<input u:model='name' u:dirty.class='dirty' class='dirty' />";
87+
const element = getElement(html);
88+
89+
t.is(element.el.classList.length, 1);
90+
t.is(element.el.classList[0], "dirty");
91+
element.handleDirty(true);
92+
t.is(element.el.classList.length, 0);
93+
});
94+
95+
test("dirty class handleDirty revert remove class attribute", (t) => {
96+
const html = "<input u:model='name' u:dirty.class='dirty' class='dirty' />";
97+
const element = getElement(html);
98+
99+
t.is(element.el.getAttribute("class"), "dirty");
100+
element.handleDirty(true);
101+
t.is(element.el.getAttribute("class"), null);
102+
});
103+
104+
test("dirty class.remove handleDirty", (t) => {
105+
const html =
106+
"<input u:model='name' u:dirty.class.remove='clean' class='clean' />";
107+
const element = getElement(html);
108+
109+
t.is(element.el.classList.length, 1);
110+
t.is(element.el.classList[0], "clean");
111+
element.handleDirty();
112+
t.is(element.el.classList.length, 0);
113+
});
114+
115+
test("dirty class.remove handleDirty revert", (t) => {
116+
const html = "<input u:model='name' u:dirty.class.remove='clean' class='' />";
117+
const element = getElement(html);
118+
119+
t.is(element.el.classList.length, 0);
120+
element.handleDirty(true);
121+
t.is(element.el.classList.length, 1);
122+
t.is(element.el.classList[0], "clean");
123+
});

0 commit comments

Comments
 (0)