@@ -30,6 +30,7 @@ describe('plugin:leetcode', function() {
3030 before ( function ( ) {
3131 log . init ( ) ;
3232 config . init ( ) ;
33+ plugin . init ( ) ;
3334
3435 session . getUser = function ( ) {
3536 return USER ;
@@ -97,6 +98,47 @@ describe('plugin:leetcode', function() {
9798 } ) ; // #login
9899
99100 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 ( ) {
100142 it ( 'should ok' , function ( done ) {
101143 nock ( 'https://leetcode.com' )
102144 . get ( '/api/problems/algorithms/' )
@@ -301,7 +343,29 @@ describe('plugin:leetcode', function() {
301343 } ) ;
302344 } ) ;
303345
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+
304357 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 ) {
305369 nock ( 'https://leetcode.com' )
306370 . get ( '/problems/find-the-difference' )
307371 . replyWithError ( 'unknown error!' ) ;
0 commit comments