Skip to content

Commit

Permalink
unread tests: Add a test for not touching unaffected parts of state.
Browse files Browse the repository at this point in the history
This is one property that only even becomes possible with the new
data structure!

This is useful in part because it might help downstream consumers
of the data make an optimization, by not recomputing their own work
if the particular piece of this data that they're interested in
hasn't changed.

The more immediate motivation for the test, though, is that it
effectively checks that we have a certain optimization working
within this model's own implementation.  A naive implementation
might end up copying the maps for all the streams when only one or
a few of them actually needed to change; in fact an early draft of
this implementation did just that.  If instead we successfully
returned the old maps, then that probably means we successfully
avoided doing the work of allocating new ones.
  • Loading branch information
gnprice committed Mar 18, 2021
1 parent 89cff7c commit 4849292
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/unread/__tests__/unreadModel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ describe('stream substate', () => {
};
};

const streamAction = args => mkMessageAction(eg.streamMessage(args));

const baseState = (() => {
const streamAction = args => mkMessageAction(eg.streamMessage(args));
const r = (state, action) => reducer(state, action, eg.plusReduxState);
let state = initialState;
state = r(state, streamAction({ stream_id: 123, subject: 'foo', id: 1 }));
Expand Down Expand Up @@ -184,6 +185,29 @@ describe('stream substate', () => {
]));
});

test("when removing, don't touch unaffected topics or streams", () => {
const state = reducer(
baseState,
streamAction({ stream_id: 123, subject: 'qux', id: 7 }),
eg.plusReduxState,
);
// prettier-ignore
expect(summary(state)).toEqual(Immutable.Map([
[123, Immutable.Map([['foo', [1, 2, 3]], ['qux', [7]]])],
[234, Immutable.Map([['bar', [4, 5]]])],
]));

const action = mkAction({ messages: [1, 2] });
const newState = reducer(state, action, eg.plusReduxState);
// prettier-ignore
expect(summary(newState)).toEqual(Immutable.Map([
[123, Immutable.Map([['foo', [3]], ['qux', [7]]])],
[234, Immutable.Map([['bar', [4, 5]]])],
]));
expect(newState.streams.get(123)?.get('qux')).toBe(state.streams.get(123)?.get('qux'));
expect(newState.streams.get(234)).toBe(state.streams.get(234));
});

test('when operation is "remove" do nothing', () => {
const action = mkAction({ messages: [1, 2], operation: 'remove' });
expect(reducer(baseState, action, eg.plusReduxState)).toBe(baseState);
Expand Down

0 comments on commit 4849292

Please sign in to comment.