Skip to content

Commit

Permalink
NOT_AN_OBJECT: throw for null; add additional tests
Browse files Browse the repository at this point in the history
Also separates test cases so specific failures can be understood.
  • Loading branch information
alxndrsn committed Apr 12, 2024
1 parent 9afecb2 commit 2e41bd9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
6 changes: 3 additions & 3 deletions packages/node_modules/pouchdb-core/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class AbstractPouchDB extends EventEmitter {
callback = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (doc == null || typeof doc !== 'object' || Array.isArray(doc)) {
return callback(createError(NOT_AN_OBJECT));
}
this.bulkDocs({docs: [doc]}, opts, yankError(callback, doc._id));
Expand All @@ -208,7 +208,7 @@ class AbstractPouchDB extends EventEmitter {
cb = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (doc == null || typeof doc !== 'object' || Array.isArray(doc)) {
return cb(createError(NOT_AN_OBJECT));
}
invalidIdError(doc._id);
Expand Down Expand Up @@ -778,7 +778,7 @@ class AbstractPouchDB extends EventEmitter {
}

for (var i = 0; i < req.docs.length; ++i) {
if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
if (req.docs[i] == null || typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
return callback(createError(NOT_AN_OBJECT));
}
}
Expand Down
72 changes: 55 additions & 17 deletions tests/integration/test.basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,23 +746,61 @@ adapters.forEach(function (adapter) {
});
});

it('Error when document is not an object', function (done) {
var db = new PouchDB(dbs.name);
var doc1 = [{ _id: 'foo' }, { _id: 'bar' }];
var doc2 = 'this is not an object';
var count = 5;
var callback = function (err) {
should.exist(err);
count--;
if (count === 0) {
done();
}
};
db.post(doc1, callback);
db.post(doc2, callback);
db.put(doc1, callback);
db.put(doc2, callback);
db.bulkDocs({docs: [doc1, doc2]}, callback);
[
null,
undefined,
[],
[{ _id: 'foo' }, { _id: 'bar' }],
'this is not an object',
String('this is not an object'),
//new String('this is not an object'), actually, this _is_ an object
].forEach((badDoc, idx) => {
describe(`Should error when document is not an object #${idx}`, () => {
let db;

beforeEach(() => {
db = new PouchDB(dbs.name);
});

it('should error for .post()', async () => {
let threw;
try {
await db.post(badDoc);
} catch (err) {
threw = true;
err.message.should.equal('Document must be a JSON object');
}
if (!threw) {
throw new Error('should have thrown');
}
});

it('should error for .put()', async () => {
let threw;
try {
await db.post(badDoc);
} catch (err) {
threw = true;
err.message.should.equal('Document must be a JSON object');
}
if (!threw) {
throw new Error('should have thrown');
}
});

it('should error for .bulkDocs()', async () => {
let threw;
try {
await db.bulkDocs({docs: [badDoc]});
} catch (err) {
threw = true;
err.message.should.equal('Document must be a JSON object');
}
if (!threw) {
throw new Error('should have thrown');
}
});
});
});

it('Test instance update_seq updates correctly', function (done) {
Expand Down

0 comments on commit 2e41bd9

Please sign in to comment.