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

Make state.narrows an Immutable.Map. #4187

Merged
merged 15 commits into from
Jul 21, 2020

Conversation

chrisbobbe
Copy link
Contributor

@chrisbobbe chrisbobbe commented Jul 10, 2020

An instance of #3949 and #3950, this is planned followup to #4047. See #4047 (comment).

Still getting all these tests type-checked, but thought I'd push my work today. 🙂

Done! Well, with a few things to note:

I've called out all of these cherry-picks with a note at the bottom of the commit message.

@chrisbobbe chrisbobbe marked this pull request as draft July 10, 2020 01:38
@chrisbobbe chrisbobbe marked this pull request as ready for review July 10, 2020 21:40
@chrisbobbe chrisbobbe changed the title [WIP] Make state.narrows an Immutable.Map. Make state.narrows an Immutable.Map. 🎉 Jul 10, 2020
@chrisbobbe chrisbobbe changed the title Make state.narrows an Immutable.Map. 🎉 Make state.narrows an Immutable.Map. Jul 10, 2020
@chrisbobbe chrisbobbe requested a review from gnprice July 10, 2020 21:48
@chrisbobbe
Copy link
Contributor Author

chrisbobbe commented Jul 20, 2020

Just resolved some conflicts; this is ready for review again.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

Before actually merging the headline change, I want to spend a bit more time looking at it, and playing around with it and thinking about ways to test.

But clearly quite a lot of the work of this was in the commits to get all those tests well-typed. I've read through all those now, and I'll go ahead and merge them!

Comment on lines 123 to 125
const anchor = getMessagesForNarrow(state, privateNarrow('john@example.com'));

const expectedState = deepFreeze([{ id: 123 }]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better -- thanks for cleaning these up 🙂

I think with these names you're looking at the results of two different cases of someone copy-pasting without thinking it through (or perhaps not understanding.) The expectedState is, as you say, a reasonable name in many reducer tests.

The anchor... I suspect comes from the last few test cases below, testing things like getFirstMessageId. There, the result is at least plausibly an "anchor" -- it's a message ID that we're going to use to identify a spot in the message stream. And then those got copied elsewhere, by a contributor who didn't understand what it meant and in a period where we weren't doing meaningful code review.

OK, and having said that I was curious to find out. 🙂 Indeed -- and another layer is that those tests for getFirstMessageId etc. were originally for a function called... getAnchor. a89d897 changed that. And before then, 8dd2081 propagated the anchor name to these other test cases.

Comment on lines +61 to +65
display_recipient: [
eg.displayRecipientFromUser(eg.selfUser),
eg.displayRecipientFromUser(userMark),
eg.displayRecipientFromUser(userJohn),
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are probably a sign that it'd be helpful to refactor eg.displayRecipientFromUser to be called, say, eg.displayRecipientFromUsers -- and to take User[] and return PmRecipientUser[], and be just a map around what it currently is.

(Maybe even both versions should exist.)

In an upcoming commit, we'll change the Dispatch type in this libdef
to play nicely with our async redux-thunk actions. I don't see an
easy way to give the libdef the flexibility to give different types
depending on what middleware is used, so, we hard-code it for
ourselves, since we know we'll be using redux-thunk for the
foreseeable future.

This means we probably can't reasonably make a PR to FlowTyped, even
though this is (surprisingly) one of the few libraries we use that
does have a libdef hosted by FlowTyped. Ah, well.

Here, we just move the file, delete the metadata at the top, and run
our auto-formatting.

[cherry-picked from zulip#4171]
This version [1] is marked for a higher version of Flow than we're
using now (v0.104 and above), but there aren't any errors on our
Flow version (v0.98). So, take it.

This gets us a particular change to the Dispatch type [2] that goes
in the right direction...until we clobber it with our own Dispatch
that accounts for async redux-thunk actions, I suppose.

Ah well, there are a few minor changes that we might like to have,
and this is a later version, so, might as well.

[1]: https://github.com/flow-typed/flow-typed/blob/c4f47bdda/definitions/npm/redux-mock-store_v1.2.x/flow_v0.104.x-/redux-mock-store_v1.2.x.js
[2]: flow-typed/flow-typed#3803

[cherry-picked from zulip#4171]
As foreshadowed in the preceding commits, we can't really expect a
FlowTyped-hosted libdef to have the flexibility to give us the right
types for Dispatch on the condition that we pass `thunk` as a piece
of middleware.

We're decidedly using `thunk`, so, hard-code support for it here by
allowing dispatch to be called with a redux-thunk async action.

We're pretty consistent with having correct types for such an
action, so not much is lost by typing its arguments loosely as
`Function, Function`, and from some attempts, it seems much easier
than nailing down something exactly right.

[cherry-picked from zulip#4171]
It looks like this was added with the idea that we'd use it in more
places than we actually have. It adds a slight bit of convenience;
with this mock, we can avoid having to write
`configureStore([thunk])` a bunch of times.

But that's a very small amount of boilerplate.

The mock doesn't seem to have any other purpose that might be part
of a unit-testing strategy.

Removing it makes it possible to use the libdef without contorting
it more drastically than we already have.

[cherry-picked from zulip#4171]
The added fields were gleaned from a real-life example observed with
`redux-logger`.

[cherry-picked from zulip#4171]
Flow won't allow these inputs (a GlobalState of `undefined`, or an
action of `{}`).
…les.

`expectedState` makes sense in tests for reducers, but not really
for selectors. The *input* of a selector is the GlobalState, but the
output is...whatever we want to do with that input. Sometimes we
just pick things directly from the state; sometimes we do that and
then nudge it around until it's in a shape we want.

Also rename `anchor` to the more general `result`; I'm not really
sure what "anchor" is meant to refer to in each of these instances.
We'll use this in pmConversationsSelectors-test in an upcoming
commit.
@gnprice gnprice merged commit afd013b into zulip:master Jul 21, 2020
@gnprice
Copy link
Member

gnprice commented Jul 21, 2020

... Err, I guess that came out slightly wrong, in that this PR is marked as merged but with that headline final commit dropped, oops. Please go ahead and open a new PR with (a rebased version of) 1a80243.

The set of commits merged to master were exactly what I intended, though 🙂

chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this pull request Jul 21, 2020
@chrisbobbe
Copy link
Contributor Author

chrisbobbe commented Jul 21, 2020

Thanks for the review and merge! OK, opened #4201.

@chrisbobbe chrisbobbe deleted the pr-narrows-immutable-map branch July 21, 2020 18:00
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this pull request Jul 29, 2020
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this pull request Sep 22, 2020
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