marcglasberg / async_redux

Flutter Package: A Redux version tailored for Flutter, which is easy to learn, to use, to test, and has no boilerplate. Allows for both sync and async reducers.
Other
230 stars 41 forks source link

Can not get waitUnitAction working #112

Closed pitazzo closed 3 years ago

pitazzo commented 3 years ago

Hey there, I can not get working the waitUnitAction method. I don't whether it is a bug or I'm using it in a wrong manner.

An action called FirstAction dispatches another action (SecondAction) under a given condition, and I would like to test that logic. In order to do so, I've written following code:

storeTester!.dispatch(FirstAction());
await storeTester!.wait(FirstAction);
await storeTester!.wait(SecondAction);

And as FirstAction dispatches SecondAction, the test passes. However, as in my real use case SecondAction has some params and I would like to also check whether those params come as expected. In order to do so, I wrote following test:

storeTester!.dispatch(FirstAction());
await storeTester!.wait(FirstAction);
await storeTester!.waitUnitAction(SecondAction, timeoutInSeconds: 10);

This test does not pass. Note I omitted the params in SecondAction in order to get a minimum example, so problem can not be related to the parameters not matching.

Am I using waitUnitAction as intended?

yamauchieduardo commented 3 years ago

Hi @Pitazzo!

I didn't find any method called waitUnitAction in the StoreTester class. Are you trying to report about the waitUntilAction method?

To test these scenarios you can use different approaches.

For the first scenario:

An action called FirstAction dispatches another action (SecondAction) under a given condition, and I would like to test that logic.

I recommend using Mock-action to check if some action was dispatched, like the SecondAction. You can find more details in: Mocking actions and reducers

For the second scenario I have some questions.

storeTester!.dispatch(FirstAction()); await storeTester!.wait(FirstAction); await storeTester!.waitUnitAction(SecondAction, timeoutInSeconds: 10);

When you wait the SecondAction using waitUntilAction, do you use the same object instance? Please note that waitUntilAction does NOT wait for a specific action TYPE. Instead, it waits for an equal OBJECT. This means you need the exact same action instance (equal by identity), or that you implement equals/hashcode in the SecondAction, using the class fields to compare the objects correctly.

If you don't implement equals/hashcode the compassion will be by the default equals. In the Dart language, if you create a object without implement equals and hashcode, the objects will be compared using the default Object == (equals), which is a comparison using identical, thus, the same instance.

To check if the equals/hashcode are working in the SecondAction objects with args, try:

assert(SecondAction(arg...) == SecondAction(arg...)); // Must return true

If you don't want to implement the equals/hashcode, I recommend using the waitUntilAllGetLast method and waiting the actions by Type.

TestInfo info = storeTester.waitUntilAllGetLast([FirstAction, SecondAction]);

Hope this helps!