Skip to content

Commit 4003625

Browse files
committed
Add the rest of the tests
1 parent ac928eb commit 4003625

File tree

2 files changed

+299
-0
lines changed

2 files changed

+299
-0
lines changed

Workshop/03. Unit-testing/data/data.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@
5555
"usernameToLower": "--f./s.f;q34/12q@",
5656
"id": "34a32b70-ae31-427b-96fa-0d15c672e0c6",
5757
"authKey": "34a32b70-ae31-427b-96fa-0d15c672e0c6GRTExYOZxrIZXBLPtsULkSOJ"
58+
},
59+
{
60+
"username": "asd",
61+
"passHash": "asd",
62+
"usernameToLower": "asd",
63+
"id": "769f8563-5410-429b-9a5e-e101bddde451",
64+
"authKey": "769f8563-5410-429b-9a5e-e101bddde451cMYoABrRflYTaFBIffMuAAvp"
65+
},
66+
{
67+
"username": "asdih",
68+
"passHash": "asda",
69+
"usernameToLower": "asdih",
70+
"id": "7414c688-932c-453f-85c3-dc32f847a498",
71+
"authKey": "7414c688-932c-453f-85c3-dc32f847a498JqosWmmuTObcVjsLbLXCtuRc"
72+
},
73+
{
74+
"username": "sada",
75+
"passHash": "asdasd",
76+
"usernameToLower": "sada",
77+
"id": "e2a5f1cd-4626-441a-ab61-932e0c6ed634",
78+
"authKey": "e2a5f1cd-4626-441a-ab61-932e0c6ed634jCSptvJCsjJHoMZveYgllAoy"
5879
}
5980
],
6081
"cookies": [
@@ -126,6 +147,33 @@
126147
"img": "https://dayinthelifeofapurpleminion.files.wordpress.com/2014/12/batman-exam.jpg",
127148
"shareDate": "2016-09-26T18:35:03.006Z",
128149
"id": "6f12561e-e5e5-4918-877c-7e938c127850"
150+
},
151+
{
152+
"text": "asd",
153+
"img": "http://asd.asd/asd.jpeg",
154+
"userId": "769f8563-5410-429b-9a5e-e101bddde451",
155+
"likes": 0,
156+
"dislikes": 0,
157+
"shareDate": "2016-09-30T18:40:09.908Z",
158+
"id": "38f8e9d3-c792-4503-be9d-d97c65fe11a8"
159+
},
160+
{
161+
"text": "asd",
162+
"img": "http://asd.asd/sad.jpg",
163+
"userId": "7414c688-932c-453f-85c3-dc32f847a498",
164+
"likes": 0,
165+
"dislikes": 0,
166+
"shareDate": "2016-09-30T18:49:05.525Z",
167+
"id": "dd2e78d3-64df-41fa-aff5-4854ffd3f133"
168+
},
169+
{
170+
"text": "asdsad",
171+
"img": "http://",
172+
"userId": "e2a5f1cd-4626-441a-ab61-932e0c6ed634",
173+
"likes": 0,
174+
"dislikes": 0,
175+
"shareDate": "2016-09-30T18:54:15.562Z",
176+
"id": "e34f9da5-f8c3-4845-bf97-727ba25605b4"
129177
}
130178
]
131179
}

Workshop/03. Unit-testing/public/test/tests.js

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ const user = {
77
username: 'SOME_USERNAME',
88
passHash: 'SOME_PASSHASH'
99
};
10+
const cookie = {
11+
img: 'SOME COOKIE IMG',
12+
text: 'SOME COOKIE TEXT',
13+
likes: 0,
14+
dislikes: 0,
15+
id: 42
16+
};
1017

1118
describe('Tests', function() {
1219
describe('Get cookies tests', function() {
@@ -225,6 +232,250 @@ describe('Tests', function() {
225232
.then(done, done);
226233
});
227234
});
235+
236+
describe('Logout tests', function() {
237+
beforeEach(function() {
238+
sinon.stub(requester, 'putJSON')
239+
.returns(new Promise((resolve, reject) => {
240+
resolve({
241+
result: {
242+
username: user.username,
243+
authKey: AUTH_KEY
244+
}
245+
});
246+
}));
247+
localStorage.clear();
248+
});
249+
afterEach(function() {
250+
requester.putJSON.restore();
251+
localStorage.clear();
252+
});
253+
254+
it('expect localStorage to have no username after logout', function(done) {
255+
dataService.login()
256+
.then(() => {
257+
return dataService.logout();
258+
})
259+
.then(() => {
260+
expect(localStorage.getItem('username')).to.be.null;
261+
})
262+
.then(done, done);
263+
});
264+
265+
it('expect localStorage to have no authKey after logout', function(done) {
266+
dataService.login()
267+
.then(() => {
268+
return dataService.logout();
269+
})
270+
.then(() => {
271+
expect(localStorage.getItem('authKey')).to.be.null;
272+
})
273+
.then(done, done);
274+
});
275+
});
276+
277+
describe('Add cookie tests', function() {
278+
let cookiesDB = [];
279+
280+
beforeEach(function() {
281+
sinon.stub(requester, 'postJSON', (route, cookie, options) => {
282+
return new Promise((resolve, reject) => {
283+
cookiesDB.push(cookie);
284+
// add userId to cookie?
285+
resolve(cookie);
286+
});
287+
});
288+
sinon.stub(requester, 'putJSON')
289+
.returns(new Promise((resolve, reject) => {
290+
resolve({
291+
result: {
292+
username: user.username,
293+
authKey: AUTH_KEY
294+
}
295+
});
296+
}));
297+
localStorage.clear();
298+
cookiesDB = [];
299+
});
300+
afterEach(function() {
301+
requester.postJSON.restore();
302+
requester.putJSON.restore();
303+
localStorage.clear();
304+
});
305+
306+
it('expect postJSON to be called for cookie', function(done) {
307+
dataService.login(user)
308+
.then(() => {
309+
return dataService.addCookie(cookie);
310+
})
311+
.then(() => {
312+
expect(requester.postJSON.calledOnce).to.be.true;
313+
})
314+
.then(done, done);
315+
});
316+
it('expect postJSON to be called with correct route', function(done) {
317+
dataService.login(user)
318+
.then(() => {
319+
return dataService.addCookie(cookie);
320+
})
321+
.then(() => {
322+
const actual = requester.postJSON
323+
.firstCall
324+
.args[0];
325+
expect(actual).to.equal('/api/cookies');
326+
})
327+
.then(done, done);
328+
});
329+
it('expect postJSON to be called with the cookie', function(done) {
330+
dataService.login(user)
331+
.then(() => {
332+
return dataService.addCookie(cookie);
333+
})
334+
.then(() => {
335+
const actual = requester.postJSON
336+
.firstCall
337+
.args[1];
338+
expect(actual).to.eql(cookie);
339+
})
340+
.then(done, done);
341+
});
342+
it('expect postJSON to be called with authorization headers', function(done) {
343+
dataService.login(user)
344+
.then(() => {
345+
return dataService.addCookie(cookie);
346+
})
347+
.then(() => {
348+
const actual = requester.postJSON
349+
.firstCall
350+
.args[2];
351+
expect(actual.headers[HTTP_HEADER_KEY]).to.equal(AUTH_KEY);
352+
})
353+
.then(done, done);
354+
});
355+
it('expect cookie to be added to cookieDB', function(done) {
356+
dataService.login(user)
357+
.then(() => {
358+
return dataService.addCookie(cookie);
359+
})
360+
.then(() => {
361+
expect(cookiesDB).to.eql([cookie]);
362+
})
363+
.then(done, done);
364+
});
365+
});
366+
367+
describe('Rate cookie tests', function() {
368+
let cookiesDB = [];
369+
370+
beforeEach(function() {
371+
sinon.stub(requester, 'putJSON', (route, type, options) => {
372+
return new Promise((resolve, reject) => {
373+
if(route === '/api/auth') {
374+
resolve({
375+
result: {
376+
username: user.username,
377+
authKey: AUTH_KEY
378+
}
379+
});
380+
return;
381+
};
382+
383+
const cookieId = +route.split('/')[3];
384+
const cookieToRate = cookiesDB.find((c) => c.id === cookieId);
385+
386+
if(type.type.type === 'like') {
387+
cookieToRate.likes += 1;
388+
}
389+
else if(type.type.type === 'dislike') {
390+
cookieToRate.dislikes += 1;
391+
}
392+
393+
resolve(cookieToRate);
394+
});
395+
});
396+
localStorage.clear();
397+
cookiesDB = [cookie];
398+
});
399+
afterEach(function() {
400+
requester.putJSON.restore();
401+
localStorage.clear();
402+
});
403+
404+
it('expect putJSON to be called for rating', function(done) {
405+
dataService.login(user)
406+
.then(() => {
407+
return dataService.rateCookie(cookie.id, {type:'like'});
408+
})
409+
.then(() => {
410+
expect(requester.putJSON.calledTwice).to.be.true;
411+
})
412+
.then(done, done);
413+
});
414+
it('expect cookie rating to be done with correct route', function(done) {
415+
dataService.login(user)
416+
.then(() => {
417+
return dataService.rateCookie(cookie.id, {type:'like'});
418+
})
419+
.then(() => {
420+
const actual = requester.putJSON
421+
.secondCall
422+
.args[0];
423+
expect(actual).to.equal(`/api/cookies/${cookie.id}`);
424+
})
425+
.then(done, done);
426+
});
427+
it('expect cookie rating type to be the used type of rating', function(done) {
428+
dataService.login(user)
429+
.then(() => {
430+
return dataService.rateCookie(cookie.id, {type:'like'});
431+
})
432+
.then(() => {
433+
const actual = requester.putJSON
434+
.secondCall
435+
.args[1];
436+
expect(actual.type.type).to.equal('like');
437+
})
438+
.then(done, done);
439+
});
440+
it('expect cookie rating to be called for the given cookie id', function(done) {
441+
dataService.login(user)
442+
.then(() => {
443+
return dataService.rateCookie(cookie.id, {type:'like'});
444+
})
445+
.then(() => {
446+
const actual = requester.putJSON
447+
.secondCall
448+
.args[2];
449+
expect(actual.headers[HTTP_HEADER_KEY]).to.equal(AUTH_KEY);
450+
})
451+
.then(done, done);
452+
});
453+
454+
it('expect liking a cookie to increment likes value of cookie', function(done) {
455+
let previousLikes = cookiesDB[0].likes;
456+
457+
dataService.login(user)
458+
.then(() => {
459+
return dataService.rateCookie(cookie.id, {type:'like'});
460+
})
461+
.then(() => {
462+
expect(cookiesDB[0].likes).to.equal(previousLikes + 1);
463+
})
464+
.then(done, done);
465+
});
466+
it('expect disliking a cookie to increment dislikes value of cookie', function(done) {
467+
let previousDislikes = cookiesDB[0].dislikes;
468+
469+
dataService.login(user)
470+
.then(() => {
471+
return dataService.rateCookie(cookie.id, {type:'dislike'});
472+
})
473+
.then(() => {
474+
expect(cookiesDB[0].dislikes).to.equal(previousDislikes + 1);
475+
})
476+
.then(done, done);
477+
});
478+
});
228479
});
229480

230481
mocha.run();

0 commit comments

Comments
 (0)