Skip to content

Commit 331a93a

Browse files
author
Larry Botha
committed
test($modified): wIP - add testt for initialising and changing values
1 parent 27007a5 commit 331a93a

File tree

1 file changed

+60
-12
lines changed

1 file changed

+60
-12
lines changed

test/library.spec.js

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('createForm', () => {
4444
describe('config', () => {
4545
it('does not throw when no initialValues provided', () => {
4646
const initialValues = undefined;
47-
const config = { initialValues };
47+
const config = {initialValues};
4848

4949
expect(() => createForm(config)).not.toThrow();
5050
});
@@ -70,22 +70,18 @@ describe('createForm', () => {
7070
});
7171

7272
it('should match the shape of validationSchema', () => {
73-
instance = getInstance({
73+
const instance = getInstance({
7474
initialValues: {
7575
name: '',
76-
address: {
77-
street: '',
78-
city: '',
79-
country: ''
80-
},
76+
address: {street: '', city: '', country: ''},
8177
},
8278
validationSchema: yup.object().shape({
8379
name: yup.string().required(),
8480
address: yup.object().shape({
8581
street: yup.string().required(),
86-
city: yup.string().required()
87-
})
88-
})
82+
city: yup.string().required(),
83+
}),
84+
}),
8985
});
9086

9187
subscribeOnce(instance.errors).then((errors) => {
@@ -111,6 +107,58 @@ describe('createForm', () => {
111107
});
112108
});
113109

110+
describe('$modified', () => {
111+
it('returns an observable with a subscribe method', () => {
112+
expect(instance.modified.subscribe).toBeDefined();
113+
});
114+
115+
it('is false for initialized values', async () => {
116+
const instance = getInstance({
117+
initialValues: {
118+
name: '',
119+
address: {street: '', city: '', country: ''},
120+
xs: [{foo: 'bar'}],
121+
},
122+
});
123+
const $modified = await subscribeOnce(instance.modified);
124+
125+
expect($modified.name).toBe(false);
126+
expect($modified.address).toBe(false);
127+
expect($modified.xs).toBe(false);
128+
});
129+
130+
it.only('is true for changed values', async () => {
131+
const name = 'foo';
132+
const street = 'bar';
133+
const xFoo = 'baz';
134+
const nameEvent = {target: {name: 'name', value: name}};
135+
const streetEvent = {target: {name: 'address.street', value: street}};
136+
const xEvent = {target: {name: 'xs[0].foo', value: xFoo}};
137+
const instance = getInstance({
138+
initialValues: {
139+
name: '',
140+
address: {street: '', city: '', country: ''},
141+
xs: [{foo: 'bar'}],
142+
},
143+
});
144+
let $modified;
145+
146+
await instance.handleChange(nameEvent);
147+
$modified = await subscribeOnce(instance.modified);
148+
149+
expect($modified.name).toBe(true);
150+
expect($modified.address).toBe(false);
151+
expect($modified.xs).toBe(false);
152+
153+
await instance.handleChange(streetEvent);
154+
$modified = await subscribeOnce(instance.modified);
155+
156+
expect($modified.name).toBe(true);
157+
expect($modified.address).toBe(true);
158+
expect($modified.xs).toBe(false);
159+
});
160+
});
161+
114162
describe('$isValid', () => {
115163
it('returns an observable with a subscribe method', () => {
116164
expect(instance.isValid.subscribe).toBeDefined();
@@ -130,7 +178,7 @@ describe('createForm', () => {
130178
await instance.form.set({
131179
name: '',
132180
email: '',
133-
country: ''
181+
country: '',
134182
});
135183

136184
instance
@@ -338,7 +386,7 @@ describe('createForm', () => {
338386
await instance.form.set({
339387
name: chance.name(),
340388
email: '',
341-
country: ''
389+
country: '',
342390
});
343391

344392
instance

0 commit comments

Comments
 (0)