From d26f4349424ac4486082944a846aee35c5f27b94 Mon Sep 17 00:00:00 2001 From: daishi Date: Thu, 11 Jul 2024 23:43:05 +0900 Subject: [PATCH] add failing test --- tests/issue_100.spec.ts | 108 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tests/issue_100.spec.ts b/tests/issue_100.spec.ts index bb852f0..b313d25 100644 --- a/tests/issue_100.spec.ts +++ b/tests/issue_100.spec.ts @@ -41,4 +41,112 @@ describe('Static props prevents selectors recall (#100)', () => { expect(selectAdjustedPriceString(state1)).toBe('10'); expect(selectAdjustedPriceString(state2)).toBe('20'); }); + + it('case 2', () => { + type State = { + booksState: { + booksObject: { + book1: { shortName: string }; + }; + }; + extendedBooksState: { + extendedBooksObject: { + book1: { + staticProp: string; + priceString: string; + book: { shortName: string }; + }; + }; + }; + }; + + const state1: State = { + booksState: { + booksObject: { + book1: { + shortName: 'book1-name', + }, + }, + }, + extendedBooksState: { + extendedBooksObject: { + book1: { + staticProp: '5', + priceString: '10', + book: { + shortName: 'book1-name', + }, + }, + }, + }, + }; + + const state2: State = { + booksState: { + booksObject: { + book1: { + shortName: 'book1-name', + }, + }, + }, + extendedBooksState: { + extendedBooksObject: { + book1: { + staticProp: '5', + priceString: '0', + book: { + shortName: 'book1-name', + }, + }, + }, + }, + }; + + const selectPriceString = memoize( + (state: State) => + state.extendedBooksState.extendedBooksObject.book1.priceString, + ); + + const selectAdjustedPriceString = memoize((state: State) => { + const { + booksState: { booksObject }, + extendedBooksState: { extendedBooksObject }, + } = state; + + const book = booksObject.book1; + + book.shortName; // touch the prop + + const priceString = selectPriceString(state); + + extendedBooksObject.book1.staticProp; // touch the prop + + return priceString; + }); + + const selectAllExtendedBooks = memoize((state: State) => + Object.values(state.extendedBooksState.extendedBooksObject), + ); + + const selectBookThroughExtendedBooks = memoize( + (state: State) => selectAllExtendedBooks(state)[0]!.book, + ); + + const selectMemoizedPriceString = memoize( + (state: State) => selectPriceString(state), + ); + + const getPriceStringSelector = + ( + _book: unknown, // looks as touch + ) => + (state: State) => + selectMemoizedPriceString(state); + + getPriceStringSelector(selectBookThroughExtendedBooks(state1))(state1); + expect(selectAdjustedPriceString(state1)).toBe('10'); + + getPriceStringSelector(selectBookThroughExtendedBooks(state2))(state2); + expect(selectAdjustedPriceString(state2)).toBe('0'); + }); });