Skip to content

Commit 59251a2

Browse files
committed
Lots of tests
1 parent 43ff378 commit 59251a2

File tree

10 files changed

+291
-6
lines changed

10 files changed

+291
-6
lines changed

src/common/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class Context {
7373
this.options = options;
7474

7575
this.database = options.database;
76-
this.debugMode = options.debug as boolean;
76+
this.debugMode = Boolean(options.debug);
7777
this.logger = new Logger(this.debugMode);
7878

7979
if (!options.database) {

test/integration/VuexORMApollo.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('VuexORMApollo', () => {
1717
__typename: 'post',
1818
id: 42,
1919
otherId: 13548,
20+
published: true,
2021
title: 'Example Post 5',
2122
content: 'Foo',
2223
comments: {
@@ -50,6 +51,7 @@ query Post($id: ID!) {
5051
content
5152
title
5253
otherId
54+
published
5355
user {
5456
id
5557
name
@@ -217,6 +219,7 @@ query Users {
217219
__typename: 'post',
218220
id: 42,
219221
otherId: 13548,
222+
published: true,
220223
title: 'Example post 1',
221224
content: 'Foo',
222225
comments: {
@@ -248,6 +251,7 @@ query Users {
248251
content: "Foo",
249252
id: 1,
250253
otherId: 9,
254+
published: true,
251255
title: "Example post 1",
252256
userId: 1,
253257
user: {
@@ -265,6 +269,7 @@ mutation CreatePost($post: PostInput!) {
265269
content
266270
title
267271
otherId
272+
published
268273
user {
269274
id
270275
name
@@ -361,6 +366,7 @@ mutation DeleteUser($id: ID!) {
361366
__typename: 'post',
362367
id: 1,
363368
otherId: 13548,
369+
published: true,
364370
title: 'Example Post 1',
365371
content: 'Foo',
366372
comments: {
@@ -389,6 +395,7 @@ mutation UpvotePost($captchaToken: String!, $id: ID!) {
389395
content
390396
title
391397
otherId
398+
published
392399
user {
393400
id
394401
name

test/support/mock-data.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ export class Post extends ORMModel {
4141
return {
4242
id: this.increment(null),
4343
content: this.string(''),
44-
title: this.string(''),
44+
title: this.attr(),
4545
userId: this.number(0),
4646
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
47+
published: this.boolean(true),
4748
user: this.belongsTo(User, 'userId'),
4849
comments: this.morphMany(Comment, 'subjectId', 'subjectType')
4950
};
@@ -88,6 +89,7 @@ export class Contract extends ORMModel {
8889
id: this.increment(),
8990
name: this.attr(''),
9091
displayName: this.attr(''),
92+
contractType: this.string(''),
9193
slug: this.attr(''),
9294

9395
contractOptions: this.belongsToMany(ContractOption, ContractContractOption, 'contractId',
@@ -125,7 +127,6 @@ export async function setupMockData() {
125127
{ model: ContractContractOption }
126128
]);
127129

128-
await User.insert({ data: { id: 1, name: 'Charlie Brown' }});
129130
await User.insert({ data: { id: 1, name: 'Charlie Brown' }});
130131
await User.insert({ data: { id: 2, name: 'Peppermint Patty' }});
131132
await Post.insert({ data: { id: 1, otherId: 9, userId: 1, title: 'Example post 1', content: 'Foo' }});

test/unit/Action.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Model from 'app/orm/model';
2+
import { setupMockData, User, Video, Post, Comment, ContractContractOption, Contract, ContractOption } from 'test/support/mock-data';
3+
import Context from "app/common/context";
4+
import Action from "app/actions/action";
5+
6+
let store;
7+
let vuexOrmApollo;
8+
let context;
9+
10+
beforeEach(async () => {
11+
[store, vuexOrmApollo] = await setupMockData();
12+
context = Context.getInstance();
13+
});
14+
15+
describe('Action', () => {
16+
describe('.getModelFromState', () => {
17+
it('returns the model', () => {
18+
expect(Action.getModelFromState({ $name: 'post' })).toEqual(context.getModel('post'));
19+
});
20+
});
21+
22+
describe('.prepareArgs', () => {
23+
it('returns a args object without the id', () => {
24+
expect(Action.prepareArgs(undefined, 15)).toEqual({ id: 15 });
25+
expect(Action.prepareArgs({}, 42)).toEqual({ id: 42 });
26+
});
27+
28+
it('returns a args object with the id', () => {
29+
expect(Action.prepareArgs(undefined)).toEqual({});
30+
expect(Action.prepareArgs({ test: 15 })).toEqual({ test: 15 });
31+
});
32+
});
33+
34+
describe('.addRecordToArgs', () => {
35+
it('returns a args object with the record', () => {
36+
const model = context.getModel('post');
37+
const record = model.getRecordWithId(1);
38+
39+
expect(Action.addRecordToArgs({test: 2}, model, record)).toEqual({
40+
post: {
41+
id: 1,
42+
content: 'Foo',
43+
otherId: 9,
44+
published: true,
45+
title: 'Example post 1',
46+
user: { id: 1, name: 'Charlie Brown'},
47+
userId: 1
48+
},
49+
50+
test: 2
51+
});
52+
});
53+
});
54+
});

test/unit/Context.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Model from 'app/orm/model';
2+
import { setupMockData, User, Video, Post, Comment, ContractContractOption, Contract, ContractOption } from 'test/support/mock-data';
3+
import Context from "app/common/context";
4+
5+
let store;
6+
let vuexOrmApollo;
7+
let context;
8+
9+
beforeEach(async () => {
10+
[store, vuexOrmApollo] = await setupMockData();
11+
context = Context.getInstance();
12+
});
13+
14+
describe('Context', () => {
15+
describe('.debugMode', () => {
16+
it('to be false', () => {
17+
expect(context.debugMode).toEqual(false)
18+
});
19+
});
20+
21+
describe('.getModel', () => {
22+
it('returns a model', () => {
23+
expect(context.getModel('post')).toEqual(context.models.get('post'))
24+
});
25+
});
26+
});

test/unit/Model.spec.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,100 @@ describe('Model', () => {
4949
});
5050
});
5151
});
52+
53+
describe('.isFiedNumber', () => {
54+
it('returns true when the field is numeric', () => {
55+
model = context.getModel('post');
56+
expect(Model.isFieldNumber(model.fields.get('otherId'))).toEqual(true);
57+
expect(Model.isFieldNumber(model.fields.get('id'))).toEqual(true);
58+
});
59+
60+
it('returns false when the field is not numeric', () => {
61+
model = context.getModel('post');
62+
expect(Model.isFieldNumber(model.fields.get('title'))).toEqual(false);
63+
expect(Model.isFieldNumber(model.fields.get('user'))).toEqual(false);
64+
});
65+
});
66+
67+
describe('.isFieldAttribute', () => {
68+
it('returns true when the field is a attribute', () => {
69+
model = context.getModel('post');
70+
expect(Model.isFieldAttribute(model.fields.get('title'))).toEqual(true);
71+
expect(Model.isFieldAttribute(model.fields.get('id'))).toEqual(true);
72+
expect(Model.isFieldAttribute(model.fields.get('userId'))).toEqual(true);
73+
});
74+
75+
it('returns false when the field is a relation', () => {
76+
model = context.getModel('post');
77+
expect(Model.isFieldAttribute(model.fields.get('user'))).toEqual(false);
78+
});
79+
});
80+
81+
describe('.augment', () => {
82+
it('adds $isPersited to the fields', () => {
83+
// FIXME how to test that?
84+
});
85+
});
86+
87+
describe('.skipField', () => {
88+
it('returns true for a field which starts with a $', () => {
89+
const model = context.getModel('post');
90+
expect(model.skipField('$isPersisted')).toEqual(true);
91+
});
92+
93+
it('returns true for a field which is listed within skipFields', () => {
94+
const model = context.getModel('video');
95+
expect(model.skipField('ignoreMe')).toEqual(true);
96+
});
97+
98+
it('returns true for a field which is the foreignKey of a belongsTo or hasOne relation', () => {
99+
const model = context.getModel('post');
100+
expect(model.skipField('userId')).toEqual(true);
101+
});
102+
103+
it('returns false for normal fields', () => {
104+
const model = context.getModel('post');
105+
expect(model.skipField('id')).toEqual(false);
106+
expect(model.skipField('title')).toEqual(false);
107+
});
108+
});
109+
110+
describe('.isTypeFieldOfPolymorphicRelation', () => {
111+
it('returns true for the type field of a polymorphic relation', () => {
112+
const model = context.getModel('comment');
113+
expect(model.isTypeFieldOfPolymorphicRelation('subjectType')).toEqual(true);
114+
});
115+
116+
it('returns false for a normal attribute which just ends with `Type`', () => {
117+
const model = context.getModel('contract');
118+
expect(model.isTypeFieldOfPolymorphicRelation('contractType')).toEqual(false);
119+
});
120+
});
121+
122+
describe('.getRecordWithId', () => {
123+
it('returns the record with the id of the model type', () => {
124+
const model = context.getModel('post');
125+
const expectedRecord = model.baseModel.query().withAllRecursive().where('id', 2).first();
126+
expect(model.getRecordWithId(2)).toEqual(expectedRecord);
127+
});
128+
});
129+
130+
describe('.shouldEagerLoadRelation', () => {
131+
it('returns true if field is a belongsTo or hasOne relation', () => {
132+
const model = context.getModel('post');
133+
expect(model.shouldEagerLoadRelation(model.fields.get('user'), context.getModel('user'))).toEqual(true);
134+
135+
// TODO test hasOne
136+
});
137+
138+
it('returns true if field is in the eagerLoad array', () => {
139+
const model = context.getModel('post');
140+
expect(model.shouldEagerLoadRelation(model.fields.get('comments'), context.getModel('comment'))).toEqual(true);
141+
});
142+
143+
it('returns false if field neither belongsTo/hasOne nor in the eagerLoad array', () => {
144+
const model = context.getModel('user');
145+
expect(model.shouldEagerLoadRelation(model.fields.get('comments'), context.getModel('comment'))).toEqual(false);
146+
});
147+
});
52148
});

test/unit/NameGenerator.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import NameGenerator from "app/graphql/name-generator";
2+
import Model from 'app/orm/model';
3+
import { setupMockData, User, Video, Post, Comment, ContractContractOption, Contract, ContractOption } from 'test/support/mock-data';
4+
import Context from "app/common/context";
5+
6+
let model;
7+
let store;
8+
let vuexOrmApollo;
9+
let context;
10+
11+
beforeEach(async () => {
12+
[store, vuexOrmApollo] = await setupMockData();
13+
context = Context.getInstance();
14+
model = context.getModel('post');
15+
});
16+
17+
describe('NameGenerator', () => {
18+
describe('.getNameForPersist', () => {
19+
it('returns a correct create mutation name', () => {
20+
expect(NameGenerator.getNameForPersist(model)).toEqual('createPost');
21+
});
22+
});
23+
24+
describe('.getNameForPush', () => {
25+
it('returns a correct update mutation name', () => {
26+
expect(NameGenerator.getNameForPush(model)).toEqual('updatePost');
27+
});
28+
});
29+
30+
describe('.getNameForDestroy', () => {
31+
it('returns a correct delete mutation name', () => {
32+
expect(NameGenerator.getNameForDestroy(model)).toEqual('deletePost');
33+
});
34+
});
35+
36+
describe('.getNameForFetch', () => {
37+
it('returns a correct fetch query name', () => {
38+
expect(NameGenerator.getNameForFetch(model, true)).toEqual('posts');
39+
expect(NameGenerator.getNameForFetch(model, false)).toEqual('post');
40+
expect(NameGenerator.getNameForFetch(model)).toEqual('post');
41+
});
42+
});
43+
});

test/unit/QueryBuilder.spec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('QueryBuilder', () => {
5656
});
5757
});
5858

59+
5960
describe('.buildRelationsQuery', () => {
6061
it('generates query fields for all relations', () => {
6162
const fields = QueryBuilder.buildRelationsQuery(context.getModel('post'));
@@ -81,7 +82,6 @@ query test {
8182
});
8283

8384

84-
8585
describe('.buildField', () => {
8686
it('generates query fields for all model fields and relations', () => {
8787
let query = QueryBuilder.buildField(context.getModel('user'), true, { age: 32 });
@@ -117,6 +117,7 @@ query Posts($title: String!) {
117117
content
118118
title
119119
otherId
120+
published
120121
user {
121122
id
122123
name
@@ -149,6 +150,7 @@ mutation CreatePost($post: PostInput!) {
149150
content
150151
title
151152
otherId
153+
published
152154
user {
153155
id
154156
name
@@ -180,6 +182,7 @@ mutation UpdatePost($id: ID!, $post: PostInput!) {
180182
content
181183
title
182184
otherId
185+
published
183186
user {
184187
id
185188
name
@@ -215,4 +218,33 @@ mutation DeleteUser($id: ID!) {
215218

216219
});
217220
});
221+
222+
223+
describe('.determineAttributeType', () => {
224+
it('throws a exception when the input is something unknown', () => {
225+
const model = context.getModel('post');
226+
expect(() => QueryBuilder.determineAttributeType(model, 'asdfsfa', undefined))
227+
.toThrowError("Can't find suitable graphql type for variable asdfsfa for model post");
228+
});
229+
230+
it('returns String for string typed fields', () => {
231+
const model = context.getModel('post');
232+
expect(QueryBuilder.determineAttributeType(model, 'title', 'Example')).toEqual('String');
233+
});
234+
235+
it('returns Int for number typed fields', () => {
236+
const model = context.getModel('post');
237+
expect(QueryBuilder.determineAttributeType(model, 'userId', 15)).toEqual('Int');
238+
});
239+
240+
it('returns Boolean for boolean typed fields', () => {
241+
const model = context.getModel('post');
242+
expect(QueryBuilder.determineAttributeType(model, 'published', true)).toEqual('Boolean');
243+
});
244+
245+
it('returns String for string typed values in generic fields', () => {
246+
const model = context.getModel('post');
247+
expect(QueryBuilder.determineAttributeType(model, 'generic', 'test')).toEqual('String');
248+
});
249+
});
218250
});

test/unit/Transformer.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Transformer from "../../src/graphql/transformer";
1+
import Transformer from "app/graphql/transformer";
22
import { setupMockData, User, Video, Post, Comment, ContractContractOption, Contract, ContractOption } from 'test/support/mock-data'
33
import Context from "app/common/context";
44

0 commit comments

Comments
 (0)