Skip to content

Commit

Permalink
added switch combine tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim de Beer committed May 31, 2016
1 parent 4add380 commit 26fe510
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 29 deletions.
1 change: 0 additions & 1 deletion lib/subscribe/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = function collection (target, subs, update, tree, stamp) {
let keys = target._keys && target.keys()
if (keys && keys.length) {
for (let i = 0, len = keys.length; i < len; i++) {
console.log('key:', keys[i])
if (item(keys[i], target, subs, update, tree.$any, stamp)) {
changed = true
}
Expand Down
1 change: 0 additions & 1 deletion lib/subscribe/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = function item (key, target, subs, update, tree, stamp) {
} else if (key === '$test') {
return test(target, subs, update, tree, stamp)
} else {
console.log(' struct it', key)
return struct(
key,
target && key in target && target[key],
Expand Down
13 changes: 1 addition & 12 deletions lib/subscribe/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ module.exports = function parent (target, subs, update, tree, stamp) {
parentTree = tree.$parent = { _p: tree, _key: '$parent' }
addC(tree)
}
// here we need to walk the tree not the parent

const targetx = get(target, tree)
console.log(targetx, subs)

return diff(targetx, subs, update, parentTree, stamp)
return diff(get(target, tree), subs, update, parentTree, stamp)
} else if (parentTree && '$remove' in subs) {
remove('$parent', target, target, subs, update, tree, parentTree, stamp)
return true
Expand All @@ -29,23 +24,17 @@ function get (target, tree) {
if (tree._key !== '$parent') {
key = tree._key
tree = tree._p

if (key[0] !== '$') {
parentcounter--
// if(key == '$')
// key any is a problem
if (parentcounter < 0) {
path.unshift(key)
}
}
// }
// }
} else {
parentcounter++
tree = tree._p
}
}
console.log(' getParent:', path, target.path())
// can be greatly optmized
return (!path.length) ? target.getRoot() : target.getRoot().get(path)
}
Expand Down
8 changes: 1 addition & 7 deletions test/parent/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,7 @@ test('parent - $any', function (t) {
}
}
)

const r = s('initial subscription', [], {})

console.log(' ')
console.log(' ')
console.log('LETS FOCUS')
s('initial subscription', [], {})
s(
'create a collection',
[
Expand All @@ -129,6 +124,5 @@ test('parent - $any', function (t) {
],
{ focus: true }
)
console.log(r.tree)
t.end()
})
1 change: 1 addition & 0 deletions test/parent/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
require('./basic')
require('./references')
require('./switch')
1 change: 1 addition & 0 deletions test/parent/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ test('parent - references - double', function (t) {
}
)
s('initial subscription', [ { path: 'a/b/d', type: 'new' } ])
// maybe add some switch ref tests
t.end()
})
64 changes: 64 additions & 0 deletions test/parent/switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict'
const test = require('tape')
const subsTest = require('../util')

test('parent - $switch', function (t) {
const subscription = {
nest: {
field: {
$switch: {
exec (state, type, stamp, subs, tree, sType) {
console.log(state)
if (state.key === 'a') {
return 'optionA'
} else if (state.key === 'b') {
return 'optionB'
} else if (state.key === 'c') {
return 'optionC'
}
},
optionA: { $parent: { bla: { val: true } } },
optionB: { $parent: { $parent: { etc: { val: true } } } },
optionC: { val: true } // should fire wtf...
}
}
}
}

const s = subsTest(
t,
{
etc: true,
nest: {
bla: { val: true },
a: {},
b: {},
c: 'hello',
field: '$root.nest.a'
}
},
subscription
)

const r = s('initial subscription', [ { path: 'nest/bla', type: 'new' } ])
t.same(r.tree.nest.field.$switch.$c, { $parent: 'parent' }, 'got correct $c')
s(
'change to c (remove parent subs)',
[ { path: 'nest/c', type: 'new' } ],
{ nest: { field: '$root.nest.c' } }
)
t.equal(r.tree.nest.field.$switch.$c, void 0, 'removed field.$switch.$c')
s(
'change to b (double parent)',
[ { path: 'etc', type: 'new' } ],
{ nest: { field: '$root.nest.b' } }
)
s(
'change to c (remove parent subs)',
[ { path: 'nest/c', type: 'new' } ],
{ nest: { field: '$root.nest.c' } }
)
t.equal(r.tree.nest.field.$switch.$c, void 0, 'removed field.$switch.$c')
t.equal(r.tree.nest.field.$c, void 0, 'removed field.$c')
t.end()
})
8 changes: 0 additions & 8 deletions test/switch/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ test('switch - basic', (t) => {
)
const result = s('initial subscription', [], {})

console.log('-> 1 heeere we go', result.state.field)

s(
'set field to a',
[
Expand All @@ -42,8 +40,6 @@ test('switch - basic', (t) => {
{ field: '$root.a' }
)

console.log('-> 2 heeere we go 2', result.state.field.listensOnBase.keys())

s(
'set field to b',
[
Expand All @@ -54,8 +50,6 @@ test('switch - basic', (t) => {
{ field: '$root.b' }
)

console.log('3 heeere we go', result.state.field.listensOnBase.keys())

s(
'set field to false',
[
Expand All @@ -65,7 +59,6 @@ test('switch - basic', (t) => {
{ field: false }
)

console.log('4 heeere we go', result.state.field.listensOnBase.keys())
s(
'set field to a',
[
Expand All @@ -75,7 +68,6 @@ test('switch - basic', (t) => {
{ field: '$root.a' }
)

console.log('REMOVE!') // needs tests in observable clearly!
s(
'remove field ',
[
Expand Down

0 comments on commit 26fe510

Please sign in to comment.