@@ -2,19 +2,6 @@ mocha.setup('bdd');
22
33const { expect, assert} = chai ;
44
5- function checkRoute ( actual , expected , reject ) {
6- if ( actual !== expected ) {
7- reject ( new Error ( `Invalid route ${ actual } ` ) ) ;
8- }
9- }
10-
11- function checkUserData ( user , reject ) {
12- const prop = Object . keys ( user ) . sort ( ) ;
13- if ( prop . length !== 2 || prop [ 0 ] !== 'passHash' || prop [ 1 ] !== 'username' ) {
14- reject ( new Error ( `Invalid user data` ) ) ;
15- }
16- }
17-
185const AUTH_KEY = "SOME_AUTH_KEY" ;
196const user = {
207 username : 'SOME_USERNAME' ,
@@ -28,17 +15,26 @@ describe('Tests', function() {
2815 } ;
2916
3017 beforeEach ( function ( ) {
31- sinon . stub ( requester , 'getJSON' , ( route ) => {
32- return new Promise ( ( resolve , reject ) => {
33- checkRoute ( route , '/api/cookies' , reject ) ;
18+ sinon . stub ( requester , 'getJSON' )
19+ . returns ( new Promise ( ( resolve , reject ) => {
3420 resolve ( result ) ;
35- } ) ;
36- } ) ;
21+ } ) ) ;
3722 } ) ;
3823 afterEach ( function ( ) {
3924 requester . getJSON . restore ( ) ;
4025 } ) ;
4126
27+ it ( 'expect dataService.cookies() to make correct getJSON call' , function ( done ) {
28+ dataService . cookies ( )
29+ . then ( obj => {
30+ const actual = requester . getJSON
31+ . firstCall
32+ . args [ 0 ] ;
33+
34+ expect ( actual ) . to . equal ( '/api/cookies' ) ;
35+ } )
36+ . then ( done , done ) ;
37+ } ) ;
4238 it ( 'expect dataService.cookies() to return correct result' , function ( done ) {
4339 dataService . cookies ( )
4440 . then ( obj => {
@@ -50,58 +46,112 @@ describe('Tests', function() {
5046
5147 describe ( 'Register tests' , function ( ) {
5248 beforeEach ( function ( ) {
53- sinon . stub ( requester , 'postJSON' , ( route , user ) => {
54- return new Promise ( ( resolve , reject ) => {
55- checkRoute ( route , '/api/users' , reject ) ;
56- checkUserData ( user , reject ) ;
49+ sinon . stub ( requester , 'postJSON' )
50+ . returns ( new Promise ( ( resolve , reject ) => {
5751 resolve ( user ) ;
58- } ) ;
59- } ) ;
52+ } ) ) ;
6053 } ) ;
6154 afterEach ( function ( ) {
6255 requester . postJSON . restore ( ) ;
6356 } ) ;
6457
65- it ( 'Expect registering of user to return the user ' , function ( done ) {
58+ it ( 'expect dataService.register() to make correct postJSON call ' , function ( done ) {
6659 dataService . register ( user )
67- . then ( actual => {
68- expect ( actual ) . to . eql ( user ) ;
60+ . then ( ( ) => {
61+ const actual = requester . postJSON
62+ . firstCall
63+ . args [ 0 ] ;
64+ expect ( actual ) . to . equal ( '/api/users' ) ;
6965 } )
7066 . then ( done , done ) ;
7167 } ) ;
72- it ( 'Expect registering invalid data to fail' , function ( done ) {
73- dataService . register ( 42 )
68+ it ( 'expect dataService.register() to post correct user data' , function ( done ) {
69+ dataService . register ( user )
70+ . then ( ( ) => {
71+ const actual = requester . postJSON
72+ . firstCall
73+ . args [ 1 ] ;
74+ const prop = Object . keys ( actual ) . sort ( ) ;
75+ expect ( prop . length ) . to . equal ( 2 ) ;
76+ expect ( prop [ 0 ] ) . to . equal ( 'passHash' ) ;
77+ expect ( prop [ 1 ] ) . to . equal ( 'username' ) ;
78+ } )
79+ . then ( done , done ) ;
80+ } ) ;
81+ it ( 'expect dataService.register() to make correct postJSON call' , function ( done ) {
82+ dataService . register ( user )
83+ . then ( ( ) => {
84+ const actual = requester . postJSON
85+ . firstCall
86+ . args [ 0 ] ;
87+ expect ( actual ) . to . equal ( '/api/users' ) ;
88+ } )
89+ . then ( done , done ) ;
90+ } ) ;
91+ it ( 'expect dataService.register() to post correct user data' , function ( done ) {
92+ dataService . register ( user )
93+ . then ( ( ) => {
94+ const actual = requester . postJSON
95+ . firstCall
96+ . args [ 1 ] ;
97+ const prop = Object . keys ( actual ) . sort ( ) ;
98+ expect ( prop . length ) . to . equal ( 2 ) ;
99+ expect ( prop [ 0 ] ) . to . equal ( 'passHash' ) ;
100+ expect ( prop [ 1 ] ) . to . equal ( 'username' ) ;
101+ } )
102+ . then ( done , done ) ;
103+ } ) ;
104+
105+ it ( 'expect registering of user to return the user' , function ( done ) {
106+ dataService . register ( user )
74107 . then ( actual => {
75- done ( new Error ( 'User data is valid' ) ) ;
76- } ,
77- ( err ) => {
78- done ( ) ;
79- } ) ;
108+ expect ( actual ) . to . eql ( user ) ;
109+ } )
110+ . then ( done , done ) ;
80111 } ) ;
81112 } ) ;
82113
83114 describe ( 'Login tests' , function ( ) {
84115 beforeEach ( function ( ) {
85- sinon . stub ( requester , 'putJSON' , ( route , user ) => {
86- return new Promise ( ( resolve , reject ) => {
87- checkRoute ( route , '/api/auth' , reject ) ;
88- checkUserData ( user , reject ) ;
89-
116+ sinon . stub ( requester , 'putJSON' )
117+ . returns ( new Promise ( ( resolve , reject ) => {
90118 resolve ( {
91119 result : {
92120 username : user . username ,
93121 authKey : AUTH_KEY
94122 }
95123 } ) ;
96- } ) ;
97- } ) ;
124+ } ) ) ;
98125 localStorage . clear ( ) ;
99126 } ) ;
100127 afterEach ( function ( ) {
101128 requester . putJSON . restore ( ) ;
102129 localStorage . clear ( ) ;
103130 } ) ;
104131
132+ it ( 'expect dataService.login() to make correct putJSON call' , function ( done ) {
133+ dataService . login ( user )
134+ . then ( ( ) => {
135+ const actual = requester . putJSON
136+ . firstCall
137+ . args [ 0 ] ;
138+ expect ( actual ) . to . equal ( '/api/auth' ) ;
139+ } )
140+ . then ( done , done ) ;
141+ } ) ;
142+ it ( 'expect dataService.login() to put correct user data' , function ( done ) {
143+ dataService . login ( user )
144+ . then ( ( ) => {
145+ const actual = requester . putJSON
146+ . firstCall
147+ . args [ 1 ] ;
148+ const prop = Object . keys ( actual ) . sort ( ) ;
149+ expect ( prop . length ) . to . equal ( 2 ) ;
150+ expect ( prop [ 0 ] ) . to . equal ( 'passHash' ) ;
151+ expect ( prop [ 1 ] ) . to . equal ( 'username' ) ;
152+ } )
153+ . then ( done , done ) ;
154+ } ) ;
105155 it ( 'Expect login to login the right user and set him in localStorage' , function ( done ) {
106156 dataService . login ( user )
107157 . then ( ( ) => {
@@ -120,19 +170,15 @@ describe('Tests', function() {
120170
121171 describe ( 'Is loggedIn tests' , function ( ) {
122172 beforeEach ( function ( ) {
123- sinon . stub ( requester , 'putJSON' , ( route , user ) => {
124- return new Promise ( ( resolve , reject ) => {
125- checkRoute ( route , '/api/auth' , reject ) ;
126- checkUserData ( user , reject ) ;
127-
173+ sinon . stub ( requester , 'putJSON' )
174+ . returns ( new Promise ( ( resolve , reject ) => {
128175 resolve ( {
129176 result : {
130177 username : user . username ,
131178 authKey : AUTH_KEY
132179 }
133180 } ) ;
134- } ) ;
135- } ) ;
181+ } ) ) ;
136182 localStorage . clear ( ) ;
137183 } ) ;
138184 afterEach ( function ( ) {
@@ -149,7 +195,9 @@ describe('Tests', function() {
149195 } ) ;
150196 it ( 'expect to be logged in when we have logged in' , function ( done ) {
151197 dataService . login ( user )
152- . then ( ( ) => dataService . isLoggedIn ( ) )
198+ . then ( ( ) => {
199+ return dataService . isLoggedIn ( ) ;
200+ } )
153201 . then ( f => {
154202 expect ( f ) . to . be . true ;
155203 } )
0 commit comments