How to write accurate tests for state mutation with custom equality implementation? #541
-
We have a state that looks like this:
For performance considerations and because we're using
Now the problem is, TCA's test functions seem to internally rely on The interesting thing is that the diffing logic implemented for showing the error messages / console logs seems to be identifying all differences correctly. So if I for example provide an incorrect value for |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
I'm gonna convert this to a discussion since it's more appropriate there. Where are the performance problems you encounter with the default |
Beta Was this translation helpful? Give feedback.
-
It's non-TCA related. Basically in our reducer we need the ability to update a subset of items in the original set based on the received action. Having this setup enables us to do something like this:
The implementation of |
Beta Was this translation helpful? Give feedback.
-
Gotcha. I can only think of two ways around this right now: 1.) You can create a wrapper type around 2.) Alternatively, you can go back to the automatically synthesized Neither are ideal, but I would lean towards the latter. In the first suggestion you will be writing wrapper types with custom |
Beta Was this translation helpful? Give feedback.
-
Got it. Thanks for the suggestions. I agree the latter would be a safer approach. Will try and see if I can refactor this in an efficient way. |
Beta Was this translation helpful? Give feedback.
-
By the way, out of curiosity, cannot the logic of detecting the diffs for logging purposes be somehow extended to be used for the actual diffing instead of simply relying on |
Beta Was this translation helpful? Give feedback.
-
We don't think it's a good idea to rely on the stringified output of types in order to test for equality in tests. Those outputs are not guaranteed to be correct or exhaustive. You can check out the code to see that it's just a big mess of reflection in order to try to get something reasonable out of any type, but there are a few known ways it can go wrong (such as Objective-C enums) and probably a lot of unknown ways it can go wrong. And I understand it may have seemed like the testing functions were broken when you witnessed that behavior, but we feel that TCA works best when value types are kept as simple as possible, and that includes keeping the automatically synthesized equality check. It's worth mentioning that this isn't really a TCA issue. If you were using this code in a non-TCA application and you wrote a test with |
Beta Was this translation helpful? Give feedback.
-
Fair. Thank you! I'm gonna close this based on the above. |
Beta Was this translation helpful? Give feedback.
Gotcha.
I can only think of two ways around this right now:
1.) You can create a wrapper type around
State
that provides a customEquatable
conformance so that you can restore the per-field equality check, and then in tests you can do something like.scope(state: Wrapper.init)
on theTestStore
to wrap state in that type. That will make it so that tests use the full equatable check.2.) Alternatively, you can go back to the automatically synthesized
Equatable
and move the efficiency concerns to the application layer. You can write all your algorithms to not leverageItem
's equatable conformance and instead just look at identifiers.Neither are ideal, but I would lean towards the latter. In…