Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should work using `post` static method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePost = done => done();
schema.methods.afterPost = done => done();
}, post);
const user = User.fake();
const mock = mockInstance(user);
const save = mock.expects('save').yields(null, user);
const beforePost = sinon.spy(user, 'beforePost');
const afterPost = sinon.spy(user, 'afterPost');
User.post(user, (error, created) => {
mock.verify();
mock.restore();
expect(save).to.have.been.calledOnce;
expect(beforePost).to.have.been.calledOnce;
expect(afterPost).to.have.been.calledOnce;
done(error, created);
});
});
it('should work using `put` static method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePut = (updates, done) => done();
schema.methods.afterPut = (updates, done) => done();
}, put);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const exec = findById.chain('exec').yields(null, user);
const save = mock.expects('save').yields(null, user);
const beforePut = sinon.spy(user, 'beforePut');
const afterPut = sinon.spy(user, 'afterPut');
const updates = _.pick(User.fake(), 'name');
User.put(user._id, updates, (error, updated) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
expect(findById).to.have.been.calledOnce;
expect(findById).to.have.been.calledWith(user._id);
expect(exec).to.have.been.calledOnce;
expect(save).to.have.been.calledOnce;
expect(beforePut).to.have.been.calledOnce;
expect(afterPut).to.have.been.calledOnce;
it('should work using `del` static method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforeDelete = done => done();
schema.methods.afterDelete = done => done();
}, del);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const exec = findById.chain('exec').yields(null, user);
const remove = mock.expects('remove').yields(null, user);
const beforeDelete = sinon.spy(user, 'beforeDelete');
const afterDelete = sinon.spy(user, 'afterDelete');
User.del(user._id, (error, deleted) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
expect(findById).to.have.been.calledOnce;
expect(findById).to.have.been.calledWith(user._id);
expect(exec).to.have.been.calledOnce;
expect(remove).to.have.been.calledOnce;
expect(beforeDelete).to.have.been.calledOnce;
expect(afterDelete).to.have.been.calledOnce;
done(error, deleted);
it('should work using `getById` static method with filter', done => {
const User = createTestModel({}, schema => {
schema.statics.beforeGetById = (done) => done();
schema.statics.afterGetById = (instance, done) => done();
}, get);
const user = User.fake();
const Mock = mockModel(User);
const findById = Mock.expects('findById');
const where = findById.chain('where');
const exec = findById.chain('exec').yields(null, user);
const beforeGetById = sinon.spy(User, 'beforeGetById');
const afterGetById = sinon.spy(User, 'afterGetById');
const options = { _id: user._id, filter: { name: user.name } };
User.getById(options, (error, found) => {
Mock.verify();
Mock.restore();
expect(findById).to.have.been.calledOnce;
expect(findById).to.have.been.calledWith(user._id);
expect(where).to.have.been.calledOnce;
expect(where).to.have.been.calledWith(options.filter);
expect(exec).to.have.been.calledOnce;
expect(beforeGetById).to.have.been.calledOnce;
expect(afterGetById).to.have.been.calledOnce;
done(error, found);
});
it('should work using `del` static method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforeDelete = done => done();
schema.methods.afterDelete = done => done();
}, del);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const exec = findById.chain('exec').yields(null, user);
const remove = mock.expects('remove').yields(null, user);
const beforeDelete = sinon.spy(user, 'beforeDelete');
const afterDelete = sinon.spy(user, 'afterDelete');
User.del(user._id, (error, deleted) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
expect(findById).to.have.been.calledOnce;
expect(findById).to.have.been.calledWith(user._id);
expect(exec).to.have.been.calledOnce;
expect(remove).to.have.been.calledOnce;
expect(beforeDelete).to.have.been.calledOnce;
expect(afterDelete).to.have.been.calledOnce;
it('should work using `post` instance method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePost = done => done();
schema.methods.afterPost = done => done();
}, post);
const user = User.fake();
const mock = mockInstance(user);
const save = mock.expects('save').yields(null, user);
const beforePost = sinon.spy(user, 'beforePost');
const afterPost = sinon.spy(user, 'afterPost');
user.post((error, created) => {
mock.verify();
mock.restore();
expect(save).to.have.been.calledOnce;
expect(beforePost).to.have.been.calledOnce;
expect(afterPost).to.have.been.calledOnce;
done(error, created);
});
});
it('should work using `put` instance method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePut = (updates, done) => done();
schema.methods.afterPut = (updates, done) => done();
}, put);
const user = User.fake();
const mock = mockInstance(user);
const save = mock.expects('save').yields(null, user);
const beforePut = sinon.spy(user, 'beforePut');
const afterPut = sinon.spy(user, 'afterPut');
const updates = _.pick(User.fake(), 'name');
user.put(updates, (error, updated) => {
mock.verify();
mock.restore();
expect(save).to.have.been.calledOnce;
expect(beforePut).to.have.been.calledOnce;
expect(afterPut).to.have.been.calledOnce;
done(error, updated);
});
});
const User = createTestModel({}, schema => {
schema.methods.beforePatch = (updates, done) => done();
schema.methods.afterPatch = (updates, done) => done();
}, patch);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const where = findById.chain('where');
const exec = findById.chain('exec').yields(null, user);
const save = mock.expects('save').yields(null, user);
const beforePatch = sinon.spy(user, 'beforePatch');
const afterPatch = sinon.spy(user, 'afterPatch');
const updates = _.pick(User.fake(), 'name');
const options = { _id: user._id, filter: { name: user.name } };
User.patch(options, updates, (error, updated) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
expect(findById).to.have.been.calledOnce;
expect(findById).to.have.been.calledWith(user._id);
expect(where).to.have.been.calledOnce;
expect(where).to.have.been.calledWith(options.filter);
expect(exec).to.have.been.calledOnce;
expect(save).to.have.been.calledOnce;
}, searchable, get);
const options = { page: 1, limit: 10 };
const results = {
data: [User.fake()],
total: 100,
size: 1,
limit: 10,
skip: 0,
page: 1,
pages: 10,
lastModified: new Date()
};
const Mock = mockModel(User);
const beforeGet = sinon.spy(User, 'beforeGet');
const afterGet = sinon.spy(User, 'afterGet');
const find = Mock.expects('_get').yields(null, results);
User.get(options, (error, found) => {
Mock.verify();
Mock.restore();
expect(find).to.have.been.calledOnce;
expect(beforeGet).to.have.been.calledOnce;
expect(afterGet).to.have.been.calledOnce;
done(error, found);
});
});
const options = { page: 1, limit: 10 };
const results = {
data: [User.fake()],
total: 100,
size: 1,
limit: 10,
skip: 0,
page: 1,
pages: 10,
lastModified: new Date()
};
const Mock = mockModel(User);
const beforeGet = sinon.spy(User, 'beforeGet');
const afterGet = sinon.spy(User, 'afterGet');
const find = Mock.expects('_get').yields(null, results);
User.get(options, (error, found) => {
Mock.verify();
Mock.restore();
expect(find).to.have.been.calledOnce;
expect(beforeGet).to.have.been.calledOnce;
expect(afterGet).to.have.been.calledOnce;
done(error, found);
});
});