-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest.js
69 lines (56 loc) · 1.38 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
var test = require('tap').test;
var expressMongoDb = require('./');
test('throws on invalid uri', function (t) {
t.throws(function () {
expressMongoDb();
}, /Expected uri to be a string/);
t.end();
});
test('middleware pass error on fail', function (t) {
var middleware = expressMongoDb('mongodb://localhost:31337');
middleware({}, {}, function (err) {
t.ok(err);
middleware({}, {}, function (err) {
t.ok(err);
t.end();
});
});
});
test('middleware stores connection to mongodb', function (t) {
var middleware = expressMongoDb('mongodb://localhost:27017');
var req = {};
middleware(req, {}, function (err) {
t.error(err);
t.ok(req.db);
req.db.close(true, t.end);
});
});
test('middleware stores connection in custom property', function (t) {
var middleware = expressMongoDb('mongodb://localhost:27017', {
property: 'myDb'
});
var req = {};
middleware(req, {}, function (err) {
t.error(err);
t.ok(req.myDb);
req.myDb.close(true, t.end);
});
});
test('returns same connection for multiple requests', function (t) {
var middleware = expressMongoDb('mongodb://localhost:27017', {
property: 'myDb'
});
var req = {};
var _db;
middleware(req, {}, function (err) {
t.error(err);
t.ok(req.myDb);
_db = req.myDb;
middleware(req, {}, function (err) {
t.error(err);
t.equal(_db, req.myDb);
req.myDb.close(true, t.end);
});
});
});