53 lines
1.8 KiB
JavaScript
53 lines
1.8 KiB
JavaScript
'use strict'
|
|
|
|
const assert = require('assert')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const vm = require('vm')
|
|
|
|
const root = path.resolve(__dirname, '..')
|
|
const sandbox = {}
|
|
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'canvas-store.js'), 'utf8'), sandbox)
|
|
const api = sandbox.IWBCanvasStore
|
|
assert.ok(api)
|
|
|
|
const changes = []
|
|
const store = api.create({
|
|
initialState: { nodes: [{ id: 'n1' }], edges: [], groups: [] },
|
|
onChange: (next, previous, meta) => changes.push({ next, previous, meta }),
|
|
})
|
|
const seen = []
|
|
const unsubscribe = store.subscribe((next, previous, meta) => seen.push({ next, previous, meta }))
|
|
|
|
const initial = store.getState()
|
|
assert.strictEqual(initial.nodes.length, 1)
|
|
initial.nodes.push({ id: 'outside' })
|
|
assert.strictEqual(store.getState().nodes.length, 1)
|
|
|
|
const patched = store.patch({ edges: [{ id: 'e1' }] }, { type: 'edge' })
|
|
assert.strictEqual(patched.edges.length, 1)
|
|
assert.strictEqual(patched.rev, 1)
|
|
assert.strictEqual(seen[0].meta.type, 'edge')
|
|
|
|
const transacted = store.transaction((draft) => {
|
|
draft.nodes = draft.nodes.concat([{ id: 'n2' }])
|
|
draft.groups = [{ id: 'g1', memberIds: ['n1', 'n2'] }]
|
|
}, { type: 'transaction' })
|
|
assert.strictEqual(transacted.nodes.length, 2)
|
|
assert.strictEqual(transacted.groups.length, 1)
|
|
assert.strictEqual(changes.length, 2)
|
|
assert.strictEqual(seen.length, 2)
|
|
assert.strictEqual(store.revision, 2)
|
|
assert.strictEqual(seen[1].meta.type, 'transaction')
|
|
|
|
unsubscribe()
|
|
store.replace({ nodes: [], edges: [], groups: [] })
|
|
assert.strictEqual(seen.length, 2)
|
|
assert.strictEqual(changes.length, 3)
|
|
|
|
store.destroy()
|
|
const afterDestroy = store.patch({ nodes: [{ id: 'ignored' }] })
|
|
assert.strictEqual(afterDestroy.nodes.length, 0)
|
|
assert.strictEqual(changes.length, 3)
|
|
console.log('canvas store checks passed')
|