-
Notifications
You must be signed in to change notification settings - Fork 440
/
helpers-test.js
131 lines (113 loc) · 3.32 KB
/
helpers-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
import { module, test } from 'qunit';
import { Model, Factory, belongsTo, hasMany, trait, association } from 'ember-cli-mirage';
import Server from 'ember-cli-mirage/server';
module('Integration | Server | Factories | helpers', function(hooks) {
hooks.afterEach(function() {
this.server.shutdown();
});
test('it creates associations with "association" helper in a dasherized factory', function(assert) {
this.server = new Server({
environment: 'test',
models: {
author: Model.extend({
blogPosts: hasMany()
}),
blogPost: Model.extend({
author: belongsTo()
})
},
factories: {
author: Factory.extend({
name: 'Sam'
}),
blogPost: Factory.extend({
title: 'Lorem ipsum',
author: association()
})
}
});
let blogPost = this.server.create('blog-post');
assert.ok(blogPost.author);
let { db } = this.server;
assert.equal(db.authors.length, 1);
assert.deepEqual(db.authors[0], {
id: '1',
name: 'Sam',
blogPostIds: ['1']
});
});
test('it creates associations with "association" helper combininig with traits', function(assert) {
this.server = new Server({
environment: 'test',
models: {
author: Model.extend({
posts: hasMany()
}),
category: Model.extend({
posts: hasMany('post', { inverse: 'kind' })
}),
post: Model.extend({
author: belongsTo(),
kind: belongsTo('category')
})
},
factories: {
author: Factory.extend({
name: 'Sam'
}),
category: Factory.extend({
name: 'awesome software'
}),
post: Factory.extend({
title: 'Lorem ipsum',
author: association(),
withCategory: trait({
kind: association()
})
})
}
});
let post = this.server.create('post', 'withCategory');
assert.ok(post.kind);
assert.ok(post.author);
let { db } = this.server;
assert.equal(db.posts.length, 1);
assert.deepEqual(db.posts[0], {
id: '1',
title: 'Lorem ipsum',
authorId: '1',
kindId: '1'
});
assert.equal(db.authors.length, 1);
assert.deepEqual(db.authors[0], {
id: '1',
name: 'Sam',
postIds: ['1']
});
assert.equal(db.categories.length, 1);
assert.deepEqual(db.categories[0], {
id: '1',
name: 'awesome software',
postIds: ['1']
});
});
test('it throws if using the association helper on a self-referential belongsTo relationship', function(assert) {
this.server = new Server({
environment: 'test',
models: {
page: Model.extend({
parentPage: belongsTo('page', { inverse: 'childPages' }),
childPages: hasMany('page', { inverse: 'parentPage' })
})
},
factories: {
page: Factory.extend({
parentPage: association()
})
}
});
assert.throws(() => {
this.server.create('page');
}, /You're using the association\(\) helper on your page factory for parentPage, which is a belongsTo self-referential relationship. You can't do this as it will lead to infinite recursion. You can move the helper inside of a trait and use it selectively./);
});
});