Exemplo n.º 1
0
 def test_combine_reducer_will_split_the_state_tree_in_sections_per_reducer_single(
         self):
     combined = combine_reducer([a_reducer])
     action_obj = "unittest"
     expected_state = pmap({"a_reducer": pmap({"actionA": action_obj})})
     actual_state = combined(action=action_obj)
     self.assertEqual(expected_state, actual_state)
Exemplo n.º 2
0
 def test_combined_reducer_will_not_change_the_state_if_nothing_to_do(self):
     action = ActionMock("ignore_action")
     combined = combine_reducer([e_reducer, d_reducer])
     initial_state = combined(action=action)
     untransformed_state = combined(action, initial_state)
     self.assertTrue(
         initial_state is untransformed_state,
         "State object has not the same reference! It must be updated!")
Exemplo n.º 3
0
 def test_combined_reducer_returns_new_state_object_when_one_part_tree_changes(
         self):
     initial_action = ActionMock("ignore_action")
     action = ActionMock("do")
     combined = combine_reducer([e_reducer, d_reducer])
     initial_state = combined(action=initial_action)
     transformed_state = combined(action, initial_state)
     self.assertTrue(
         initial_state is not transformed_state,
         "State object has not the same reference! It must be updated!")
Exemplo n.º 4
0
 def test_combine_reducer_create_one_part_tree_per_reducer(self):
     reducer_list = [a_reducer, b_reducer]
     combined = combine_reducer(reducer_list)
     action_obj = {"2": 3}
     actual_state = combined(action=action_obj)
     expected_state = pmap({
         "a_reducer": {
             "actionA": action_obj
         },
         "b_reducer": {
             "actionB": action_obj
         }
     })
     self.assertEqual(expected_state, actual_state)
Exemplo n.º 5
0
    def test_all_reducers_will_have_their_initial_state(self):
        def a_init_reducer(action=None, state=pmap({1: "a"})):
            return state

        def b_init_reducer(action=None, state=pmap({2: "b"})):
            return state

        combined = combine_reducer({
            "a_r": a_init_reducer,
            "b_r": b_init_reducer
        })
        new_state = combined(action="init_unittest")
        expected_state = pmap({"a_r": pmap({1: "a"}), "b_r": pmap({2: "b"})})
        self.assertEqual(expected_state, new_state)
Exemplo n.º 6
0
@counter_reducer.register(DEC_COUNTER)
def _(action, state):
    return state.set('count', state['count'] - 1)


@counter_reducer.register(SET_COUNTER)
def _(action, state):
    return state.set('count', action.payload)


@counter_reducer.register(ADD_COUNTER)
def _(action, state):
    return state.set('count', state['count'] + action.payload)


mystore = create_store(combine_reducer([counter_reducer, todo_reducer]))


def subscriber(s):
    pprint(thaw(s.state), indent=2)


mystore.subscribe(subscriber)

s1 = mystore.dispatch(SET_COUNTER(0))

s1 = mystore.dispatch(ADD_COUNTER(100))

for _ in range(10):
    s1 = mystore.dispatch(INC_COUNTER())
    s2 = mystore.dispatch(ADD_TODO("Say hello to the world"))
Exemplo n.º 7
0
 def test_state_split_keys_can_be_changed_through_dict(self):
     combined = combine_reducer({"a": a_reducer, "b": b_reducer})
     initial_state = combined(action={"unittest": "ignore"})
     self.assertItemsEqual(["a", "b"], initial_state.keys())
Exemplo n.º 8
0
 def test_combine_reducer_will_make_subtree_states_by_method_name(self):
     combined = combine_reducer([a_reducer, ReducerKlass().c_reducer])
     initial_state = combined(action="zz!!")
     self.assertItemsEqual(["a_reducer", "c_reducer"], initial_state.keys())
Exemplo n.º 9
0
 def test_combine_reducer_will_make_subtree_states_by_function_name(self):
     combined = combine_reducer([a_reducer, b_reducer])
     initial_state = combined(action="asdf")
     self.assertItemsEqual(["a_reducer", "b_reducer"], initial_state.keys())
Exemplo n.º 10
0
 def test_combine_reducer_returns_function(self):
     combined = combine_reducer([a_reducer, b_reducer])
     self.assertTrue(inspect.isfunction(combined),
                     "combine_reducer does not return a function")