Skip to content

Commit

Permalink
Fix unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Feb 12, 2025
1 parent 78f9c08 commit bcb4f2c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 73 deletions.
72 changes: 4 additions & 68 deletions src/redux/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createSelector } from "reselect"
import type { ItemNameAndKeywords } from "../types/api"
import type { RootSelectorParamsProvider } from "../types/reduxHelperTypes"
import { fallbackToEmptyArray } from "../utils/fallbackToEmptyArray"
Expand All @@ -9,7 +8,6 @@ import {
createParametricSelectorHooks,
createSelectorWeakMap,
} from "./createSelectors"
import type { RootState } from "./store"
import { TOP_LEVEL_SELECTORS } from "./topLevelSelectors"

const ROOT_SELECTOR_PARAMS_PROVIDER: RootSelectorParamsProvider = {
Expand Down Expand Up @@ -91,8 +89,6 @@ export const selectCheckedVendorIds = createSelectorWeakMap(
fallbackToEmptyArray(checkedVendorItem?.checkedVendorIds),
)

// console.log(Object.keys(selectCheckedVendorIds))

export const isVendorChecked = createSelectorWeakMap(
[
ADAPTER_SELECTORS.GLOBAL.itemVendors.selectById,
Expand All @@ -119,40 +115,6 @@ export const selectCategoryItemIds = createSelectorWeakMap(
[ADAPTER_SELECTORS.GLOBAL.categories.selectById],
category => fallbackToEmptyArray(category?.itemIds),
)
export const selectCategoryItemIds1 = createSelector(
ADAPTER_SELECTORS.GLOBAL.categories.selectById,
category => fallbackToEmptyArray(category?.itemIds),
)

// export const selectCategoryItemIds1 = createSelector(
// [
// (state, id: number) => {
// console.log("selectCategoryItemIds1 input run")
// for (let i = 0; i < 1_000_000; i += 1) {
// /* empty */
// }
// return id
// },
// selectCategoriesData,
// ],
// (id, categories) => {
// console.log("selectCategoryItemIds1 output")
// return SIMPLE_SELECTORS.categories.selectById(categories, id)?.itemIds
// }
// )
// export const selectCategoryItemIds2 = createSelectorWeakMap(
// [
// selectCategoriesData,
// (state, id: number) => {
// console.log("selectCategoryItemIds2 input run")
// return id
// },
// ],
// (categories, id) => {
// console.log("selectCategoryItemIds2 output")
// return SIMPLE_SELECTORS.categories.selectById(categories, id)?.itemIds
// },
// )

export const checkIfAddedToVendor = createSelectorWeakMap(
[selectCartItemsIds, ROOT_SELECTOR_PARAMS_PROVIDER.getCartIdAndItemId],
Expand Down Expand Up @@ -190,11 +152,6 @@ export const selectVendorItemIds = createSelectorWeakMap(
[ADAPTER_SELECTORS.GLOBAL.vendors.selectById],
vendor => fallbackToEmptyArray(vendor?.itemIds),
)
// export const selectVendorItemIds1 = createSelectorWeakMap(
// [ADAPTER_SELECTORS.GLOBAL.vendors.selectById],
// vendor => fallbackToEmptyArray(vendor?.itemIds),
// { memoize: unstable_autotrackMemoize },
// )

export const selectCartsByItemId = createSelectorWeakMap(
[
Expand All @@ -215,14 +172,6 @@ export const checkIfAddedToAllVendors = createSelectorWeakMap(
true,
),
)
// export const checkIfAddedToAllVendors1 = createSelector(
// (state: RootState) => ({ ...state.added }),
// added => added
// )
export const selectIfAddedToAllVendors2 = createSelector(
[(state: RootState) => state.added],
added => added,
)

export const parametricSelectors = {
selectVendorsLinks,
Expand Down Expand Up @@ -297,27 +246,14 @@ export const mainSelectors = {
export const allSelectors = setSelectorNames({
...mainSelectors,
...apiSelectors,
// ...DRAFT_SAFE_SELECTORS,
...getAllEntitySelectors(),
...TOP_LEVEL_SELECTORS,
} as const)

export const resetAllSelectors = () => {
Object.values(allSelectors).forEach(e => {
// console.log(e.name)
// console.log(Object.keys(e))
if ("clearCache" in e) {
e.clearCache()
}
if ("resetRecomputations" in e) {
e.resetRecomputations()
}
if ("memoizedResultFunc" in e && "clearCache" in e.memoizedResultFunc) {
e.memoizedResultFunc.clearCache()
}
Object.values(allSelectors).forEach(selector => {
selector.clearCache()
selector.resetRecomputations()
selector.memoizedResultFunc.clearCache()
})
}

// export const useOfficialVendorName = createParametricSelectorHook(
// selectOfficialVendorName
// );
19 changes: 14 additions & 5 deletions src/tests/unit-tests/renderApp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
checkIfAnyItemsAdded,
isMinimized,
isVendorChecked,
mainSelectors,
resetAllSelectors,
selectCartItemNamesStringified,
selectCartItemsIds,
selectCartItemsLength,
selectCartsByItemId,
selectCartsItemIdsLength,
selectCategoryItemIds,
selectCategoryName,
Expand Down Expand Up @@ -74,7 +74,9 @@ describe<LocalTestContext>("render App", it => {
expect(selectQRCodeText.recomputations()).toBe(0)
expect(selectOfficialVendorName.recomputations()).toBe(8)
expect(selectVendorItemIds.recomputations()).toBe(8)
expect(selectCartsByItemId.recomputations()).toBe(0)
expect(checkIfAddedToAllVendors.recomputations()).toBe(0)
expect(selectCartsItemIdsLength.recomputations()).toBe(1)
const { store, user, container } = view
const state = store.getState()
expect(selectVendorsData(state).ids).toBeArrayOfSize(8)
Expand Down Expand Up @@ -105,7 +107,9 @@ describe<LocalTestContext>("render App", it => {
expect(selectQRCodeText.recomputations()).toBe(0)
expect(selectOfficialVendorName.recomputations()).toBe(8)
expect(selectVendorItemIds.recomputations()).toBe(8)
expect(selectCartsByItemId.recomputations()).toBe(10)
expect(checkIfAddedToAllVendors.recomputations()).toBe(10)
expect(selectCartsItemIdsLength.recomputations()).toBe(1)
const addButtons = screen.getAllByRole<HTMLButtonElement>("button", {
name: "Add",
})
Expand Down Expand Up @@ -134,7 +138,9 @@ describe<LocalTestContext>("render App", it => {
expect(selectQRCodeText.recomputations()).toBe(2)
expect(selectOfficialVendorName.recomputations()).toBe(8)
expect(selectVendorItemIds.recomputations()).toBe(8)
expect(selectCartsByItemId.recomputations()).toBe(21)
expect(checkIfAddedToAllVendors.recomputations()).toBe(21)
expect(selectCartsItemIdsLength.recomputations()).toBe(2)
await user.clear(inputField)
const accordionSummaries = container.querySelectorAll<HTMLDivElement>(
".MuiAccordionSummary-root",
Expand Down Expand Up @@ -169,7 +175,9 @@ describe<LocalTestContext>("render App", it => {
expect(selectQRCodeText.recomputations()).toBe(2)
expect(selectOfficialVendorName.recomputations()).toBe(8)
expect(selectVendorItemIds.recomputations()).toBe(8)
expect(selectCartsByItemId.recomputations()).toBe(26)
expect(checkIfAddedToAllVendors.recomputations()).toBe(26)
expect(selectCartsItemIdsLength.recomputations()).toBe(2)
const item = view.getByRole("button", {
name: /10 dram vials/iu,
})
Expand All @@ -194,11 +202,12 @@ describe<LocalTestContext>("render App", it => {
expect(selectQRCodeText.recomputations()).toBe(4)
expect(selectOfficialVendorName.recomputations()).toBe(8)
expect(selectVendorItemIds.recomputations()).toBe(8)
expect(selectCartsByItemId.recomputations()).toBe(31)
expect(checkIfAddedToAllVendors.recomputations()).toBe(31)
const rec = Object.entries(mainSelectors).map(([key, value]) => ({
[key]: value.recomputations(),
}))
expect(rec).toMatchSnapshot()
expect(selectCartsItemIdsLength.recomputations()).toBe(3)
// const rec = Object.entries(mainSelectors).map(([key, value]) => ({
// [key]: value.recomputations(),
// }))
findFastestSelector(selectItemNamesAndKeywords, state)
findFastestSelector(checkIfAnyItemsAdded, state)
findFastestSelector(selectCartsItemIdsLength, state)
Expand Down

0 comments on commit bcb4f2c

Please sign in to comment.