Exemple #1
0
    def test_store_can_handle_combined_reducers_with_singledispatch(self):
        combined = combine_reducer([reducer_a, normal_reducer])
        expected_state = freeze({
            "normal_reducer": {
                "my_type": "normal"
            },
            "reducer_a": {
                "static": True
            }
        })
        store = create_store(combined)
        self.assertEqual(expected_state, store.state)

        dyn_action = DynamicAction()
        expected_state = freeze({
            "normal_reducer": {
                "my_type": "normal"
            },
            "reducer_a": {
                "static": True,
                "dynamic": "dynamo"
            }
        })
        actual_state = store.dispatch(dyn_action)
        self.assertEqual(expected_state, actual_state)
Exemple #2
0
 def test_middleware_will_be_called_on_first_user_dispatch(self):
     combined_reducer = combine_reducer({
         "n": normal_reducer,
         "a": reducer_a
     })
     store = create_store(combined_reducer,
                          enhancer=apply_middleware(logging_middleware))
     static_action = StaticAction(type="AppendAction", payload="900")
     store.dispatch(static_action)
     self.assertEqual(len(logger), 1)
     self.assertNotEqual(logger[0]["old_state"], store.state)
     self.assertEqual(
         logger[0]["old_state"],
         freeze({
             "a": pmap({"static": True}),
             "n": pmap({"my_type": "normal"}),
         }))
     self.assertEqual(logger[0]["new_state"], store.state)
     self.assertEqual(
         store.state,
         freeze({
             "a": {
                 "static": True,
                 'action': (static_action.type, static_action.payload)
             },
             "n": pmap({"my_type": "normal"}),
         }))
Exemple #3
0
 def test_store_can_apply_single_middleware_and_dispatch(self):
     combined_reducer = combine_reducer((normal_reducer, reducer_a))
     static_action = StaticAction(type="AppendAction", payload="900")
     store = create_store(combined_reducer,
                          enhancer=apply_middleware(logging_middleware))
     new_state = store.dispatch(static_action)
     self.assertEqual(new_state, store.state)
Exemple #4
0
 def test_store_can_handle_reducer_with_singledispatch(self):
     store = create_store(reducer_a)
     action = StaticAction(type="AppendAction", payload="900")
     new_state = store.dispatch(action)
     expected_state = freeze({
         "static": True,
         "action": ("AppendAction", "900")
     })
     self.assertEqual(new_state, store.state)
     self.assertEqual(new_state, expected_state)
Exemple #5
0
 def test_exception_handling_works_properly(self):
     global saga_runned
     saga_runned = False
     saga = Saga()
     saga.take_every(AsyncTodo, my_saga_fetcher)
     store = create_store(action_logger,
                          enhancer=apply_middleware(saga.saga_middleware))
     store.dispatch(fetch_async_creator("this_is_failing"))
     self.assertEqual(len(store.state["actions"]), 1)
     self.assertIsInstance(store.state["actions"][0], AsyncFail)
     self.assertTrue(saga_runned)
Exemple #6
0
 def test_non_saga_actions_get_passed_to_the_store(self):
     global saga_runned
     saga_runned = False
     saga = Saga()
     saga.take_every(AsyncTodo, my_saga_fetcher)
     store = create_store(action_logger,
                          enhancer=apply_middleware(saga.saga_middleware))
     store.dispatch(create_add_todo("Learn fancy"))
     self.assertEqual(len(store.state["actions"]), 1)
     self.assertIsInstance(store.state["actions"][0], Todo)
     self.assertFalse(saga_runned)
Exemple #7
0
 def test_only_resolved_saga_actions_are_dispatched(self):
     saga = Saga()
     saga.take_every(AsyncTodo, my_saga_fetcher)
     store = create_store(action_logger,
                          enhancer=apply_middleware(saga.saga_middleware))
     store.dispatch(fetch_async_creator())
     new_state = store.state
     actions_ = new_state["actions"]
     self.assertEqual(len(actions_), 1)
     self.assertIsInstance(actions_[0], Todo)
     self.assertEqual(actions_[0].payload, "None42")
     self.assertTrue(saga_runned)
Exemple #8
0
 def test_middleware_will_be_called_after_store_init(self):
     combined_reducer = combine_reducer({
         "n": normal_reducer,
         "a": reducer_a
     })
     store = create_store(combined_reducer,
                          enhancer=apply_middleware(logging_middleware))
     self.assertEqual(len(logger), 0)
     self.assertEqual(
         store.state,
         freeze({
             "a": pmap({"static": True}),
             "n": pmap({"my_type": "normal"}),
         }))
Exemple #9
0
class StoreImpl:
    the_store = create_store(combine_reducer([default, config]))

    @impl
    def get_default_reducer(self):
        return default

    @impl
    def get_config_reducer(self):
        return config

    @impl
    def get_store(self):
        return self.the_store
Exemple #10
0
    def test_only_notified_on_state_change(self):
        calls = {}

        def reducer(state, _):
            return state

        def listener():
            calls['listener'] = True

        store = create_store(reducer, {})
        store.subscribe(listener)

        store.dispatch({})

        self.assertFalse(calls.get('listener', False), 'not notified')
Exemple #11
0
    def test_only_notified_on_state_change(self):
        calls = {}

        def reducer(state, _):
            return state

        def listener():
            calls['listener'] = True

        store = create_store(reducer, {})
        store.subscribe(listener)

        store.dispatch({})

        self.assertFalse(calls.get('listener', False), 'not notified')
Exemple #12
0
    def test_create_dispatch_and_notify(self):
        calls = {}

        def reducer(state, action):
            if action['type'] == 'MYTYPE':
                calls['reducer'] = True
                new_state = deepcopy(state)
                new_state['reducer'] = True
                return new_state
            return state

        def listener():
            calls['listener'] = True

        store = create_store(reducer, {})
        store.subscribe(listener)

        store.dispatch({'type': 'MYTYPE'})

        self.assertTrue(store.state['reducer'], 'state not changed in reducer')
        self.assertTrue(calls['reducer'], 'Not reduced')
        self.assertTrue(calls['listener'], 'not notified')
Exemple #13
0
    def test_create_dispatch_and_notify(self):
        calls = {}

        def reducer(state, action):
            if action['type'] == 'MYTYPE':
                calls['reducer'] = True
                new_state = deepcopy(state)
                new_state['reducer'] = True
                return new_state
            return state

        def listener():
            calls['listener'] = True

        store = create_store(reducer, {})
        store.subscribe(listener)

        store.dispatch({'type': 'MYTYPE'})

        self.assertTrue(store.state['reducer'], 'state not changed in reducer')
        self.assertTrue(calls['reducer'], 'Not reduced')
        self.assertTrue(calls['listener'], 'not notified')
Exemple #14
0
 def test_can_apply_saga_middleware(self):
     saga = Saga()
     store = create_store(action_logger,
                          enhancer=apply_middleware(saga.saga_middleware))
     self.assertIsNotNone(store)
Exemple #15
0
    if isinstance(action, INC_COUNTER):
        new_state = state.set('count', state['count'] + 1)
        return new_state
    if isinstance(action, DEC_COUNTER):
        new_state = state.set('count', state['count'] - 1)
        return new_state
    if isinstance(action, SET_COUNTER):
        new_state = state.set('count', action.payload)
        return new_state
    if isinstance(action, ADD_COUNTER):
        new_state = state.set('count', state['count'] + action.payload)
        return new_state
    return state


mystore = create_store(reducer)


def subscriber(s):
    print(s.state)


mystore.subscribe(subscriber)

s = mystore.dispatch(SET_COUNTER(10))

s = mystore.dispatch(ADD_COUNTER(100))

for _ in range(10):
    s = mystore.dispatch(INC_COUNTER())