Skip to content

Commit

Permalink
Add render tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Sep 12, 2017
1 parent 86a7eb2 commit 3498af0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 14 deletions.
33 changes: 33 additions & 0 deletions test/__setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,36 @@ global.window = window;
global.navigator = {
userAgent: "node.js"
};

global.CustomEvent = function CustomEvent(name, {detail}) {
this.name = name;
this.detail = detail;
}

global.spyFunction = function(orig = x => void x) {
let _args = [];

function fake(...args) {
_args = args;
return orig(...args);
}

fake.args = () => _args;
return fake;
}

global.mockElement = function() {
let _innerHTML = "";

return {
_dirty: new Map(),
set innerHTML(value) {
this._dirty.set("innerHTML", true);
_innerHTML = value;
},
get innerHTML() {
return _innerHTML;
},
dispatchEvent: spyFunction()
};
}
16 changes: 2 additions & 14 deletions test/connect_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ function counter(state = 0, action) {
}
}

function spy(orig = x => void x) {
let _args = [];

function fake(...args) {
_args = args;
return orig(...args);
}

fake.args = () => _args;
return fake;
}

suite("connect", function() {
let store;
let root;
Expand All @@ -36,7 +24,7 @@ suite("connect", function() {
test("passes the current state as the first argument", function() {
const { attach, connect, dispatch } = store;

const TestApp = spy();
const TestApp = spyFunction();
const ConnectedTestApp = connect(TestApp);
attach(ConnectedTestApp, root);
dispatch("INCREMENT");
Expand All @@ -47,7 +35,7 @@ suite("connect", function() {
test("passes other args after the state", function() {
const { attach, connect, dispatch } = store;

const TestComponent = spy();
const TestComponent = spyFunction();
const ConnectedTestComponent = connect(TestComponent);

function TestApp() {
Expand Down
93 changes: 93 additions & 0 deletions test/render_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require = require("@std/esm")(module, {esm: "js"});

const assert = require("assert");
const { createStore } = require("../index")

function counter(state = 0, action) {
switch (action) {
case "INCREMENT":
return state + 1;
default:
return state;
}
}

suite("render", function() {
let store;
let root;

setup(function() {
store = createStore(counter);
root = mockElement();
});

test("assigns innerHTML of the root", function() {
const { attach, dispatch } = store;

// Always returns the same output.
const TestApp = () => "Foo";
attach(TestApp, root);

dispatch("INCREMENT");
assert.equal(root.innerHTML, "Foo");
});

test("re-assigns when the output changes", function() {
const { attach, connect, dispatch } = store;

const TestApp = state => `Foo ${state}`;
const ConnectedTestApp = connect(TestApp);

attach(ConnectedTestApp, root);
assert.equal(root.innerHTML, "Foo 0");

dispatch("INCREMENT");
assert.equal(root.innerHTML, "Foo 1");

dispatch("INCREMENT");
assert.equal(root.innerHTML, "Foo 2");
});

test("dispatches an event when the output changes", function() {
const { attach, connect, dispatch } = store;

const TestApp = state => `Foo ${state}`;
const ConnectedTestApp = connect(TestApp);
attach(ConnectedTestApp, root);
assert.deepEqual(root.dispatchEvent.args(), [{
name: "render",
detail: 0
}]);

dispatch("INCREMENT");
assert.deepEqual(root.dispatchEvent.args(), [{
name: "render",
detail: 1
}]);

dispatch("INCREMENT");
assert.deepEqual(root.dispatchEvent.args(), [{
name: "render",
detail: 2
}]);
});

test("avoids re-assignment if the output doesn't change", function() {
const { attach, connect, dispatch } = store;

// Always returns the same output.
const TestApp = () => "Foo";
root._dirty.set("innerHTML", false);

attach(TestApp, root);
assert.equal(root._dirty.get("innerHTML"), true);

root._dirty.set("innerHTML", false);
dispatch("INCREMENT");
assert.equal(root._dirty.get("innerHTML"), false);

root._dirty.set("innerHTML", false);
dispatch("INCREMENT");
assert.equal(root._dirty.get("innerHTML"), false);
});
});

0 comments on commit 3498af0

Please sign in to comment.