-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexamples.js
122 lines (85 loc) · 5.48 KB
/
examples.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
describe('Test Examples', function () {
var ds;
var example = {};
beforeEach(function () {
ds = new JSData.DS();
var dsAdapter = new DSJsonApiAdapter.JsonApiAdapter({
queryTransform: queryTransform
});
ds.registerAdapter('jsonApi', dsAdapter, { default: true });
});
describe('oneToMany Example', function () {
// Load onetomany example
beforeEach(function () {
// Configure
example.config = examples.oneToMany.config(ds);
return loadJSON('/base/examples/oneToMany/oneToMany.json').then(function (json) {
example.json = json;
});
});
it('Should Deserialize the sample oneToMany Json', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
_this.requests[_this.requests.length-1].respond(200, { 'Content-Type': 'application/vnd.api+json' }, example.json);
}, 30);
return example.config.article.find(1).then(function (data) {
assert.isDefined(example.config.article.get(1), 'Expect article#1 to exist');
assert.isDefined(example.config.comment.get(1), 'Expect comment#1 to exist');
assert.isDefined(example.config.comment.get(2), 'Expect comment#2 to exist');
assert.isDefined(example.config.author.get(9), 'Expect author#9 to exist');
assert.isDefined(data[0][JSONAPIMETATAG].relationships.author, 'json api relationship for one "author", should exist');
assert.isDefined(data[0][JSONAPIMETATAG].relationships.comments, 'json api relationship for many "comments", should exist');
assert.isDefined(data[0][JSONAPIMETATAG].relationships.image, 'json api relationship for one "image", should exist');
assert.isDefined(data[0][JSONAPIMETATAG].relationships.tags, 'json api relationship for many "tags", should exist');
var article = example.config.article.get(1);
var author = example.config.author.get(9);
var comment1 = example.config.comment.get(1);
var comment2 = example.config.comment.get(2);
assert.equal(article.IsJsonApiReference, false, 'Expect article to be fully populated');
assert.equal(comment1.IsJsonApiReference, false, 'Expect comment#1 to be fully populated');
assert.equal(comment2.IsJsonApiReference, false, 'Expect comment#2 to be fully populated');
assert.equal(author.IsJsonApiReference, true , 'Expect author to be a reference object only');
});
});
});
describe('manyToMany Example', function () {
// Load manytomany example
beforeEach(function () {
// Configure
example.config = examples.manyToMany.config(ds);
return loadJSON('/base/examples/manyToMany/manyToMany.json').then(function (json) {
example.json = json;
});
});
it('Should Deserialize the sample manyToMany Json', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
_this.requests[_this.requests.length - 1].respond(200, { 'Content-Type': 'application/vnd.api+json' }, example.json);
}, 30);
return example.config.article.find(1).then(function (data) {
var article = example.config.article.get(1);
var person1 = example.config.person.get(1);
var person2 = example.config.person.get(2);
assert.isDefined(article, 'Expect article#1 to exist');
assert.isDefined(person1, 'Expect person#1 to exist');
assert.isDefined(person2, 'Expect person#2 to exist');
assert.equal(article.IsJsonApiReference, false, 'Expect article to be fully populated');
assert.equal(person1.IsJsonApiReference, false, 'Expect person#1 to be fully populated');
assert.equal(person2.IsJsonApiReference, false, 'Expect person#2 to be fully populated');
assert.isDefined(article.authors[0], 'Expect article.authors joining data to be populated');
assert.strictEqual(article.authors[0].article, article, 'Expect article_person person to be person 1');
assert.strictEqual(article.authors[0].author, person1, 'Expect article_person person to be person 1');
assert.strictEqual(article.authors[1].author, person2, 'Expect article_person person to be person 2');
assert.strictEqual(article.authors[0].author.articles[0].article, article, 'Expect article stored in joining table to be same article');
assert.isDefined(person1.articles[0], 'Expect person1.articles joining data to be populated');
assert.strictEqual(person1.articles[0].author, person1, 'Expect person1 author to be person1');
assert.strictEqual(person1.articles[0].article, article, 'Expect person1 article to be article1');
assert.isDefined(person2.articles[0], 'Expect person2.articles joining data to be populated');
assert.strictEqual(person2.articles[0].author, person2, 'Expect person2 author to be person 2');
assert.strictEqual(person2.articles[0].article, article, 'Expect person2 article to be article1');
});
});
});
});