Skip to content

Commit

Permalink
test(document.test): test hooks for "validateSync" to skip async func…
Browse files Browse the repository at this point in the history
…tions and promises
  • Loading branch information
hasezoey committed Sep 15, 2022
1 parent 71478c4 commit fc0ccc1
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11864,6 +11864,73 @@ describe('document', function() {
assert.strictEqual(doc.sub.propertyB, 'foo');
assert.strictEqual(called, 1);
});

it('validateSync executes error hook and ignores async function (gh-4885)', function(done) {
let asyncCalled = false;
let errorHookCalled = false;
let normalHookCalled = false;

const schema = new Schema({
prop: Boolean
});

schema.post('validate', async function() { asyncCalled = true; });
schema.post('validate', () => { normalHookCalled = true; throw new Error('test error for error hook'); });
schema.post('validate', function(err, doc, next) {
errorHookCalled = true;
next();
});

const model = db.model('test', schema);

const doc = new model({ prop: true });
doc.validateSync();

// schema.path('prop').doValidateSync(true);

assert.strictEqual(normalHookCalled, true);
assert.strictEqual(asyncCalled, false);
assert.strictEqual(errorHookCalled, true);
done();
});

it('validateSync executes error hook and ignores returned Promises (gh-4885)', function(done) {
let asyncCalled = false;
let asyncCompleted = false;
let errorHookCalled = false;
let normalHookCalled = false;

const schema = new Schema({
prop: Boolean
});

schema.post('validate', function() {
asyncCalled = true;
return new Promise((res) => setTimeout(() => {
asyncCompleted = true;
return res();
}, 1000));
});
schema.post('validate', () => { normalHookCalled = true; throw new Error('test error for error hook'); });
schema.post('validate', function(err, doc, next) {
errorHookCalled = true;
assert.ok(err);
next();
});

// schema.path('prop').doValidateSync(true);

const model = db.model('test', schema);

const doc = new model({ prop: true });
doc.validateSync();

assert.strictEqual(normalHookCalled, true);
assert.strictEqual(asyncCalled, true);
assert.strictEqual(asyncCompleted, false);
assert.strictEqual(errorHookCalled, true);
done();
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit fc0ccc1

Please sign in to comment.