diff --git a/docs/tutorial-effects.test.js b/docs/tutorial-effects.test.js index de404e8..9ab10d3 100644 --- a/docs/tutorial-effects.test.js +++ b/docs/tutorial-effects.test.js @@ -1,7 +1,7 @@ import { test, expect } from 'vitest'; import u from 'updeep'; -import { action, Updux } from '../src/index.js'; +import { action, Updux, dux } from '../src/index.js'; const addTodoWithId = action('addTodoWithId'); const incNextId = action('incNextId'); @@ -42,3 +42,28 @@ test( "tutorial example", async () => { }) }); + +test( "catch-all effect", () => { + + let seen = []; + + const foo = new Updux({ + actions: { + one: null, + two: null, + }, + effects: { + '*': (api) => next => action => { + seen.push(action.type); + next(action); + } + } + } ); + + const store = foo.createStore(); + + store.dispatch.one(); + store.dispatch.two(); + + expect(seen).toEqual([ 'one', 'two' ]); +} ) diff --git a/docs/tutorial.md b/docs/tutorial.md index aaa6eb7..8315316 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -123,3 +123,11 @@ const store = todosDux.createStore(); store.dispatch.addTodo('Do the thing'); ``` +### Catch-all effect + +It is possible to have an effect match all actions via the special `*` token. +``` +todosUpdux.addEffect('*', () => next => action => { + console.log( 'seeing action fly by:', action ); + next(action); +});