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 `get` static method', done => {
const User = createTestModel({}, schema => {
schema.statics.beforeGet = (options, done) => done();
schema.statics.afterGet = (options, results, done) => done();
}, 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()
};
it('should work using `getById` static method', 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 exec = findById.chain('exec').yields(null, user);
const beforeGetById = sinon.spy(User, 'beforeGetById');
const afterGetById = sinon.spy(User, 'afterGetById');
User.getById(user._id, (error, found) => {
Mock.verify();
Mock.restore();
expect(findById).to.have.been.calledOnce;
describe('index', () => {
const User = createTestModel({
name: { type: String, fake: f => f.name.findName() },
address: { type: String, hide: true, fake: f => f.address.streetAddress() },
password: { type: String, fake: f => f.internet.password() }
}, actions);
it('should be able to generate fake instance', () => {
expect(User.fake).to.exist;
expect(User.fake).to.be.a('function');
const user = User.fake();
expect(user).to.exist;
expect(user.name).to.exist;
expect(user.password).to.exist;
});
it('should be able to hide default properties', () => {
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();
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;
describe('post', () => {
const Guardian = createTestModel({
email: { type: String, unique: true, fake: f => f.internet.email() }
}, actions);
const Child = createTestModel({
email: { type: String, unique: true, fake: f => f.internet.email() },
father: { type: ObjectId, ref: Guardian.modelName }
}, actions);
let father = Guardian.fake();
before(done => clear(Guardian, Child, done));
before(done => create(father, done));
it('should work using `post` static method', done => {
const guardian = Guardian.fake();
Guardian.post(guardian.toObject(), (error, created) => {
expect(error).to.not.exist;
expect(created).to.exist;
expect(created._id).to.exist.and.be.eql(guardian._id);
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;
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');
describe('post', () => {
const Guardian = createTestModel({
email: { type: String, unique: true, fake: f => f.internet.email() }
}, actions);
const Child = createTestModel({
email: { type: String, unique: true, fake: f => f.internet.email() },
father: { type: ObjectId, ref: Guardian.modelName }
}, actions);
let father = Guardian.fake();
before(done => clear(Guardian, Child, done));
before(done => create(father, done));
it('should work using `post` static method', done => {
const guardian = Guardian.fake();
describe('put', () => {
const Guardian = createTestModel({
email: { type: String, unique: true, fake: f => f.internet.email() }
}, actions);
const Child = createTestModel({
email: { type: String, unique: true, fake: f => f.internet.email() },
father: { type: ObjectId, ref: Guardian.modelName }
}, actions);
let father;
let aunt;
let child;
beforeEach(done => clear(Guardian, Child, done));
beforeEach(done => {
father = Guardian.fake();