Advertisement
Guest User

Untitled

a guest
May 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**@flow*/
  2. import {assert} from 'chai'
  3. import {concat, event, events, init, project, select, append, remove, update, value} from '../src/index'
  4.  
  5. describe('Big example', () => {
  6.   it('quick test', () => {
  7.     const
  8.       TYPE_NOTIFICATION = 'TYPE_NOTIFICATION',
  9.       TYPE_DELETE = 'TYPE_DELETE',
  10.       TYPE_UPDATE = 'TYPE_UPDATE',
  11.       event2 = {
  12.         type: TYPE_NOTIFICATION,
  13.         data: {
  14.           type: TYPE_UPDATE,
  15.           notifications: [
  16.             {id: 0, read: false},
  17.             {id: 1, read: true},
  18.             {id: 3, read: false}
  19.           ]
  20.         }
  21.       },
  22.       event1 = {
  23.         type: TYPE_NOTIFICATION,
  24.         data: {
  25.           type: TYPE_DELETE,
  26.           notifications: [1,2,3]
  27.         }
  28.       },
  29.       reducer = events(
  30.         event(
  31.           TYPE_NOTIFICATION,
  32.           project(
  33.             e => e.data,
  34.             event(
  35.               TYPE_DELETE,
  36.               ({counter, list}, {notifications}) => {
  37.                 list = list.filter(item => notifications.indexOf(item.id) == -1)
  38.                 return {
  39.                   counter: list.filter(item => !item.read).length,
  40.                   list: list
  41.                 }
  42.               }
  43.             ),
  44.             event(
  45.               TYPE_UPDATE,
  46.               (state) => {
  47.                 console.log('dfadasfasdf')
  48.                 return state
  49.               },
  50.               select('counter', (state, {notifications}) => state + notifications.filter(item => !item.read).length),
  51.               select('list', (state, {notifications}) => [...state, ...notifications])
  52.             )
  53.           )
  54.         ),
  55.         init({
  56.           counter: 0,
  57.           list: []
  58.         })
  59.       ),
  60.       initialState = reducer(undefined, {type: '@@point/INIT'}),
  61.       stateWithNotifications = reducer(initialState, event2),
  62.       stateAfterRemove = reducer(stateWithNotifications, event1)
  63.     console.log(initialState, stateWithNotifications, stateAfterRemove)
  64.   })
  65. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement