This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formCreateMixin.test.js
220 lines (185 loc) · 6.69 KB
/
formCreateMixin.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import Vue from "vue";
import { shallowMount } from "@vue/test-utils";
// Utilities
import { createForm, FormCreateMixin } from "../src";
describe("Form Create Mixin", () => {
let wrapper = null;
const Component = Vue.component("formComponent", {
template: "<div />",
});
const formName = "form";
const fields = {
email: "test@example.com",
password: "******",
};
const fieldChanges = {
email: "noone@example.com",
};
const updatedFields = {
...fields,
...fieldChanges,
};
// Setup the component with mixin before each test
const beforeHandler = () => {
wrapper = shallowMount(Component, {
mixins: [
// NOTE: Must spread setup values to avoid mutating by reference!
FormCreateMixin(formName, { ...fields }),
],
});
};
const afterHandler = () => {
wrapper.destroy();
};
beforeEach(beforeHandler);
afterEach(afterHandler);
it("should run mixin in component", () => {
// Should import successfully
expect(FormCreateMixin).toBeTruthy();
// Should have form data key from mixin options
expect(wrapper.vm).toHaveProperty(formName);
});
it("should populate fields from mixin options", () => {
// Should have field values from mixin options
expect(wrapper.vm[formName].fields).toEqual(fields);
// Should have matching initial valuse
expect(wrapper.vm[formName]._initial).toEqual(fields);
});
it("should populate initial flags", () => {
expect(wrapper.vm[formName].flags).toMatchObject({
changed: false,
disabled: false,
loading: false,
submitting: false,
});
});
describe("should handle form data methods", () => {
beforeEach(beforeHandler);
afterEach(afterHandler);
it("'should get form values", () => {
expect(wrapper.vm[formName].getValues()).toEqual(fields);
});
it("should update form values (but not initial)", () => {
wrapper.vm[formName].setValues({ ...fieldChanges }, false);
expect(wrapper.vm[formName].getValues()).toEqual(updatedFields);
expect(wrapper.vm[formName]._initial).toEqual(fields);
});
it("should update form values (and initial)", () => {
// Should set form values and update initial values
wrapper.vm[formName].setValues({ ...fieldChanges });
expect(wrapper.vm[formName].getValues()).toEqual(updatedFields);
expect(wrapper.vm[formName]._initial).toEqual(updatedFields);
// Should use new initial values in reset
wrapper.vm[formName].reset();
expect(wrapper.vm[formName].getValues()).toEqual(updatedFields);
});
it("should reset form values", () => {
wrapper.vm[formName].setValues({ ...fieldChanges }, false);
expect(wrapper.vm[formName].getValues()).toEqual(updatedFields);
wrapper.vm[formName].reset();
expect(wrapper.vm[formName].getValues()).toEqual(fields);
});
});
describe("should handle form flag methods", () => {
beforeEach(beforeHandler);
afterEach(afterHandler);
it("should not set invalid flags", () => {
wrapper.vm[formName].setFlag("locked", true);
expect(wrapper.vm[formName].flags.locked).toBeUndefined();
});
it("should set submitting flag", () => {
// Should set form submitting flag (both values)
wrapper.vm[formName].setSubmitting(true);
expect(wrapper.vm[formName].flags.submitting).toEqual(true);
wrapper.vm[formName].setSubmitting(false);
expect(wrapper.vm[formName].flags.submitting).toEqual(false);
});
it("should set loading flag", () => {
// Should set form loading flag (both values)
wrapper.vm[formName].setLoading(true);
expect(wrapper.vm[formName].flags.loading).toEqual(true);
wrapper.vm[formName].setLoading(false);
expect(wrapper.vm[formName].flags.loading).toEqual(false);
});
});
describe("should handle form computed flags", () => {
beforeEach(beforeHandler);
afterEach(afterHandler);
it("should calculate computed 'changed' flag", () => {
// Should start unchanged
expect(wrapper.vm[formName].flags.changed).toEqual(false);
// Should update form 'changed' key
wrapper.setData({ [formName]: { fields: { ...fieldChanges } } });
expect(wrapper.vm[formName].flags.changed).toEqual(true);
});
it("should track computed 'changed' flag through changes", () => {
// Should update form data and 'changed' key
wrapper.setData({ [formName]: { fields: { ...fieldChanges } } });
expect(wrapper.vm[formName].flags.changed).toEqual(true);
// Should reset form and 'changed' key
wrapper.vm[formName].reset();
expect(wrapper.vm[formName].flags.changed).toEqual(false);
});
});
describe("should use optional mixin options", () => {
let wrapperStatic = null;
beforeEach(() => {
const options = { calculateChanged: false, flags: { locked: true } };
wrapperStatic = shallowMount(Component, {
mixins: [
// NOTE: Must spread setup values to avoid mutating by reference!
FormCreateMixin(formName, { ...fields }, options),
],
});
});
afterEach(() => {
wrapperStatic.destroy();
});
it("should use custom flags", () => {
// Should have custom flag set by mixin
expect(wrapperStatic.vm[formName].flags).toHaveProperty("locked", true);
});
it("should set custom form flag", () => {
wrapperStatic.vm[formName].setFlag("locked", false);
expect(wrapperStatic.vm[formName].flags.locked).toEqual(false);
});
it("should avoid calculating 'changed' flag", () => {
wrapperStatic.setData({ [formName]: { fields: { ...fieldChanges } } });
expect(wrapperStatic.vm[formName].flags.changed).toBeUndefined();
});
});
});
describe("Form Create Function", () => {
let wrapper = null;
const formName = "form";
const fields = {
email: "test@example.com",
password: "******",
};
// Setup the component with mixin before each test
const beforeHandler = () => {
const Component = Vue.component("testComponent", {
template: "<div />",
});
wrapper = shallowMount(Component, {
// Form function with initial fields and custom flags
// NOTE: Must spread setup values to avoid mutating by reference!
data() {
return {
...createForm(formName, { ...fields }),
};
},
});
};
beforeEach(beforeHandler);
afterEach(() => {
wrapper.destroy();
});
// NOTE: Only this test is necessary, since the Mixin uses this function underneath!
it("should run mixin in component", () => {
// Should import successfully
expect(createForm).toBeTruthy();
// Should have form data key from mixin options
expect(wrapper.vm).toHaveProperty(formName);
});
});