From d12d114af898d74b549fc5d9eb071e7bd3120cca Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 26 Aug 2022 15:51:00 -0400 Subject: [PATCH] starting on the tutorial --- docs/tutorial.md | 62 +++++++++++++++++++++++++++++++++++++++++ docs/tutorial.test.js | 26 +++++++++++++++++ src/Updux.js | 5 ++-- src/createStore.test.js | 6 ++-- src/index.js | 1 + 5 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 docs/tutorial.md create mode 100644 docs/tutorial.test.js create mode 100644 src/index.js diff --git a/docs/tutorial.md b/docs/tutorial.md new file mode 100644 index 0000000..d766735 --- /dev/null +++ b/docs/tutorial.md @@ -0,0 +1,62 @@ +# Tutorial + +This tutorial walks you through the features of `Updux` using the +time-honored example of the implementation of Todo list store. + +We'll be using +[updeep](https://www.npmjs.com/package/updeep) to +help with immutability and deep merging, +but that's totally optional. If `updeep` is not your bag, +it can easily be substitued with, say, [immer][], [lodash][], or even +plain JavaScript. + +## Definition of the state + +To begin with, let's define that has nothing but an initial state. + +```js +import { Updux } from 'updux'; + +const todosDux = new Updux({ + initial: { + next_id: 1, + todos: [], + } +}); +``` + +Congrats! You have written your first Updux object. It +doesn't do a lot, but you can already create a store out of it, and its +initial state will be automatically set: + +```js +const store = todosDux.createStore(); + +console.log(store.getState()); // prints { next_id: 1, todos: [] } +``` + +## Add actions + +This is all good, but a little static. Let's add actions! + +```js +const todosDux = new Updux({ + initial: { + next_id: 1, + todos: [], + }, + { + addTodo: null, + todoDone: null, + } +}); +``` + +### Accessing actions + +Once an action is defined, its creator is accessible via the `actions` accessor. + +```js +console.log( todosDux.actions.addTodo('write tutorial') ); + // prints { type: 'addTodo', payload: 'write tutorial' } +``` diff --git a/docs/tutorial.test.js b/docs/tutorial.test.js new file mode 100644 index 0000000..1d7d02a --- /dev/null +++ b/docs/tutorial.test.js @@ -0,0 +1,26 @@ +import { test, expect } from 'vitest'; + +import { Updux } from '../src/index.js'; + +test( "basic checks", async () => { + + +const todosDux = new Updux({ + initial: { + next_id: 1, + todos: [], + }, + actions: { + addTodo: null, + todoDone: null, + } +}); + +const store = todosDux.createStore(); + +expect(store.getState()).toEqual({ next_id: 1, todos: [] }); + + expect(store.actions.addTodo("learn updux")).toMatchObject({ + type: 'addTodo', payload: 'learn updux' + }) +}); diff --git a/src/Updux.js b/src/Updux.js index 98b5a99..bf49936 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -93,7 +93,7 @@ export class Updux { store.actions = this.actions; -// store.selectors = this.selectors; + // store.selectors = this.selectors; // store.getState = R.merge( // store.getState, @@ -110,7 +110,7 @@ export class Updux { for (const action in this.actions) { store.dispatch[action] = (...args) => { - return store.dispatch(this.actions[action](...(args))); + return store.dispatch(this.actions[action](...args)); }; } @@ -118,5 +118,4 @@ export class Updux { return store; } - } diff --git a/src/createStore.test.js b/src/createStore.test.js index 0defe9e..e2a6524 100644 --- a/src/createStore.test.js +++ b/src/createStore.test.js @@ -1,17 +1,17 @@ import { test, expect } from 'vitest'; import { Updux } from './Updux.js'; -test( "basic createStore", async () => { +test('basic createStore', async () => { const foo = new Updux({ initial: { a: 1 }, actions: { a1: null, - } + }, }); const store = foo.createStore(); - expect(store.getState()).toEqual({a:1}); + expect(store.getState()).toEqual({ a: 1 }); console.log(store.actions.a1); diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..36ef6f4 --- /dev/null +++ b/src/index.js @@ -0,0 +1 @@ +export { Updux, dux } from './Updux.js';