/
index.js
96 lines (75 loc) · 2.88 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var should = require('should'),
sinon = require('sinon'),
proxyquire = require('proxyquire').noCallThru();
require('should-sinon');
describe('Mongo', function() {
var monkMock,
collectionMock,
collectionObj,
Storage,
config;
beforeEach(function() {
config = {mongoUri: 'http://someurl.somewhere.com'};
collectionObj = {
find: sinon.stub(),
findOne: sinon.stub(),
findOneAndUpdate: sinon.stub(),
findOneAndDelete: sinon.stub(),
};
collectionMock = {
get: sinon.stub().returns(collectionObj)
};
monkMock = sinon.stub().returns(collectionMock);
Storage = proxyquire('../src/index', {monk: monkMock});
});
describe('Initialization', function() {
it('should throw an error if config is missing', function() {
Storage.should.throw('Need to provide mongo address.');
});
it('should throw an error if mongoUri is missing', function() {
(function() {Storage({});}).should.throw('Need to provide mongo address.');
});
it('should initialize monk with mongoUri', function() {
Storage(config);
monkMock.callCount.should.equal(1);
monkMock.args[0][0].should.equal(config.mongoUri);
});
});
['teams', 'channels', 'users'].forEach(function(method) {
describe(method + '.get', function() {
it('should call findOne with callback', function() {
var cb = sinon.stub();
Storage(config)[method].get('walterwhite', cb);
collectionObj.findOne.should.be.calledWith({id: 'walterwhite'}, cb);
});
});
describe(method + '.save', function() {
it('should call findOneAndUpdate', function() {
var data = {id: 'walterwhite'},
cb = sinon.stub();
Storage(config)[method].save(data, cb);
collectionObj.findOneAndUpdate.should.be.calledWith(
{id: 'walterwhite'},
data,
{upsert: true, 'returnNewDocument': true},
cb
);
});
});
describe(method + '.all', function() {
it('should call find', function() {
var cb = sinon.stub();
Storage(config)[method].all(cb);
collectionObj.find.should.be.calledWith({}, cb);
});
});
describe(method + '.delete', function() {
it('should call findOneAndDelete', function() {
var id = 'walterwhite',
cb = sinon.stub();
Storage(config)[method].delete(id, cb);
collectionObj.findOneAndDelete.should.be.calledWith({id: 'walterwhite'}, cb);
});
});
});
});