Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msglist test: Backfill tests for rest of model; overhaul test setup #260

Merged
merged 12 commits into from
Aug 9, 2023

Conversation

gnprice
Copy link
Member

@gnprice gnprice commented Aug 9, 2023

This is part of #78. That issue will involve substantial refactoring on MessageListView, which makes it a good time to have tests in place.

The final commit here adds tests for all the exposed methods of MessageListView that lack them, namely fetch, maybeAddMessage, and reassemble. That was in fact the origin of this PR — I wrote those tests a few weeks ago as part of my draft branch for #78. (Which I've now picked back up to bring toward merge, after having held off for a while because its changes in lib/ conflicted with other PRs and I wanted to handle the conflicts myself rather than pushing them to those PRs' authors to deal with.) The 11 preceding commits convert all the existing tests in this file to work the same way as those new tests.

In particular the first 9 of these 12 commits are a sort of followup to #258, making further cleanups and simplifications to those existing tests, compared to the patterns that were set in #238.

The remaining two commits make deeper changes:


msglist test: Overhaul setup of tests, with shared helpers

All the tests in this file are going to have a common structure
to the objects they operate on: chiefly, a MessageListView.
So we can hoist those up to the level of main, which enables
the tests to share helper functions that act on those objects.

In principle this means a state leak between tests. We mitigate
that by resetting all the shared variables at the same time,
in the prepare function that each test calls at the outset.

We could get a similar effect by putting these shared variables and
helpers as fields and methods on a class, and having each test case
make its own instance of the class -- its own "tester" object, akin to
the WidgetTester object that testWidgets provides to its test body.
That'd provide more encapsulation, and would be a good strategy if
for example this logic were to be shared across several test files.
But I think this test file can get away without it.

The immediate payoff of the shared helpers is that they let us
centralize the logic for checking that the view-model has, or hasn't,
notified its listeners. That simplifies each test case, plus it
lets us make that logic slightly more sophisticated (at O(1) rather
than O(n) cost in the source code): we now detect duplicate calls too.

Coming up, we'll add further centralized useful logic to these helpers,
and add some tests that benefit from the split between prepare
and prepareMessages.


msglist test: Systematically check invariants of MessageListView

This takes further advantage of having these centralized helpers.

gnprice added 12 commits August 8, 2023 20:09
We don't have any need for these here -- the code under test isn't
mounting any widgets (let alone GlobalStoreWidget) nor otherwise
interacting with the global state.  Leave them out, simplifying the tests.
The code under test doesn't actually look at this information --
the only thing it uses the PerAccountStore for is to (a) get an
API connection, and (b) unregister itself in `dispose`.
So simplify the tests by leaving it out.

If later the code under test does start needing this information
so that we do need to set it up in the tests, we can revisit then
and may find a bit simpler of a way to arrange it.
Now that we don't have setup code trying to coordinate on using
the same arbitrary user ID as any other code, the two test cases
that happen to need to mention a user ID can just mention one inline.
That avoids the appearance of this `userId` constant potentially
having subtle wider interactions.
These values are, as the doc on their class says, view-models
for message lists.  That's a type of view-model, not a type of
message list; or more briefly, it's a type of model.
None of these tests actually care what the narrow is.
So use the narrow that means we can have any kind of message there
without causing any inconsistency.
Now that the narrow is AllMessagesNarrow, there's no need for these
test messages to be in any particular stream, so we can simplify
a bit further.
All the tests in this file are going to have a common structure
to the objects they operate on: chiefly, a MessageListView.
So we can hoist those up to the level of `main`, which enables
the tests to share helper functions that act on those objects.

In principle this means a state leak between tests.  We mitigate
that by resetting all the shared variables at the same time,
in the `prepare` function that each test calls at the outset.

We could get a similar effect by putting these shared variables and
helpers as fields and methods on a class, and having each test case
make its own instance of the class -- its own "tester" object, akin to
the WidgetTester object that `testWidgets` provides to its test body.
That'd provide more encapsulation, and would be a good strategy if
for example this logic were to be shared across several test files.
But I think this test file can get away without it.

The immediate payoff of the shared helpers is that they let us
centralize the logic for checking that the view-model has, or hasn't,
notified its listeners.  That simplifies each test case, plus it
lets us make that logic slightly more sophisticated (at O(1) rather
than O(n) cost in the source code): we now detect duplicate calls too.

Coming up, we'll add further centralized useful logic to these helpers,
and add some tests that benefit from the split between `prepare`
and `prepareMessages`.
This takes further advantage of having these centralized helpers.
For the more recently-added functionality, we'd written tests
along the way.  But we didn't have tests for the logic we
built earlier, which is some of the most core functionality.
Add tests for all that logic too.
@chrisbobbe chrisbobbe merged commit c5500b1 into zulip:main Aug 9, 2023
@chrisbobbe
Copy link
Collaborator

Thanks, LGTM! Merged.

@gnprice gnprice deleted the pr-msglist-test branch August 9, 2023 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants