Skip to content

Commit 01d8895

Browse files
committed
Start to implement isDirty and isPersisted
1 parent 31b6fe9 commit 01d8895

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/context.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ export default class Context {
4949
*/
5050
private collectModels () {
5151
this.database.entities.forEach((entity: any) => {
52-
const model = new Model(entity.model as ORMModel, this);
52+
const model: Model = new Model(entity.model as ORMModel, this);
5353
this.models.set(model.singularName, model);
54+
55+
this.addFields(model);
5456
});
5557
}
58+
59+
60+
private addFields(model: Model) {
61+
// FIXME model.baseModel.fields.push('$isDirty');
62+
// FIXME model.baseModel.fields.push('$isPersisted');
63+
}
5664
}

test/integration/VuexORMApollo.spec.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,92 @@ mutation SignupUser($user: UserInput!, $captchaToken: String!) {
374374
`.trim() + "\n");
375375
});
376376
});
377+
378+
describe('$isDirty', () => {
379+
it('is false for newly created records', async () => {
380+
const user = store.dispatch('entities/users/create', { name: 'Snoopy' });
381+
expect(user.$isDirty).toBeFalsy();
382+
});
383+
384+
it('is true for changed but unsaved records', async () => {
385+
const user = store.dispatch('entities/users/create', { name: 'Snoopy' });
386+
user.name = 'Snoop Dog';
387+
expect(user.$isDirty).toBeTruthy();
388+
});
389+
390+
it('is false for changed and saved records', async () => {
391+
const user = store.dispatch('entities/users/create', { name: 'Snoopy' });
392+
user.name = 'Snoop Dog';
393+
store.dispatch('entities/users/update', { where: user.id, data: user });
394+
expect(user.$isDirty).toBeFalsy();
395+
});
396+
});
397+
398+
describe('$isPersisted', () => {
399+
it('is false for newly created records', async () => {
400+
const user = store.dispatch('entities/users/create', { name: 'Snoopy' });
401+
expect(user.$isPersisted).toBeFalsy();
402+
});
403+
404+
it('is true for persisted records', async () => {
405+
const response = {
406+
data: {
407+
createUser: {
408+
__typename: 'user',
409+
id: 1,
410+
name: 'Charlie Brown',
411+
posts: {
412+
__typename: 'post',
413+
nodes: []
414+
}
415+
}
416+
}
417+
};
418+
419+
await sendWithMockFetch(response, async () => {
420+
await store.dispatch('entities/users/persist', { id: 1 });
421+
});
422+
423+
const user = store.getters['entities/users/find'](1);
424+
expect(user.$isPersisted).toBeTruthy();
425+
});
426+
427+
it('is true for fetched records', async () => {
428+
const response = {
429+
data: {
430+
user: {
431+
__typename: 'user',
432+
id: 1,
433+
name: 'Johnny Imba',
434+
posts: {
435+
__typename: 'post',
436+
nodes: [
437+
{
438+
__typename: 'post',
439+
id: 1,
440+
userId: 1,
441+
title: 'Example Post 1',
442+
content: 'Foo'
443+
},
444+
{
445+
__typename: 'post',
446+
id: 2,
447+
userId: 1,
448+
title: 'Example Post 2',
449+
content: 'Bar'
450+
}
451+
]
452+
}
453+
}
454+
}
455+
};
456+
457+
await sendWithMockFetch(response, async () => {
458+
await store.dispatch('entities/users/fetch', { filter: { id: 1 } });
459+
});
460+
461+
const user = store.getters['entities/users/find'](1);
462+
expect(user.$isPersisted).toBeTruthy();
463+
});
464+
});
377465
});

0 commit comments

Comments
 (0)