diff --git a/package.json b/package.json index bca3b859..fdb6b296 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "html-webpack-plugin": "^2.28.0", "jest": "^20.0.4", "jsdoc": "^3.4.3", + "logux-store-tests": "MrMeison/logux-store-tests#move-common-tests", "lint-staged": "^4.0.0", "pre-commit": "^1.2.2", "rimraf": "^2.6.1", diff --git a/test/indexed-store.test.js b/test/indexed-store.test.js index 164ddb5c..7c165a6d 100644 --- a/test/indexed-store.test.js +++ b/test/indexed-store.test.js @@ -1,6 +1,7 @@ /* eslint-disable no-invalid-this */ var fakeIndexedDB = require('fake-indexeddb') +var eachTest = require('logux-store-tests') var TestTime = require('logux-core').TestTime var IndexedStore = require('../indexed-store') @@ -49,6 +50,13 @@ function check (indexed, created, added) { function nope () { } +eachTest(function (desc, creator) { + it(desc, creator(function () { + store = new IndexedStore() + return store + })) +}) + it('use logux as default name', function () { store = new IndexedStore() return store.init().then(function () { @@ -65,57 +73,6 @@ it('allows to change DB name', function () { }) }) -it('is empty in the beginning', function () { - store = new IndexedStore() - return check(store, []).then(function () { - return store.getLastAdded() - }).then(function (added) { - expect(added).toEqual(0) - return store.getLastSynced() - }).then(function (synced) { - expect(synced).toEqual({ sent: 0, received: 0 }) - }) -}) - -it('updates last sent value', function () { - store = new IndexedStore() - return store.setLastSynced({ sent: 1 }).then(function () { - return store.getLastSynced() - }).then(function (synced) { - return expect(synced).toEqual({ sent: 1, received: 0 }) - }).then(function () { - return store.setLastSynced({ sent: 2, received: 1 }) - }).then(function () { - return store.getLastSynced() - }).then(function (synced) { - return expect(synced).toEqual({ sent: 2, received: 1 }) - }).then(function () { - other = new IndexedStore(store.name) - return other.getLastSynced() - }).then(function (synced) { - return expect(synced).toEqual({ sent: 2, received: 1 }) - }) -}) - -it('stores entries sorted', function () { - store = new IndexedStore() - return Promise.all([ - store.add({ type: '1' }, { id: [1, 'a'], time: 1 }), - store.add({ type: '2' }, { id: [1, 'c'], time: 2 }), - store.add({ type: '3' }, { id: [1, 'b'], time: 2 }) - ]).then(function () { - return check(store, [ - [{ type: '2' }, { added: 2, id: [1, 'c'], time: 2 }], - [{ type: '3' }, { added: 3, id: [1, 'b'], time: 2 }], - [{ type: '1' }, { added: 1, id: [1, 'a'], time: 1 }] - ], [ - [{ type: '3' }, { added: 3, id: [1, 'b'], time: 2 }], - [{ type: '2' }, { added: 2, id: [1, 'c'], time: 2 }], - [{ type: '1' }, { added: 1, id: [1, 'a'], time: 1 }] - ]) - }) -}) - it('stores any metadata', function () { store = new IndexedStore() return store.add( @@ -142,22 +99,6 @@ it('ignores entries with same ID', function () { }) }) -it('returns last added', function () { - store = new IndexedStore() - return store.add({ type: 'A' }, { id: [1], time: 1 }).then(function () { - return store.add({ type: 'B' }, { id: [2], time: 2 }) - }).then(function () { - return store.getLastAdded() - }).then(function (added) { - return expect(added).toBe(2) - }).then(function () { - other = new IndexedStore(store.name) - return other.getLastAdded() - }).then(function (added) { - return expect(added).toBe(2) - }) -}) - it('reloads page on database update', function () { document.reload = jest.fn() store = new IndexedStore() @@ -189,25 +130,6 @@ it('checks that action ID is used in log', function () { }) }) -it('changes meta', function () { - store = new IndexedStore() - return store.add({ }, { id: [1], time: 1, a: 1 }).then(function () { - return store.changeMeta([1], { a: 2, b: 2 }) - }).then(function (result) { - expect(result).toBeTruthy() - return check(store, [ - [{ }, { id: [1], time: 1, added: 1, a: 2, b: 2 }] - ]) - }) -}) - -it('resolves to false on unknown ID in changeMeta', function () { - store = new IndexedStore() - return store.changeMeta([1], { a: 1 }).then(function (result) { - expect(result).toBeFalsy() - }) -}) - it('works with real log', function () { store = new IndexedStore() var log = TestTime.getLog({ store: store }) @@ -224,32 +146,6 @@ it('works with real log', function () { }) }) -it('removes entries', function () { - store = new IndexedStore() - return Promise.all([ - store.add({ type: '1' }, { id: [1, 'node', 0], time: 1 }), - store.add({ type: '2' }, { id: [2, 'node', 0], time: 2 }), - store.add({ type: '3' }, { id: [3, 'node', 0], time: 3 }) - ]).then(function () { - return store.remove([2, 'node', 0]) - }).then(function (entry) { - expect(entry).toEqual([ - { type: '2' }, { id: [2, 'node', 0], time: 2, added: 2 } - ]) - return check(store, [ - [{ type: '3' }, { id: [3, 'node', 0], time: 3, added: 3 }], - [{ type: '1' }, { id: [1, 'node', 0], time: 1, added: 1 }] - ]) - }) -}) - -it('ignores unknown entry', function () { - store = new IndexedStore() - return store.remove([2]).then(function (removed) { - expect(removed).toBeFalsy() - }) -}) - it('throws a errors', function () { var error = new Error() global.indexedDB = { @@ -270,87 +166,6 @@ it('throws a errors', function () { }) }) -it('removes reasons and actions without reason', function () { - store = new IndexedStore() - var removed = [] - return Promise.all([ - store.add({ type: '1' }, { id: [1], time: 1, reasons: ['a'] }), - store.add({ type: '2' }, { id: [2], time: 2, reasons: ['a'] }), - store.add({ type: '3' }, { id: [3], time: 3, reasons: ['a', 'b'] }), - store.add({ type: '4' }, { id: [4], time: 4, reasons: ['b'] }) - ]).then(function () { - return store.removeReason('a', { }, function (action, meta) { - removed.push([action, meta]) - }) - }).then(function () { - expect(removed).toEqual([ - [{ type: '1' }, { added: 1, id: [1], time: 1, reasons: [] }], - [{ type: '2' }, { added: 2, id: [2], time: 2, reasons: [] }] - ]) - return check(store, [ - [{ type: '4' }, { added: 4, id: [4], time: 4, reasons: ['b'] }], - [{ type: '3' }, { added: 3, id: [3], time: 3, reasons: ['b'] }] - ]) - }) -}) - -it('removes reason with minimum added', function () { - store = new IndexedStore() - return Promise.all([ - store.add({ type: '1' }, { id: [1], time: 1, reasons: ['a'] }), - store.add({ type: '2' }, { id: [2], time: 2, reasons: ['a'] }), - store.add({ type: '3' }, { id: [3], time: 3, reasons: ['a'] }) - ]).then(function () { - return store.removeReason('a', { minAdded: 2 }, nope) - }).then(function () { - return check(store, [ - [{ type: '1' }, { added: 1, id: [1], time: 1, reasons: ['a'] }] - ]) - }) -}) - -it('removes reason with maximum added', function () { - store = new IndexedStore() - return Promise.all([ - store.add({ type: '1' }, { id: [1], time: 1, reasons: ['a'] }), - store.add({ type: '2' }, { id: [2], time: 2, reasons: ['a'] }), - store.add({ type: '3' }, { id: [3], time: 3, reasons: ['a'] }) - ]).then(function () { - return store.removeReason('a', { maxAdded: 2 }, nope) - }).then(function () { - return check(store, [ - [{ type: '3' }, { added: 3, id: [3], time: 3, reasons: ['a'] }] - ]) - }) -}) - -it('removes reason with minimum and maximum added', function () { - store = new IndexedStore() - return Promise.all([ - store.add({ type: '1' }, { id: [1], time: 1, reasons: ['a'] }), - store.add({ type: '2' }, { id: [2], time: 2, reasons: ['a'] }), - store.add({ type: '3' }, { id: [3], time: 3, reasons: ['a'] }) - ]).then(function () { - return store.removeReason('a', { maxAdded: 2, minAdded: 2 }, nope) - }).then(function () { - return check(store, [ - [{ type: '3' }, { added: 3, id: [3], time: 3, reasons: ['a'] }], - [{ type: '1' }, { added: 1, id: [1], time: 1, reasons: ['a'] }] - ]) - }) -}) - -it('removes reason with zero at maximum added', function () { - store = new IndexedStore() - return store.add({ }, { id: [1], time: 1, reasons: ['a'] }).then(function () { - return store.removeReason('a', { maxAdded: 0 }, nope) - }).then(function () { - return check(store, [ - [{ }, { added: 1, id: [1], time: 1, reasons: ['a'] }] - ]) - }) -}) - it('updates reasons cache', function () { store = new IndexedStore() return store.add({ }, { id: [1], time: 1, reasons: ['a'] }).then(function () { diff --git a/yarn.lock b/yarn.lock index 29f8a350..c14e0cf3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2800,6 +2800,10 @@ logux-core@logux/logux-core: dependencies: nanoevents "^0.3.0" +logux-store-tests@MrMeison/logux-store-tests#move-common-tests: + version "0.0.1" + resolved "https://codeload.github.com/MrMeison/logux-store-tests/tar.gz/09b89226d467b125f4ea6a5b3b32704c5b252c0d" + logux-sync@logux/logux-sync: version "0.1.2" resolved "https://codeload.github.com/logux/logux-sync/tar.gz/a49b499c3e224cf3a0e9f0f0ad6f0d2b9aa531bf"