Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed May 6, 2016
1 parent d6bb79d commit f4def00
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["es2015", "stage-2"],
"presets": ["react", "es2015", "stage-2"],
"plugins": ["transform-es2015-modules-commonjs"]
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
"babel-eslint": "^6.0.2",
"babel-plugin-transform-es2015-modules-commonjs": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"eslint": "^2.7.0",
"kefir.atom": "^2.2.1",
"mocha": "^2.4.5",
"nyc": "^6.4.0"
"nyc": "^6.4.0",
"react-addons-test-utils": "^15.0.2",
"react-dom": "^15.0.2"
},
"dependencies": {
"kefir": "^3.2.1",
Expand Down
80 changes: 63 additions & 17 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as Kefir from "kefir"
import * as R from "ramda"
import Atom from "kefir.atom"

import {classes} from "../src/kefir.react.html"
import React from "react"
import ReactDOM from "react-dom/server"

import K, {bind, classes, fromIds, fromKefir} from "../src/kefir.react.html"

function show(x) {
switch (typeof x) {
Expand All @@ -13,29 +17,71 @@ function show(x) {
}
}

function tryGet(s) {
let result
s.take(1).onValue(v => result = v)
return result
}
const testEq = (expr, expect) => it(`${expr} => ${show(expect)}`, done => {
const actual = eval(`(Atom, K, Kefir, R, bind, classes) => ${expr}`)(Atom, K, Kefir, R, bind, classes)
const check = actual => {
if (!R.equals(actual, expect))
throw new Error(`Expected: ${show(expect)}, actual: ${show(actual)}`)
done()
}
if (actual instanceof Kefir.Observable)
actual.take(1).onValue(check)
else
check(actual)
})

const testEq = (expr, lambda, expected) => it(
`${expr} equals ${show(expected)}`, () => {
const actual = lambda()
if (!R.equals(actual, expected))
throw new Error(`Expected: ${show(expected)}, actual: ${show(actual)}`)
})
const testRender = (vdom, expect) => it(`${expect}`, () => {
const actual = ReactDOM.renderToStaticMarkup(vdom)

if (actual !== expect)
throw new Error(`Expected: ${show(expect)}, actual: ${show(actual)}`)
})

describe("classes", () => {
testEq('classes()', () => classes(), {className: ""})
testEq('classes()', {className: ""})

testEq('classes("a")', () => classes("a"), {className: "a"})
testEq('classes("a")', {className: "a"})

testEq('classes("a", undefined, 0, false, "", "b")',
() => classes("a", undefined, 0, false, "", "b"),
{className: "a b"})

testEq('R.map(tryGet, classes("a", Kefir.constant("b")))',
() => R.map(tryGet, classes("a", Kefir.constant("b"))),
testEq('K(classes("a", Kefir.constant("b")), R.identity)',
{className: "a b"})
})

describe("K", () => {
testEq('K()', [])
testEq('K("a")', ["a"])
testEq('K("a", Kefir.constant("b"))', ["a", "b"])

testEq('K("a", x => x + x)', "aa")
testEq('K(Kefir.constant("a"), Kefir.constant(x => x + x))', "aa")

testEq('K([1, {y: {z: Kefir.constant("x")}}, Kefir.constant(3)], R.prepend(4))', [4, 1, {y: {z: "x"}}, 3])
})

describe("fromKefir", () => {
testRender(fromKefir(Kefir.constant(<p>Yes</p>)), '<p>Yes</p>')
})

describe("K.elems", () => {
testRender(<K.p>Just testing <K.span>constants</K.span>.</K.p>,
'<p>Just testing <span>constants</span>.</p>')

testRender(<K.div onClick={() => {}}
style={{display: "block",
color: Kefir.constant("red")}}>
{fromIds(Kefir.constant(["Hello"]), id =>
<span key={id}>{id}</span>)}
</K.div>,
"<div style=\"display:block;color:red;\"><span>Hello</span></div>")

testRender(<K.a href="#lol">
{Kefir.constant("Hello")} {Kefir.constant("world!")}
</K.a>,
'<a href="#lol">Hello world!</a>')
})

describe("bind", () => {
testEq('{const a = Atom(1); const x = bind({a}); x.onChange({target: {a: 2}}); return a}', 2)
})

0 comments on commit f4def00

Please sign in to comment.