Skip to content

Commit 8f87dcb

Browse files
committed
Test refactorings
1 parent 83e7d24 commit 8f87dcb

File tree

6 files changed

+152
-369
lines changed

6 files changed

+152
-369
lines changed

test/integration/VuexORMApollo.spec.js

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,12 @@
1-
import {Model as ORMModel} from "@vuex-orm/core";
2-
import {createStore, sendWithMockFetch} from "../support/Helpers";
1+
import { setupMockData, User, Video, Post, Comment, ContractContractOption, Contract, ContractOption } from 'test/support/mock-data'
2+
import {sendWithMockFetch} from "../support/helpers";
33

44
let store;
55
let vuexOrmApollo;
66

7-
class User extends ORMModel {
8-
static entity = 'users';
9-
10-
static fields () {
11-
return {
12-
id: this.increment(0),
13-
name: this.string(''),
14-
posts: this.hasMany(Post, 'userId'),
15-
comments: this.hasMany(Comment, 'userId')
16-
};
17-
}
18-
}
19-
20-
class Video extends ORMModel {
21-
static entity = 'videos';
22-
static eagerLoad = ['comments'];
23-
static skipFields = ['ignoreMe'];
24-
25-
static fields () {
26-
return {
27-
id: this.increment(null),
28-
content: this.string(''),
29-
title: this.string(''),
30-
userId: this.number(0),
31-
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
32-
ignoreMe: this.string(''),
33-
user: this.belongsTo(User, 'userId'),
34-
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
35-
};
36-
}
37-
}
38-
39-
class Post extends ORMModel {
40-
static entity = 'posts';
41-
static eagerLoad = ['comments'];
42-
43-
static fields () {
44-
return {
45-
id: this.increment(null),
46-
content: this.string(''),
47-
title: this.string(''),
48-
userId: this.number(0),
49-
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
50-
user: this.belongsTo(User, 'userId'),
51-
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
52-
};
53-
}
54-
}
55-
56-
57-
class Comment extends ORMModel {
58-
static entity = 'comments';
59-
60-
static fields () {
61-
return {
62-
id: this.increment(0),
63-
content: this.string(''),
64-
userId: this.number(0),
65-
user: this.belongsTo(User, 'userId'),
66-
67-
subjectId: this.number(0),
68-
subjectType: this.string('')
69-
};
70-
}
71-
}
72-
737
describe('VuexORMApollo', () => {
748
beforeEach(async () => {
75-
[store, vuexOrmApollo] = createStore([{ model: User }, { model: Post }, { model: Video }, { model: Comment }]);
76-
77-
await User.insert({ data: { id: 1, name: 'Charlie Brown' }});
78-
await User.insert({ data: { id: 1, name: 'Charlie Brown' }});
79-
await User.insert({ data: { id: 2, name: 'Peppermint Patty' }});
80-
await Post.insert({ data: { id: 1, otherId: 9, userId: 1, title: 'Example post 1', content: 'Foo' }});
81-
await Post.insert({ data: { id: 2, otherId: 10, userId: 1, title: 'Example post 2', content: 'Bar' }});
82-
await Video.insert({ data: { id: 1, otherId: 11, userId: 1, title: 'Example video', content: 'Video' }});
83-
await Comment.insert({ data: { id: 1, userId: 1, subjectId: 1, subjectType: 'videos', content: 'Example comment 1' }});
84-
await Comment.insert({ data: { id: 2, userId: 2, subjectId: 1, subjectType: 'posts', content: 'Example comment 2' }});
85-
await Comment.insert({ data: { id: 3, userId: 2, subjectId: 2, subjectType: 'posts', content: 'Example comment 3' }});
9+
[store, vuexOrmApollo] = await setupMockData();
8610
});
8711

8812
describe('fetch', () => {
File renamed without changes.

test/support/mock-data.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import {Model as ORMModel} from "@vuex-orm/core";
2+
import {createStore} from "./helpers";
3+
4+
export class User extends ORMModel {
5+
static entity = 'users';
6+
7+
static fields () {
8+
return {
9+
id: this.increment(0),
10+
name: this.string(''),
11+
posts: this.hasMany(Post, 'userId'),
12+
comments: this.hasMany(Comment, 'userId')
13+
};
14+
}
15+
}
16+
17+
export class Video extends ORMModel {
18+
static entity = 'videos';
19+
static eagerLoad = ['comments'];
20+
static skipFields = ['ignoreMe'];
21+
22+
static fields () {
23+
return {
24+
id: this.increment(null),
25+
content: this.string(''),
26+
title: this.string(''),
27+
userId: this.number(0),
28+
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
29+
ignoreMe: this.string(''),
30+
user: this.belongsTo(User, 'userId'),
31+
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
32+
};
33+
}
34+
}
35+
36+
export class Post extends ORMModel {
37+
static entity = 'posts';
38+
static eagerLoad = ['comments'];
39+
40+
static fields () {
41+
return {
42+
id: this.increment(null),
43+
content: this.string(''),
44+
title: this.string(''),
45+
userId: this.number(0),
46+
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
47+
user: this.belongsTo(User, 'userId'),
48+
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
49+
};
50+
}
51+
}
52+
53+
54+
export class Comment extends ORMModel {
55+
static entity = 'comments';
56+
57+
static fields () {
58+
return {
59+
id: this.increment(0),
60+
content: this.string(''),
61+
userId: this.number(0),
62+
user: this.belongsTo(User, 'userId'),
63+
64+
subjectId: this.number(0),
65+
subjectType: this.string('')
66+
};
67+
}
68+
}
69+
70+
export class ContractContractOption extends ORMModel {
71+
static entity = 'contractContractOptions';
72+
73+
static primaryKey = ['contractId', 'contractOptionId'];
74+
75+
static fields () {
76+
return {
77+
contractId: this.attr(null),
78+
contractOptionId: this.attr(null),
79+
}
80+
}
81+
}
82+
83+
export class Contract extends ORMModel {
84+
static entity = 'contracts';
85+
86+
static fields () {
87+
return {
88+
id: this.increment(),
89+
name: this.attr(''),
90+
displayName: this.attr(''),
91+
slug: this.attr(''),
92+
93+
contractOptions: this.belongsToMany(ContractOption, ContractContractOption, 'contractId',
94+
'contractOptionId'),
95+
}
96+
}
97+
}
98+
99+
100+
export class ContractOption extends ORMModel {
101+
static entity = 'contractOptions';
102+
103+
static fields () {
104+
return {
105+
id: this.increment(),
106+
name: this.attr(''),
107+
description: this.attr(''),
108+
109+
contracts: this.belongsToMany(Contract, ContractContractOption, 'contractOptionId', 'contractId')
110+
}
111+
}
112+
}
113+
114+
115+
export async function setupMockData() {
116+
let store, vuexOrmApollo;
117+
118+
[store, vuexOrmApollo] = createStore([
119+
{ model: User },
120+
{ model: Post },
121+
{ model: Video },
122+
{ model: Comment },
123+
{ model: ContractOption },
124+
{ model: Contract },
125+
{ model: ContractContractOption }
126+
]);
127+
128+
await User.insert({ data: { id: 1, name: 'Charlie Brown' }});
129+
await User.insert({ data: { id: 1, name: 'Charlie Brown' }});
130+
await User.insert({ data: { id: 2, name: 'Peppermint Patty' }});
131+
await Post.insert({ data: { id: 1, otherId: 9, userId: 1, title: 'Example post 1', content: 'Foo' }});
132+
await Post.insert({ data: { id: 2, otherId: 10, userId: 1, title: 'Example post 2', content: 'Bar' }});
133+
await Video.insert({ data: { id: 1, otherId: 11, userId: 1, title: 'Example video', content: 'Video' }});
134+
await Comment.insert({ data: { id: 1, userId: 1, subjectId: 1, subjectType: 'videos', content: 'Example comment 1' }});
135+
await Comment.insert({ data: { id: 2, userId: 2, subjectId: 1, subjectType: 'posts', content: 'Example comment 2' }});
136+
await Comment.insert({ data: { id: 3, userId: 2, subjectId: 2, subjectType: 'posts', content: 'Example comment 3' }});
137+
138+
return [store, vuexOrmApollo];
139+
}

test/unit/Model.spec.js

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11
import Model from 'app/model';
2-
import { createStore } from 'test/support/Helpers';
3-
import { Model as ORMModel } from '@vuex-orm/core';
2+
import { setupMockData, User, Video, Post, Comment, ContractContractOption, Contract, ContractOption } from 'test/support/mock-data';
43

54
let model;
65
let store;
76
let vuexOrmApollo;
87

9-
class User extends ORMModel {
10-
static entity = 'users';
11-
12-
static fields () {
13-
return {
14-
id: this.increment(null),
15-
name: this.string(null),
16-
profile: this.hasOne(Profile, 'userId')
17-
};
18-
}
19-
}
20-
21-
class Profile extends ORMModel {
22-
static entity = 'profiles';
23-
24-
static fields () {
25-
return {
26-
id: this.increment(null),
27-
userId: this.number(null)
28-
};
29-
}
30-
}
31-
328
beforeEach(async () => {
33-
[store, vuexOrmApollo] = createStore([{ model: User }, { model: Profile }]);
34-
await Profile.insert({ data: { id: 1, userId: 1 }});
35-
await User.insert({ data: { id: 1, name: 'Foo Bar', profile: { id: 1 } }});
36-
9+
[store, vuexOrmApollo] = await setupMockData();
3710
model = vuexOrmApollo.context.getModel('user');
3811
});
3912

@@ -66,9 +39,10 @@ describe('Model', () => {
6639
it('returns a list of the models relations', () => {
6740
const relations = model.getRelations();
6841

69-
expect(relations.has('profile')).toEqual(true);
70-
expect(relations.get('profile')).toEqual({
71-
foreignKey: "userId", localKey: "id", model: User, "related": Profile
42+
expect(relations.has('posts')).toEqual(true);
43+
expect(relations.has('comments')).toEqual(true);
44+
expect(relations.get('posts')).toEqual({
45+
foreignKey: "userId", localKey: "id", model: User, "related": Post
7246
});
7347
});
7448
});

0 commit comments

Comments
 (0)