imp: work on store, refactor, improve todoapp

This commit is contained in:
Géry Debongnie
2019-03-22 09:52:48 +01:00
parent a236ae396a
commit d48cb5aae4
9 changed files with 150 additions and 24 deletions
+55 -2
View File
@@ -1,5 +1,11 @@
import { Store } from "../src/store";
import { nextMicroTick } from "./helpers";
import { Store, connect } from "../src/store";
import { Component, Env } from "../src/component";
import {
nextMicroTick,
makeTestWEnv,
makeTestFixture,
nextTick
} from "./helpers";
describe("basic use", () => {
test("commit a mutation", () => {
@@ -54,3 +60,50 @@ describe("basic use", () => {
expect(updateCounter).toBe(1);
});
});
describe("connecting a component to store", () => {
let fixture: HTMLElement;
let env: Env;
beforeEach(() => {
fixture = makeTestFixture();
env = makeTestWEnv();
});
afterEach(() => {
fixture.remove();
});
class App extends Component<any, any, any> {
inlineTemplate = `
<div>
<t t-foreach="props.todos" t-as="todo">
<t t-widget="Todo" t-props="todo"/>
</t>
</div>`;
widgets = { Todo };
}
class Todo extends Component<any, any, any> {
inlineTemplate = `<span><t t-esc="props.msg"/></span>`;
}
test("connecting a component works", async () => {
const state = { todos: [] };
const mutations = {
addTodo(state, msg) {
state.todos.push({ msg });
}
};
const TodoApp = connect(s => s)(App);
const store = new Store({ state, mutations });
(<any>env).store = store;
const app = new TodoApp(env);
await app.mount(fixture);
expect(fixture.innerHTML).toMatchSnapshot();
store.commit("addTodo", "hello");
await nextTick();
expect(fixture.innerHTML).toMatchSnapshot();
});
});