Skip to content

Commit

Permalink
msglist tests [nfc]: Use Redux state in inputs, not background data
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe committed Mar 10, 2022
1 parent e801cdf commit 303d268
Showing 1 changed file with 95 additions and 52 deletions.
147 changes: 95 additions & 52 deletions src/webview/__tests__/generateInboundEventEditSequence-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ import {
keyFromNarrow,
ALL_PRIVATE_NARROW,
} from '../../utils/narrow';
import type { Message, Outbox, FlagsState } from '../../types';
import type {
PerAccountState,
GlobalState,
Narrow,
Message,
Outbox,
FlagsState,
} from '../../types';
import { assumeSecretlyGlobalState } from '../../reduxTypes';
import type { ReadWrite } from '../../generics';
import { getEditSequence } from '../generateInboundEventEditSequence';
import { applyEditSequence } from '../js/handleInboundEvents';
import getMessageListElements from '../../message/getMessageListElements';
import { getGlobalSettings, getDebug } from '../../selectors';
import { getBackgroundData } from '../backgroundData';

// Tell ESLint to recognize `check` as a helper function that runs
// assertions.
Expand Down Expand Up @@ -184,10 +194,9 @@ const pmMessages6 = [
}),
];

const plusBackgroundData = {
...eg.plusBackgroundData,
streams: new Map([stream1, stream2].map(s => [s.stream_id, s])),
};
const baseState = eg.reduxStatePlus({
streams: [stream1, stream2],
});

/**
* Highlight changes in content-HTML generation.
Expand Down Expand Up @@ -224,7 +233,17 @@ const plusBackgroundData = {
* and `messageListElementHtml`.
*/
describe('messages -> piece descriptors -> content HTML is stable/sensible', () => {
const check = ({ backgroundData = plusBackgroundData, narrow, messages }) => {
const check = ({
state = eg.plusReduxState,
globalState = assumeSecretlyGlobalState(state),
narrow,
messages,
}: {|
state?: PerAccountState,
globalState?: GlobalState,
narrow: Narrow,
messages: $ReadOnlyArray<Message>,
|}) => {
invariant(
messages.every((message, i, allMessages) => {
const prevMessage: Message | void = allMessages[i - 1];
Expand All @@ -245,8 +264,24 @@ describe('messages -> piece descriptors -> content HTML is stable/sensible', ()
// Simulate applying an edit-sequence event to the DOM.
applyEditSequence(
getEditSequence(
{ backgroundData, elements: [], _: mock_ },
{ backgroundData, elements: getMessageListElements(messages, narrow), _: mock_ },
{
backgroundData: getBackgroundData(
state,
getGlobalSettings(globalState),
getDebug(globalState),
),
elements: [],
_: mock_,
},
{
backgroundData: getBackgroundData(
state,
getGlobalSettings(globalState),
getDebug(globalState),
),
elements: getMessageListElements(messages, narrow),
_: mock_,
},
),
);

Expand Down Expand Up @@ -417,15 +452,10 @@ describe('messages -> piece descriptors -> content HTML is stable/sensible', ()

check({
narrow: HOME_NARROW,
backgroundData: {
...eg.plusBackgroundData,
ownUser: stableSelfUser,
allUsersById: new Map([
[singleMessageSender.user_id, singleMessageSender],
[stableSelfUser.user_id, stableSelfUser],
[stableOtherUser.user_id, stableOtherUser],
]),
},
state: eg.reduxStatePlus({
realm: { ...eg.plusReduxState.realm, user_id: stableSelfUser.user_id },
users: [singleMessageSender, stableSelfUser, stableOtherUser],
}),
messages: [
{
...baseSingleMessage,
Expand Down Expand Up @@ -486,14 +516,14 @@ describe('messages -> piece descriptors -> content HTML is stable/sensible', ()
});
});

Object.keys(eg.baseReduxState.flags).forEach(flag => {
Object.keys(eg.plusReduxState.flags).forEach(flag => {
test(`message with flag: ${flag}`, () => {
const flags: ReadWrite<FlagsState> = { ...eg.plusBackgroundData.flags };
const flags: ReadWrite<FlagsState> = { ...eg.baseBackgroundData.flags };
flags[flag] = { [baseSingleMessage.id]: true };
check({
narrow: HOME_NARROW,
messages: [baseSingleMessage],
backgroundData: { ...eg.plusBackgroundData, flags },
state: eg.reduxStatePlus({ flags }),
});
});
});
Expand All @@ -502,10 +532,9 @@ describe('messages -> piece descriptors -> content HTML is stable/sensible', ()
check({
narrow: HOME_NARROW,
messages: [baseSingleMessage],
backgroundData: {
...eg.plusBackgroundData,
state: eg.reduxStatePlus({
mutedUsers: Immutable.Map([[baseSingleMessage.sender_id, 1644366787]]),
},
}),
});
});
});
Expand All @@ -524,19 +553,39 @@ describe('getEditSequence correct for interesting changes', () => {
return msglistElementsDiv.cloneNode(true);
};

type CheckArg = {|
state?: PerAccountState,
globalState?: GlobalState,
narrow?: Narrow,
messages: $ReadOnlyArray<Message>,
|};

const check = (
// TODO: Test with a variety of different things in background data
// TODO: Test with a variety of different things in state/globalState
{
backgroundData: oldBackgroundData = plusBackgroundData,
state: oldState = baseState,
globalState: oldGlobalState = assumeSecretlyGlobalState(oldState),
narrow: oldNarrow = HOME_NARROW,
messages: oldMessages,
},
}: CheckArg,
{
backgroundData: newBackgroundData = plusBackgroundData,
state: newState = baseState,
globalState: newGlobalState = assumeSecretlyGlobalState(newState),
narrow: newNarrow = HOME_NARROW,
messages: newMessages,
},
}: CheckArg,
) => {
const oldBackgroundData = getBackgroundData(
oldState,
getGlobalSettings(oldGlobalState),
getDebug(oldGlobalState),
);
const newBackgroundData = getBackgroundData(
newState,
getGlobalSettings(newGlobalState),
getDebug(newGlobalState),
);

const oldElements = getMessageListElements(oldMessages, oldNarrow);
const newElements = getMessageListElements(newMessages, newNarrow);

Expand Down Expand Up @@ -778,17 +827,15 @@ describe('getEditSequence correct for interesting changes', () => {
check(
{
messages: [message],
backgroundData: {
...eg.plusBackgroundData,
flags: { ...eg.plusBackgroundData.flags, starred: {} },
},
state: eg.reduxStatePlus({
flags: { ...eg.plusReduxState.flags, starred: {} },
}),
},
{
messages: [message],
backgroundData: {
...eg.plusBackgroundData,
flags: { ...eg.plusBackgroundData.flags, starred: { [message.id]: true } },
},
state: eg.reduxStatePlus({
flags: { ...eg.plusReduxState.flags, starred: { [message.id]: true } },
}),
},
);
});
Expand All @@ -798,17 +845,15 @@ describe('getEditSequence correct for interesting changes', () => {
check(
{
messages: [message],
backgroundData: {
...eg.plusBackgroundData,
flags: { ...eg.plusBackgroundData.flags, starred: { [message.id]: true } },
},
state: eg.reduxStatePlus({
flags: { ...eg.plusReduxState.flags, starred: { [message.id]: true } },
}),
},
{
messages: [message],
backgroundData: {
...eg.plusBackgroundData,
flags: { ...eg.plusBackgroundData.flags, starred: {} },
},
state: eg.reduxStatePlus({
flags: { ...eg.plusReduxState.flags, starred: {} },
}),
},
);
});
Expand All @@ -821,14 +866,13 @@ describe('getEditSequence correct for interesting changes', () => {
check(
{
messages: [message],
backgroundData: { ...eg.plusBackgroundData, mutedUsers: Immutable.Map() },
state: eg.reduxStatePlus({ mutedUsers: Immutable.Map() }),
},
{
messages: [message],
backgroundData: {
...eg.plusBackgroundData,
state: eg.reduxStatePlus({
mutedUsers: Immutable.Map([[message.sender_id, 1644366787]]),
},
}),
},
);
});
Expand All @@ -838,14 +882,13 @@ describe('getEditSequence correct for interesting changes', () => {
check(
{
messages: [message],
backgroundData: {
...eg.plusBackgroundData,
state: eg.reduxStatePlus({
mutedUsers: Immutable.Map([[message.sender_id, 1644366787]]),
},
}),
},
{
messages: [message],
backgroundData: { ...eg.plusBackgroundData, mutedUsers: Immutable.Map() },
state: eg.reduxStatePlus({ mutedUsers: Immutable.Map() }),
},
);
});
Expand Down

0 comments on commit 303d268

Please sign in to comment.