Skip to content

Commit 1e24a10

Browse files
committed
Ref #4 - Unit test for create serialization
1 parent f0c9f6a commit 1e24a10

File tree

7 files changed

+112
-5
lines changed

7 files changed

+112
-5
lines changed

karma.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function (config) {
1616
{ pattern: 'test/api/**/*.json', watched: true, served: true, included: false }
1717
],
1818
proxies: {
19-
"/api/": "/base/test/api/"
19+
"/api/": "/base/test/api"
2020
},
2121
exclude: [
2222
],

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"karma-sourcemap-loader": "^0.3.7",
5151
"karma-webpack": "^2.0.2",
5252
"mocha": "^3.2.0",
53-
"sinon": "^1.17.7",
53+
"sinon": "^2.0.0-pre.5",
5454
"ts-loader": "=2.0.1",
5555
"ts-node": "^2.1.0",
5656
"typescript": "^2.1.6",

src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class JsonApiAdapter extends HttpAdapter{
5555
public jsonApiSerialize(resourceConfig:Mapper, data:any){
5656
// console.log('Serialize: ', resourceConfig, res);
5757

58-
console.log(data)
58+
console.log('lol', data)
5959

6060
return data;
6161
}
@@ -185,6 +185,7 @@ export class JsonApiAdapter extends HttpAdapter{
185185
}
186186

187187
public create(mapper: Mapper, props: any, opts?: any): Promise<any> {
188+
console.log('create')
188189
return super.create(mapper, props, opts).then(this.handleResponse(opts))
189190
}
190191

test/unit/crud/create.spec.ts

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as sinon from 'sinon';
2+
import { expect } from 'chai';
3+
import { store } from '../../ds';
4+
import * as Resources from '../../resources';
5+
6+
function respondJson(route: string, content?: any, statusCode?: number): any {
7+
let [method, endpoint] = route.split(' ')
8+
let reqPointer: any = {};
9+
10+
this.respondWith(method, `api/${endpoint}.json`, function(request: any) {
11+
let body = null;
12+
try { body = JSON.parse(request.requestBody); } catch (e) {}
13+
reqPointer.body = body;
14+
reqPointer.headers = request.requestHeaders;
15+
16+
request.respond(
17+
statusCode || 200,
18+
{ 'Content-Type': 'application/json' },
19+
JSON.stringify(content || null)
20+
);
21+
});
22+
23+
return reqPointer;
24+
}
25+
26+
describe('CREATE', () => {
27+
let requests:Array<any> = [];
28+
let server:any = null
29+
30+
beforeEach(() => {
31+
server = sinon.fakeServer.create();
32+
server.autoRespond = true;
33+
server.answer = respondJson;
34+
})
35+
36+
afterEach(() => {
37+
server.restore();
38+
})
39+
40+
describe('when creating a simple object with a belongsTo link', () => {
41+
const
42+
MAPPER_NAME:string = 'Article',
43+
MAPPER_NAME_LINK:string = 'User',
44+
RESPONSE_ID:string = '99633a09-9047-41bf-955f-4a8d72a38d58',
45+
RECORD:any = {
46+
title: 'hello',
47+
content: 'what\'s up ?',
48+
authorId: 'e81fea3d-6379-4137-8068-7d70a90a1a7c'
49+
},
50+
RESPONSE:any = {
51+
data: {
52+
id: RESPONSE_ID,
53+
type: MAPPER_NAME,
54+
attributes: {
55+
title: RECORD.title,
56+
content: RECORD.content
57+
}
58+
}
59+
}
60+
61+
let data:any = null;
62+
let req:any = null;
63+
64+
beforeEach(() => {
65+
req = server.answer('POST articles', RESPONSE);
66+
return store.create('Article', RECORD).then((_data) => {
67+
data = _data
68+
});
69+
})
70+
71+
afterEach(() => {
72+
data = req = null;
73+
})
74+
75+
it('the request should include the correct JSONApi structure', () => {
76+
expect(req.body).to.be.an('object');
77+
expect(req.body.data).to.be.an('object');
78+
expect(req.body.data.type).to.equal('Article');
79+
expect(req.body.data.attributes).to.be.an('object');
80+
});
81+
82+
it('the request should contain correct attributes', () => {
83+
expect(req.body.data.attributes).to.be.deep.equal({
84+
title: RECORD.title,
85+
content: RECORD.content
86+
});
87+
});
88+
89+
it('the request should also get a relationships object', () => {
90+
expect(req.body.data.relationships).to.be.an('object');
91+
expect(req.body.data.relationships.author).to.be.an('object');
92+
expect(req.body.data.relationships.author.data).to.be.deep.equal({
93+
type: 'User',
94+
id: RECORD.authorId
95+
});
96+
});
97+
98+
it('the response')
99+
100+
it('the record should be injected in the datastore', () => {
101+
let articles:Array<any> = store.getAll('Article');
102+
expect(articles).to.have.lengthOf(1);
103+
expect(articles[0].id).to.equal(RESPONSE_ID);
104+
expect(articles[0].title).to.equal(RECORD.title);
105+
expect(articles[0].content).to.equal(RECORD.content);
106+
});
107+
})
108+
});

test/unit/crud/destroy.spec.ts

Whitespace-only changes.

test/unit/crud/update.spec.ts

Whitespace-only changes.

test/unit/relations/hasMany.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ describe('relations/hasMany', () => {
4646
expect(groups).to.be.an('array').and.to.have.lengthOf(GROUP_LENGTH);
4747
})
4848

49-
xit('above fetch should be switched back to = groups, but .users fail if we put that')
50-
5149
it('should return the correct attributes (including the id)', () => {
5250
let [ group1, group2 ] = groups;
5351

0 commit comments

Comments
 (0)