Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove common tests #21

Merged
merged 2 commits into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
201 changes: 8 additions & 193 deletions test/indexed-store.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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 () {
Expand All @@ -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(
Expand All @@ -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()
Expand Down Expand Up @@ -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 })
Expand All @@ -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 = {
Expand All @@ -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 () {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down