Skip to content

Commit 61d7de6

Browse files
committed
Refactor UTs.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 917855b commit 61d7de6

13 files changed

+385
-268
lines changed

test/helper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use_strict';
2+
const fs = require('fs');
3+
4+
const h = {
5+
DIR: './tmp/'
6+
};
7+
8+
h.clean = function() {
9+
for (let f of fs.readdirSync(this.DIR))
10+
fs.unlinkSync(this.DIR + f);
11+
};
12+
13+
module.exports = h;

test/plugins/test_cache.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
'use strict';
2-
const execSync = require('child_process').execSync;
3-
const fs = require('fs');
4-
52
const _ = require('underscore');
63
const assert = require('chai').assert;
74
const rewire = require('rewire');
85

96
const log = require('../../lib/log');
107
const config = require('../../lib/config');
11-
12-
const cache = rewire('../../lib/cache');
13-
const h = rewire('../../lib/helper');
14-
const session = rewire('../../lib/session');
15-
const plugin = rewire('../../lib/plugins/cache');
16-
17-
const HOME = './tmp';
8+
const th = require('../helper');
189

1910
describe('plugin:cache', function() {
11+
let plugin;
12+
let next;
13+
let cache;
14+
let h;
15+
let session;
16+
2017
const PROBLEMS = [
2118
{id: 0, fid: 0, name: 'name0', slug: 'slug0', starred: false, category: 'algorithms'},
2219
{id: 1, fid: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
2320
];
2421
const PROBLEM = {id: 0, fid: 0, slug: 'slug0', category: 'algorithms'};
2522

26-
const NEXT = {};
27-
2823
before(function() {
2924
log.init();
3025
config.init();
31-
plugin.init();
26+
});
3227

33-
h.getCacheDir = () => HOME;
28+
beforeEach(function() {
29+
th.clean();
30+
next = {};
31+
32+
h = rewire('../../lib/helper');
33+
h.getCacheDir = () => th.DIR;
34+
35+
cache = rewire('../../lib/cache');
3436
cache.__set__('h', h);
3537
cache.init();
3638

39+
session = rewire('../../lib/session');
3740
session.__set__('cache', cache);
41+
42+
plugin = rewire('../../lib/plugins/cache');
3843
plugin.__set__('cache', cache);
3944
plugin.__set__('session', session);
40-
plugin.setNext(NEXT);
41-
});
45+
plugin.init();
4246

43-
beforeEach(function() {
44-
execSync('rm -rf ' + HOME);
45-
fs.mkdirSync(HOME);
47+
plugin.setNext(next);
4648
});
4749

4850
describe('#getProblems', function() {
@@ -58,7 +60,7 @@ describe('plugin:cache', function() {
5860

5961
it('should getProblems w/o cache ok', function(done) {
6062
cache.del('problems');
61-
NEXT.getProblems = cb => cb(null, PROBLEMS);
63+
next.getProblems = cb => cb(null, PROBLEMS);
6264

6365
plugin.getProblems(function(e, problems) {
6466
assert.equal(e, null);
@@ -69,7 +71,7 @@ describe('plugin:cache', function() {
6971

7072
it('should getProblems w/o cache fail if client error', function(done) {
7173
cache.del('problems');
72-
NEXT.getProblems = cb => cb('client getProblems error');
74+
next.getProblems = cb => cb('client getProblems error');
7375

7476
plugin.getProblems(function(e, problems) {
7577
assert.equal(e, 'client getProblems error');
@@ -93,7 +95,7 @@ describe('plugin:cache', function() {
9395
it('should getProblem w/o cache ok', function(done) {
9496
cache.set('problems', PROBLEMS);
9597
cache.del('0.slug0.algorithms');
96-
NEXT.getProblem = (problem, cb) => cb(null, PROBLEMS[0]);
98+
next.getProblem = (problem, cb) => cb(null, PROBLEMS[0]);
9799

98100
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
99101
assert.equal(e, null);
@@ -105,7 +107,7 @@ describe('plugin:cache', function() {
105107
it('should getProblem fail if client error', function(done) {
106108
cache.set('problems', PROBLEMS);
107109
cache.del('0.slug0.algorithms');
108-
NEXT.getProblem = (problem, cb) => cb('client getProblem error');
110+
next.getProblem = (problem, cb) => cb('client getProblem error');
109111

110112
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
111113
assert.equal(e, 'client getProblem error');
@@ -171,7 +173,7 @@ describe('plugin:cache', function() {
171173
assert.equal(session.getUser(), null);
172174
assert.equal(session.isLogin(), false);
173175

174-
NEXT.login = (user, cb) => cb(null, user);
176+
next.login = (user, cb) => cb(null, user);
175177

176178
plugin.login(USER, function(e, user) {
177179
assert.equal(e, null);
@@ -188,7 +190,7 @@ describe('plugin:cache', function() {
188190
config.autologin.enable = false;
189191
cache.del(h.KEYS.user);
190192

191-
NEXT.login = (user, cb) => cb(null, user);
193+
next.login = (user, cb) => cb(null, user);
192194

193195
plugin.login(USER, function(e, user) {
194196
assert.equal(e, null);
@@ -200,7 +202,7 @@ describe('plugin:cache', function() {
200202
});
201203

202204
it('should login fail if client login error', function(done) {
203-
NEXT.login = (user, cb) => cb('client login error');
205+
next.login = (user, cb) => cb('client login error');
204206

205207
plugin.login(USER, function(e, user) {
206208
assert.equal(e, 'client login error');
@@ -220,5 +222,18 @@ describe('plugin:cache', function() {
220222
assert.equal(session.isLogin(), false);
221223
done();
222224
});
225+
226+
it('should logout ok', function(done) {
227+
// before logout
228+
cache.set(h.KEYS.user, USER);
229+
assert.deepEqual(session.getUser(), USER);
230+
assert.equal(session.isLogin(), true);
231+
232+
// after logout
233+
plugin.logout(null, true);
234+
assert.equal(session.getUser(), null);
235+
assert.equal(session.isLogin(), false);
236+
done();
237+
});
223238
}); // #user
224239
});

test/plugins/test_leetcode.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nock = require('nock');
55
const rewire = require('rewire');
66

77
const config = require('../../lib/config');
8+
const chalk = require('../../lib/chalk');
89
const log = require('../../lib/log');
910

1011
const plugin = rewire('../../lib/plugins/leetcode');
@@ -31,6 +32,7 @@ describe('plugin:leetcode', function() {
3132
before(function() {
3233
log.init();
3334
config.init();
35+
chalk.init();
3436
plugin.init();
3537

3638
session.getUser = () => USER;

test/test_cache.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
'use strict';
2-
const execSync = require('child_process').execSync;
3-
42
const assert = require('chai').assert;
53
const rewire = require('rewire');
64

7-
const cache = rewire('../lib/cache');
8-
const h = rewire('../lib/helper');
5+
const th = require('./helper');
96

107
describe('cache', function() {
11-
const k = '.test';
12-
const v = {test: 'data'};
8+
let cache;
9+
10+
const K = '.test';
11+
const V = {test: 'data'};
1312

14-
before(function() {
15-
const cachedir = './tmp';
16-
execSync('rm -rf ' + cachedir);
13+
beforeEach(function() {
14+
th.clean();
1715

18-
h.getCacheDir = () => cachedir;
16+
const h = rewire('../lib/helper');
17+
h.getCacheDir = () => th.DIR;
18+
19+
cache = rewire('../lib/cache');
1920
cache.__set__('h', h);
2021
cache.init();
2122
});
2223

23-
it('should ok when not cached', function() {
24-
cache.del(k);
25-
26-
assert.equal(cache.get(k), null);
27-
assert.equal(cache.del(k), false);
24+
it('should get ok when not cached', function() {
25+
cache.del(K);
26+
assert.equal(cache.get(K), null);
27+
assert.equal(cache.del(K), false);
2828
});
2929

30-
it('should ok when cached', function() {
31-
assert.equal(cache.set(k, v), true);
32-
33-
assert.deepEqual(cache.get(k), v);
34-
assert.equal(cache.del(k), true);
30+
it('should get ok when cached', function() {
31+
assert.equal(cache.set(K, V), true);
32+
assert.deepEqual(cache.get(K), V);
33+
assert.equal(cache.del(K), true);
3534
});
3635

3736
it('should list ok when no cached', function() {
@@ -40,12 +39,10 @@ describe('cache', function() {
4039
});
4140

4241
it('should list ok when cached', function() {
43-
assert.equal(cache.set(k, v), true);
44-
42+
assert.equal(cache.set(K, V), true);
4543
const items = cache.list();
4644
assert.equal(items.length, 1);
47-
48-
assert.equal(items[0].name, k);
49-
assert.equal(items[0].size, JSON.stringify(v).length);
45+
assert.equal(items[0].name, K);
46+
assert.equal(items[0].size, JSON.stringify(V).length);
5047
});
5148
});

test/test_chalk.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ const rewire = require('rewire');
44

55
// refer to https://en.wikipedia.org/wiki/ANSI_escape_code
66
describe('chalk', function() {
7-
it('should ok w/ 256 colors', function() {
8-
const chalk = rewire('../lib/chalk');
7+
let chalk;
8+
9+
beforeEach(function() {
10+
chalk = rewire('../lib/chalk');
911
chalk.enabled = true;
1012
chalk.use256 = true;
13+
});
14+
15+
it('should ok w/ 256 colors', function() {
1116
chalk.init();
1217
chalk.setTheme('default');
1318

@@ -29,8 +34,6 @@ describe('chalk', function() {
2934
});
3035

3136
it('should ok w/ 8 colors', function() {
32-
const chalk = rewire('../lib/chalk');
33-
chalk.enabled = true;
3437
chalk.use256 = false;
3538
chalk.init();
3639
chalk.setTheme('default');
@@ -46,7 +49,6 @@ describe('chalk', function() {
4649
});
4750

4851
it('should ok w/o colors', function() {
49-
const chalk = rewire('../lib/chalk');
5052
chalk.enabled = false;
5153
chalk.init();
5254
chalk.setTheme('default');
@@ -61,36 +63,28 @@ describe('chalk', function() {
6163
assert.equal(chalk.white(' '), ' ');
6264
});
6365

64-
it('should sprint ok', function() {
65-
const chalk = rewire('../lib/chalk');
66-
chalk.enabled = true;
67-
chalk.use256 = true;
66+
it('should sprint w/ 256 colors ok', function() {
6867
chalk.init();
6968
chalk.setTheme('default');
70-
7169
assert.equal(chalk.sprint(' ', '#00ff00'), '\u001b[38;5;46m \u001b[39m');
70+
});
7271

72+
it('should sprint w/ 8 colors ok', function() {
7373
chalk.use256 = false;
74+
chalk.init();
75+
chalk.setTheme('default');
7476
assert.equal(chalk.sprint(' ', '#00ff00'), '\u001b[92m \u001b[39m');
7577
});
7678

7779
it('should set theme ok', function() {
78-
const chalk = rewire('../lib/chalk');
79-
chalk.enabled = true;
80-
chalk.use256 = true;
8180
chalk.init();
8281
chalk.setTheme('dark');
83-
8482
assert.equal(chalk.sprint(' ', '#009900'), chalk.green(' '));
8583
});
8684

8785
it('should set unknown theme ok', function() {
88-
const chalk = rewire('../lib/chalk');
89-
chalk.enabled = true;
90-
chalk.use256 = true;
9186
chalk.init();
9287
chalk.setTheme('unknown');
93-
9488
assert.equal(chalk.sprint(' ', '#00ff00'), chalk.green(' '));
9589
});
9690
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy