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 `put` static method with filter', 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 where = findById.chain('where');
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');
const options = { _id: user._id, filter: { name: user.name } };
User.put(options, updates, (error, updated) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
it('should work using `patch` instance method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePatch = (updates, done) => done();
schema.methods.afterPatch = (updates, done) => done();
}, patch);
const user = User.fake();
const mock = mockInstance(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');
user.patch(updates, (error, updated) => {
mock.verify();
mock.restore();
expect(save).to.have.been.calledOnce;
expect(beforePatch).to.have.been.calledOnce;
expect(afterPatch).to.have.been.calledOnce;
done(error, updated);
});
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 `del` static method with filter', 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 where = findById.chain('where');
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');
const options = { _id: user._id, filter: { name: user.name } };
User.del(options, (error, deleted) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();