diff --git a/compose.js b/compose.js index dd98803..bd9c989 100644 --- a/compose.js +++ b/compose.js @@ -34,7 +34,7 @@ module.exports = function compose () { // If db is levelup, it will auto-open and call the callback. If // abstract-leveldown, it won't. If abstract-db (a concept), it might. if (callback && !isLevelup(db) && db.status === 'new') { - db.open(options, function (err) { + db.open(xtend(layer.defaults, options), function (err) { if (err) return callback(err) callback(null, db) }) @@ -54,7 +54,8 @@ module.exports = function compose () { return shell } - layers.push(function (db, options, callback) { + layers.push(function wrapped (db, options, callback) { + wrapped.defaults = defaults return layer(db, xtend(defaults, options), callback) }) diff --git a/test.js b/test.js index d6cc580..6fb62dc 100644 --- a/test.js +++ b/test.js @@ -10,7 +10,6 @@ var packager = function (down) { } // Tests copied from level-packager -// TODO: write own tests test('packager - Level constructor has access to levelup errors', function (t) { function Down () {} @@ -93,3 +92,106 @@ test('packager - Level constructor with custom options', function (t) { t.is(levelup._db.codec.opts.keyEncoding, 'binary') t.is(levelup._db.codec.opts.valueEncoding, 'binary') }) + +// Own tests +// TODO: write more + +test('passes defaults only to preceding layer', function (t) { + t.plan(12) + + compose(negative, positive, { test: true }, negative)('test-location') + compose([negative, positive, { test: true }, negative])('test-location') + compose().use([negative, positive, { test: true }, negative])('test-location') + compose().use(negative).use(positive, { test: true }).use(negative)('test-location') + + function negative (dbOrLocation, options) { + t.same(options, {}, 'did not get defaults') + return dbOrLocation + } + + function positive (dbOrLocation, options) { + t.same(options, { test: true }, 'did get defaults') + return dbOrLocation + } +}) + +test('passes defaults only to preceding layers (in array)', function (t) { + t.plan(4) + + compose(negative, [positive, positive], { test: true }, negative)('test-location') + + function negative (dbOrLocation, options) { + t.same(options, {}, 'did not get defaults') + return dbOrLocation + } + + function positive (dbOrLocation, options) { + t.same(options, { test: true }, 'did get defaults') + return dbOrLocation + } +}) + +test('merges defaults into preceding layers', function (t) { + t.plan(9) + + const objects = { a: { a: 1 }, b: { b: 2 } } + + compose().use([b, [ab, ab], objects.a, b], objects.b)('test-location') + compose().use(none).use([a, a], objects.a).use(b, objects.b)('test-location') + + t.same(objects, { a: { a: 1 }, b: { b: 2 } }, 'did not mutate original objects') + + function a (dbOrLocation, options) { + t.same(options, { a: 1 }, 'only a') + return dbOrLocation + } + + function b (dbOrLocation, options) { + t.same(options, { b: 2 }, 'only b') + return dbOrLocation + } + + function ab (dbOrLocation, options) { + t.same(options, { a: 1, b: 2 }, 'a and b') + return dbOrLocation + } + + function none (dbOrLocation, options) { + t.same(options, {}, 'none') + return dbOrLocation + } +}) + +test('passes defaults to abstract db.open() if callback is provided', function (t) { + t.plan(20) + + compose(lastLayer, { test: true })('test-location', onOpen) + compose([firstLayer, lastLayer], { test: true })('test-location', onOpen) + compose([[firstLayer, lastLayer], { test: false }], { test: true })('test-location', onOpen) + + function firstLayer (location, options, callback) { + t.is(callback, undefined, 'only last layer receives callback') + return location + } + + function lastLayer (location, options, callback) { + t.is(location, 'test-location', 'got location') + t.same(options, { test: true }, 'got options') + t.is(callback, onOpen, 'got callback') + + // Mock abstract-leveldown + return { + status: 'new', + mocked: true, + open: function (options, callback) { + t.same(options, { test: true }, 'got open() options') + process.nextTick(callback) + } + } + } + + function onOpen (err, db) { + t.ifError(err, 'no open error') + t.is(db.mocked, true, 'got db') + } +})