Skip to content

Commit

Permalink
add tests for Zalgo behavior (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur authored Jul 22, 2016
1 parent e9552f2 commit 31a7767
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"npm-run-all": "^1.6.0",
"object-assign": "^4.1.0",
"own-enumerable-keys": "^1.0.0",
"pull-defer": "^0.2.2",
"pull-delay": "^1.0.1",
"sheet-router": "^3.0.1",
"standard": "^7.0.1",
Expand Down
91 changes: 91 additions & 0 deletions test/zalgo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const test = require('tape')
const defer = require('pull-defer')
const extend = require('extend')
const inu = require('../')
const pull = inu.pull

test('infinite loop if sync dispatch, model always new, and effect needs to run async', function (t) {
var i = 0
var ended = false
const app = {
init: function () {
return { model: {}, effect: 'INIT' }
},
update: function (model, action) {
switch (action) {
case 'TICK':
return { model: {}, effect: 'NEXT' }
case 'DONE':
return { model: false }
}
return { model: model }
},
view: function (model, dispatch) {
if (ended) return
if (i++ > 1000) {
ended = true
t.ok(true)
return t.end()
}
if (model) return dispatch('TICK')
t.ok(false)
},
run: function (effect) {
switch (effect) {
case 'NEXT':
const deferred = defer.source()
process.nextTick(function () {
deferred.resolve(pull.values(['DONE']))
})
return deferred
}
}
}
const sources = inu.start(app)
})

test('no infinite loop if async dispatch, model always new, and effect needs to run async', function (t) {
var i = 0
var ended = false
const app = {
init: function () {
return { model: {}, effect: 'INIT' }
},
update: function (model, action) {
switch (action) {
case 'TICK':
return { model: {}, effect: 'NEXT' }
case 'DONE':
return { model: false }
}
return { model: model }
},
view: function (model, dispatch) {
if (ended) return
if (i++ > 1000) {
ended = true
t.ok(false)
return t.end()
}
if (model) {
process.nextTick(() => {
return dispatch('TICK')
})
return
}
t.ok(true)
t.end()
},
run: function (effect) {
switch (effect) {
case 'NEXT':
const deferred = defer.source()
process.nextTick(function () {
deferred.resolve(pull.values(['DONE']))
})
return deferred
}
}
}
const sources = inu.start(app)
})

0 comments on commit 31a7767

Please sign in to comment.