@@ -30,6 +30,7 @@ describe('plugin:leetcode', function() {
30
30
before ( function ( ) {
31
31
log . init ( ) ;
32
32
config . init ( ) ;
33
+ plugin . init ( ) ;
33
34
34
35
session . getUser = function ( ) {
35
36
return USER ;
@@ -97,6 +98,47 @@ describe('plugin:leetcode', function() {
97
98
} ) ; // #login
98
99
99
100
describe ( '#getProblems' , function ( ) {
101
+ it ( 'should ok' , function ( done ) {
102
+ nock ( 'https://leetcode.com' )
103
+ . get ( '/api/problems/algorithms/' )
104
+ . replyWithFile ( 200 , './test/mock/problems.json.20160911' ) ;
105
+
106
+ nock ( 'https://leetcode.com' )
107
+ . get ( '/api/problems/database/' )
108
+ . replyWithFile ( 200 , './test/mock/problems.json.20160911' ) ;
109
+
110
+ nock ( 'https://leetcode.com' )
111
+ . get ( '/api/problems/shell/' )
112
+ . replyWithFile ( 200 , './test/mock/problems.json.20160911' ) ;
113
+
114
+ plugin . getProblems ( function ( e , problems ) {
115
+ assert . equal ( e , null ) ;
116
+ assert . equal ( problems . length , 377 * 3 ) ;
117
+ done ( ) ;
118
+ } ) ;
119
+ } ) ;
120
+
121
+ it ( 'should fail if error occurs' , function ( done ) {
122
+ nock ( 'https://leetcode.com' )
123
+ . get ( '/api/problems/algorithms/' )
124
+ . replyWithFile ( 200 , './test/mock/problems.json.20160911' ) ;
125
+
126
+ nock ( 'https://leetcode.com' )
127
+ . get ( '/api/problems/database/' )
128
+ . replyWithError ( 'unknown error' ) ;
129
+
130
+ nock ( 'https://leetcode.com' )
131
+ . get ( '/api/problems/shell/' )
132
+ . replyWithFile ( 200 , './test/mock/problems.json.20160911' ) ;
133
+
134
+ plugin . getProblems ( function ( e , problems ) {
135
+ assert . equal ( e . message , 'unknown error' ) ;
136
+ done ( ) ;
137
+ } ) ;
138
+ } ) ;
139
+ } ) ; // #getProblems
140
+
141
+ describe ( '#getCategoryProblems' , function ( ) {
100
142
it ( 'should ok' , function ( done ) {
101
143
nock ( 'https://leetcode.com' )
102
144
. get ( '/api/problems/algorithms/' )
@@ -301,7 +343,29 @@ describe('plugin:leetcode', function() {
301
343
} ) ;
302
344
} ) ;
303
345
346
+ it ( 'should fail if session expired' , function ( done ) {
347
+ nock ( 'https://leetcode.com' )
348
+ . get ( '/problems/find-the-difference' )
349
+ . reply ( 403 ) ;
350
+
351
+ plugin . getProblem ( PROBLEM , function ( e , problem ) {
352
+ assert . equal ( e , session . errors . EXPIRED ) ;
353
+ done ( ) ;
354
+ } ) ;
355
+ } ) ;
356
+
304
357
it ( 'should fail if http error' , function ( done ) {
358
+ nock ( 'https://leetcode.com' )
359
+ . get ( '/problems/find-the-difference' )
360
+ . reply ( 500 ) ;
361
+
362
+ plugin . getProblem ( PROBLEM , function ( e , problem ) {
363
+ assert . deepEqual ( e , { msg : 'http error' , statusCode : 500 } ) ;
364
+ done ( ) ;
365
+ } ) ;
366
+ } ) ;
367
+
368
+ it ( 'should fail if unknown error' , function ( done ) {
305
369
nock ( 'https://leetcode.com' )
306
370
. get ( '/problems/find-the-difference' )
307
371
. replyWithError ( 'unknown error!' ) ;
0 commit comments