Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mstdokumaci committed Oct 9, 2019
1 parent bc8c8ae commit a954ac1
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 20 deletions.
18 changes: 9 additions & 9 deletions src/api/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,48 +111,47 @@ const removeFromBranches = (branches, leaf, id, parent, keys, ownKeys, rT, stamp
}
})

const removeChildren = (branch, id, stamp) => {
const removeChildren = (branch, id, stamp, ownKey) => {
branch.leaves[id].keys.forEach(key => {
if (branch.leaves[key] !== null) {
remove(branch, key, stamp, true)
remove(branch, key, stamp, true, ownKey)
}
})
}

const remove = (branch, id, stamp, ignoreParent) => {
const remove = (branch, id, stamp, ignoreParent, ownKey = false) => {
emit(branch, id, 'data', 'remove', stamp)

removeChildren(branch, id, stamp)

const leaf = branch.leaves[id]
const rT = leaf.rT && leaf.val
let geniuneOwnKey = false

if (!ignoreParent && leaf.parent) {
let pLeaf = branch.leaves[leaf.parent]
if (!Object.prototype.hasOwnProperty.call(branch.leaves, leaf.parent)) {
pLeaf = addOverrideLeaf(branch, leaf.parent)
} else if (Object.prototype.hasOwnProperty.call(pLeaf, 'keys')) {
geniuneOwnKey = pLeaf.keys.delete(id)
ownKey = pLeaf.keys.delete(id)
}
pLeaf.stamp = stamp
addDataEvent(undefined, leaf.parent, 'remove-key', pLeaf.depth)
}

removeChildren(branch, id, stamp, ownKey)

if (branch.branches.length) {
removeFromBranches(
branch.branches, leaf, id, !ignoreParent && leaf.parent, [...leaf.keys], true, rT, stamp
)
}

if (geniuneOwnKey) {
if (ownKey) {
delete branch.leaves[id]
} else {
branch.leaves[id] = null
}

if (branch.persist) {
if (geniuneOwnKey) {
if (ownKey) {
branch.persist.remove(String(id))
} else {
branch.persist.store(String(id), null)
Expand All @@ -162,6 +161,7 @@ const remove = (branch, id, stamp, ignoreParent) => {
if (rT) {
removeReferenceFrom(branch, id, rT)
}

removeListenersSubscriptions(branch, id)
}

Expand Down
160 changes: 149 additions & 11 deletions test/network/subscriptions/circular-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,85 @@ test('network - subscriptions - circular references', t => {

const server = sMaster.listen(7070)

const cMaster = create()
const cMaster1 = create()
const cMaster2 = create()
const cMaster3 = create()

sMaster.branch.newBranchMiddleware = branchRoot => {
branchRoot.set({
user: {
name: 'User',
author: ['@', 'authors', 'authorA']
},
draft: {
bookB: {
title: 'Book B improved',
book: ['@', 'books', 'bookB']
}
}
})
}

server.switchBranch = async (_, branchKey, switcher) => switcher(branchKey)

cMaster.on('connected', val => {
if (val) {
cMaster.switchBranch('A')
} else {
let closedCount = 0
const closed = () => {
if (++closedCount >= 3) {
server.close()
t.end()
}
}

cMaster1.on('connected', val => {
if (val) {
cMaster1.switchBranch('A')
} else {
closed()
}
})

cMaster2.on('connected', val => {
if (val) {
cMaster2.switchBranch('B')
} else {
closed()
}
})

cMaster3.on('connected', val => {
if (val) {
cMaster3.switchBranch('C')
} else {
closed()
}
})

cMaster.get('user', {}).subscribe(user => {
cMaster1.get('user', {}).subscribe(user => {
if (user.get('name')) {
t.same(
user.serialize(),
{ name: 'User', author: ['@', 'authors', 'authorA'] },
'user.serialize() = { name: User, author: [@, authors, authorA] }'
'cm1.user.serialize() = { name: User, author: [@, authors, authorA] }'
)
}
})

cMaster.get('user').subscribe({ keys: ['author'], depth: 1 }, user => {
cMaster1.get('draft', {}).subscribe(draft => {
if (draft.get('bookB')) {
t.same(
draft.serialize(),
{
bookB: {
title: 'Book B improved',
book: ['@', 'books', 'bookB']
}
},
'cm1.draft.serialize() = correct'
)
}
})

cMaster1.get('user').subscribe({ keys: ['author'], depth: 2 }, user => {
const author = user.get('author')
if (author) {
author.get('books').subscribe({ depth: 2 }, books => {
Expand All @@ -79,13 +125,105 @@ test('network - subscriptions - circular references', t => {
bookA: ['@', 'books', 'bookA'],
bookB: ['@', 'books', 'bookB']
},
'books.serialize = correct'
'cm1.books.serialize = correct'
)

client.socket.close()
client1.socket.close()
})
}
})

cMaster2.get('user', {}).subscribe({ excludeKeys: ['author'] }, user => {
if (user.get('name')) {
t.same(
user.serialize(),
{ name: 'User', author: ['@', 'authors', 'authorA'] },
'cm2.user.serialize() = { name: User, author: [@, authors, authorA] }'
)
}
})

cMaster2.get('draft', {}).subscribe(draft => {
if (draft.get('bookB')) {
t.same(
draft.serialize(),
{
bookB: {
title: 'Book B improved',
book: ['@', 'books', 'bookB']
}
},
'cm2.draft.serialize() = correct'
)
}
})

cMaster2.get('user', {}).subscribe({ keys: ['author'], depth: 2 }, user => {
const author = user.get('author')
if (author) {
author.get('books').subscribe({ depth: 2 }, books => {
if (books.get('bookB')) {
t.same(
books.serialize(),
{
bookA: ['@', 'books', 'bookA'],
bookB: ['@', 'books', 'bookB']
},
'cm2.books.serialize = correct'
)

client2.socket.close()
}
})
}
})

cMaster3.get('user', {}).subscribe({ excludeKeys: ['author'] }, user => {
if (user.get('name')) {
t.same(
user.serialize(),
{ name: 'User', author: ['@', 'authors', 'authorA'] },
'cm3.user.serialize() = { name: User, author: [@, authors, authorA] }'
)
}
})

cMaster3.get('draft', {}).subscribe({ depth: 2 }, draft => {
if (draft.get('bookB')) {
t.same(
draft.serialize(),
{
bookB: {
title: 'Book B improved',
book: ['@', 'books', 'bookB']
}
},
'cm3.draft.serialize() = correct'
)
}
})

cMaster3.get('user', {}).subscribe({ keys: ['author'], depth: 2 }, user => {
const author = user.get('author')
if (author) {
author.get('books').subscribe({ depth: 2 }, books => {
if (books.get('bookB')) {
t.same(
books.serialize(),
{
bookA: ['@', 'books', 'bookA'],
bookB: ['@', 'books', 'bookB']
},
'cm3.books.serialize = correct'
)

client3.socket.close()
}
})
}
})

const client = cMaster.connect('ws://localhost:7070')
const client1 = cMaster1.connect('ws://localhost:7070')
const client2 = cMaster2.connect('ws://localhost:7070')
const client3 = cMaster3.connect('ws://localhost:7070')
})
1 change: 1 addition & 0 deletions test/reference/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ require('./merge')
require('./key-swapping')
require('./multi-branch')
require('./override')
require('./remove')
require('./corner-cases')
82 changes: 82 additions & 0 deletions test/reference/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const test = require('tape')
const { create } = require('../../dist')

test('references - remove', t => {
const state = create({
books: {
bookA: {
title: 'Book A',
author: ['@', 'authors', 'authorA']
}
},
authors: {
authorA: {
name: 'Author A',
books: {
bookA: ['@', 'books', 'bookA']
}
}
}
})

const branch = state.create({
myBooks: {
bookA: ['@', 'books', 'bookA']
},
myAuthors: {
authorA: ['@', 'authors', 'authorA']
}
})

t.same(
branch.serialize(),
{
authors: { authorA: { name: 'Author A', books: { bookA: ['@', 'books', 'bookA'] } } },
books: { bookA: { title: 'Book A', author: ['@', 'authors', 'authorA'] } },
myBooks: { bookA: ['@', 'books', 'bookA'] },
myAuthors: { authorA: ['@', 'authors', 'authorA'] }
},
'branch.serialize() = correct'
)

state.get(['books', 'bookA']).set(null)
state.get(['authors', 'authorA', 'books', 'bookA']).set(null)
branch.get(['myBooks', 'bookA']).set(null)

t.same(
branch.serialize(),
{
authors: { authorA: { name: 'Author A', books: {} } },
books: {},
myBooks: {},
myAuthors: { authorA: ['@', 'authors', 'authorA'] }
},
'branch.serialize() = correct'
)

state.get('books').set({
bookA: {
title: 'Book A',
author: ['@', 'authors', 'authorA']
}
})
state.get(['authors', 'authorA', 'books']).set({
bookA: ['@', 'books', 'bookA']
})
branch.get('myBooks').set({
bookA: ['@', 'books', 'bookA']
})

t.same(
branch.serialize(),
{
authors: { authorA: { name: 'Author A', books: { bookA: ['@', 'books', 'bookA'] } } },
books: { bookA: { title: 'Book A', author: ['@', 'authors', 'authorA'] } },
myBooks: { bookA: ['@', 'books', 'bookA'] },
myAuthors: { authorA: ['@', 'authors', 'authorA'] }
},
'branch.serialize() = correct'
)

t.end()
})

0 comments on commit a954ac1

Please sign in to comment.