-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from canjs/update
Refactor to use map events mixin
- Loading branch information
Showing
3 changed files
with
253 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,152 +1,186 @@ | ||
var QUnit = require("steal-qunit"); | ||
var Kefir = require("can-kefir"); | ||
var QUnit = require("steal-qunit"); | ||
var queues = require("can-queues"); | ||
var canReflect = require("can-reflect"); | ||
var batch = require("can-event/batch/batch"); | ||
|
||
QUnit.module("can-kefir", { | ||
QUnit.module("can-kefir"); | ||
|
||
}); | ||
|
||
QUnit.test("basics", 5, function() { | ||
QUnit.test("basics", function() { | ||
var EMITTER; | ||
var stream = Kefir.stream(function(emitter){ | ||
|
||
QUnit.expect(5); | ||
|
||
var stream = Kefir.stream(function(emitter) { | ||
EMITTER = emitter; | ||
}); | ||
|
||
|
||
var valueEventCount = 0; | ||
function valueHandler(value){ | ||
function valueHandler(value) { | ||
valueEventCount++; | ||
if(valueEventCount === 1) { | ||
if (valueEventCount === 1) { | ||
QUnit.equal(value, 1, "produced a value"); | ||
} else if (valueEventCount === 2) { | ||
QUnit.equal(value, 2, "produced a value"); | ||
} else { | ||
QUnit.ok(false, "should not be called"); | ||
} | ||
} | ||
canReflect.onKeyValue(stream,"value", valueHandler); | ||
canReflect.onKeyValue(stream, "value", valueHandler); | ||
|
||
EMITTER.value(1); | ||
|
||
QUnit.equal( canReflect.getKeyValue(stream,"value"), 1, "got initial value"); | ||
QUnit.equal(canReflect.getKeyValue(stream, "value"), 1, "got initial value"); | ||
|
||
EMITTER.value(2); | ||
canReflect.offKeyValue(stream,"value", valueHandler); | ||
EMITTER.value(3); | ||
canReflect.offKeyValue(stream, "value", valueHandler); | ||
|
||
EMITTER.value(3); | ||
|
||
var errorEventCount = 0; | ||
function errorHandler(value){ | ||
function errorHandler(value) { | ||
errorEventCount++; | ||
if(errorEventCount === 1) { | ||
if (errorEventCount === 1) { | ||
QUnit.equal(value, "a", "produced an error"); | ||
} else { | ||
QUnit.ok(false, "no more errors"); | ||
} | ||
} | ||
canReflect.onKeyValue(stream,"error", errorHandler); | ||
canReflect.onKeyValue(stream, "error", errorHandler); | ||
|
||
EMITTER.error("a"); | ||
QUnit.equal( | ||
canReflect.getKeyValue(stream, "error"), | ||
"a", | ||
"got initial value" | ||
); | ||
|
||
QUnit.equal( canReflect.getKeyValue(stream,"error"), "a", "got initial value"); | ||
|
||
canReflect.offKeyValue(stream,"error", errorHandler); | ||
canReflect.offKeyValue(stream, "error", errorHandler); | ||
EMITTER.error("b"); | ||
|
||
}); | ||
|
||
QUnit.test("streams have a cid", function(){ | ||
|
||
var stream = Kefir.stream(function(){}); | ||
QUnit.ok(stream._cid, "streams have a cid"); | ||
}); | ||
|
||
QUnit.test("callbacks are within a batch", function(){ | ||
QUnit.test("properties can be read without binding", function() { | ||
var EMITTER; | ||
var stream = Kefir.stream(function(emitter){ | ||
|
||
var property = Kefir.stream(function(emitter) { | ||
EMITTER = emitter; | ||
}); | ||
}).toProperty(); | ||
|
||
function valueHandler(){ | ||
QUnit.ok(batch.batchNum, "batchNum exists"); | ||
} | ||
canReflect.onKeyValue(stream,"value", valueHandler); | ||
property.onValue(function() {}); | ||
EMITTER.value(10); | ||
|
||
EMITTER.value(1); | ||
QUnit.equal( | ||
canReflect.getKeyValue(property, "value"), | ||
10, | ||
"got property value" | ||
); | ||
}); | ||
|
||
QUnit.test("properties can be read without binding", function(){ | ||
var EMITTER; | ||
var property = Kefir.stream(function(emitter){ | ||
EMITTER = emitter; | ||
QUnit.test("properties caches value/error correctly when unbound", function(assert) { | ||
var emitter; | ||
|
||
var stream = Kefir.stream(function(e) { | ||
emitter = e; | ||
}).toProperty(); | ||
|
||
property.onValue(function(){}); | ||
EMITTER.value(10); | ||
var handler = function noop() {}; | ||
canReflect.onKeyValue(stream, "value", handler); | ||
emitter.value(10); | ||
canReflect.offKeyValue(stream, "value", handler); | ||
|
||
QUnit.equal( canReflect.getKeyValue(property,"value"), 10, "got property value" ); | ||
assert.equal(canReflect.getKeyValue(stream, "value"), 10); | ||
assert.equal(canReflect.getKeyValue(stream, "error"), undefined); | ||
|
||
canReflect.onKeyValue(stream, "value", handler); | ||
assert.equal(canReflect.getKeyValue(stream, "value"), 10, "should be cached"); | ||
canReflect.offKeyValue(stream, "value", handler); | ||
}); | ||
|
||
QUnit.test("Kefir.emitterProperty", function(){ | ||
var stream = new Kefir.emitterProperty(); | ||
QUnit.test("callbacks are within a batch", function(assert) { | ||
var emitter; | ||
assert.expect(2); | ||
|
||
var stream = Kefir.stream(function(e) { | ||
emitter = e; | ||
}); | ||
|
||
var valueChangeCounter = 0; | ||
canReflect.onKeyValue(stream, "value", function onValueChange() { | ||
valueChangeCounter += 1; | ||
}); | ||
|
||
queues.batch.start(); | ||
emitter.value(1); | ||
assert.equal( | ||
valueChangeCounter, | ||
0, | ||
"handler should not be called while flushing is prevented" | ||
); | ||
queues.batch.stop(); | ||
|
||
assert.equal(valueChangeCounter, 1); | ||
}); | ||
|
||
QUnit.test("Kefir.emitterProperty", function() { | ||
var stream = new Kefir.emitterProperty(); | ||
|
||
var valueEventCount = 0; | ||
function valueHandler(value){ | ||
function valueHandler(value) { | ||
valueEventCount++; | ||
if(valueEventCount === 1) { | ||
if (valueEventCount === 1) { | ||
QUnit.equal(value, 1, "produced a value"); | ||
} else if (valueEventCount === 2) { | ||
QUnit.equal(value, 2, "produced a value"); | ||
} else { | ||
QUnit.ok(false, "should not be called"); | ||
} | ||
} | ||
canReflect.onKeyValue(stream,"value", valueHandler); | ||
|
||
canReflect.onKeyValue(stream, "value", valueHandler); | ||
stream.emitter.emit(1); | ||
|
||
QUnit.equal( canReflect.getKeyValue(stream,"value"), 1, "got initial value"); | ||
QUnit.equal(canReflect.getKeyValue(stream, "value"), 1, "got initial value"); | ||
|
||
canReflect.setKeyValue( stream, "value", 2); | ||
canReflect.offKeyValue(stream,"value", valueHandler); | ||
canReflect.setKeyValue(stream, "value", 2); | ||
canReflect.offKeyValue(stream, "value", valueHandler); | ||
stream.emitter.value(3); | ||
|
||
|
||
var errorEventCount = 0; | ||
function errorHandler(value){ | ||
function errorHandler(value) { | ||
errorEventCount++; | ||
if(errorEventCount === 1) { | ||
if (errorEventCount === 1) { | ||
QUnit.equal(value, "a", "produced an error"); | ||
} else { | ||
QUnit.ok(false, "no more errors"); | ||
} | ||
} | ||
canReflect.onKeyValue(stream,"error", errorHandler); | ||
canReflect.onKeyValue(stream, "error", errorHandler); | ||
|
||
stream.emitter.error("a"); | ||
QUnit.equal( | ||
canReflect.getKeyValue(stream, "error"), | ||
"a", | ||
"got initial value" | ||
); | ||
|
||
QUnit.equal( canReflect.getKeyValue(stream,"error"), "a", "got initial value"); | ||
|
||
canReflect.offKeyValue(stream,"error", errorHandler); | ||
canReflect.offKeyValue(stream, "error", errorHandler); | ||
stream.emitter.error("b"); | ||
}); | ||
|
||
QUnit.test("get behavior with constant stream", function(){ | ||
var stream = Kefir.stream(function(emit){ | ||
QUnit.test("get behavior with constant stream", function() { | ||
var stream = Kefir.stream(function(emit) { | ||
emit.value(1); | ||
}); | ||
|
||
canReflect.onKeyValue(stream, "value", function(newVal){ | ||
canReflect.onKeyValue(stream, "value", function(newVal) { | ||
QUnit.equal(newVal, 1, "got new Value"); | ||
}); | ||
|
||
QUnit.equal( canReflect.getKeyValue(stream,"value"), 1, "undefined"); | ||
QUnit.equal(canReflect.getKeyValue(stream, "value"), 1, "undefined"); | ||
}); | ||
|
||
QUnit.test("read emitter", function(){ | ||
QUnit.test("read emitter", function() { | ||
var stream = new Kefir.emitterProperty(); | ||
QUnit.equal( canReflect.getKeyValue(stream,"emitter"), stream.emitter, "got the emitter"); | ||
QUnit.equal( | ||
canReflect.getKeyValue(stream, "emitter"), | ||
stream.emitter, | ||
"got the emitter" | ||
); | ||
}); |
Oops, something went wrong.