Add update scenario tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mark Erikson · committed by Mark Erikson · parent 460ddab · Browse files · 1 file·+557 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+557 added−0 removed
- tests:+557 added−0 removed
Provenance
- Commit
773455a- Tree
7dc665c- Author
Mark Erikson <mark@isquaredsoftware.com>- Committer
Mark Erikson <mark@isquaredsoftware.com>- Parent
460ddab
1 file changed+557 added−0 removed
added:__tests__/updateScenarios.js+557 added
1+import {describe, test, expect, beforeEach} from "vitest"2+import {produce} from "../src/immer"3+4+// Test configuration - smaller values for faster tests5+const TEST_CONFIG = {6+ arraySize: 10,7+ nestedArraySize: 2,8+ multiUpdateCount: 5,9+ reuseStateIterations: 510+}11+12+function createInitialState(arraySize = TEST_CONFIG.arraySize) {13+ const initialState = {14+ largeArray: Array.from({length: arraySize}, (_, i) => ({15+ id: i,16+ value: Math.random(),17+ nested: {key: `key-${i}`, data: Math.random()},18+ moreNested: {19+ items: Array.from({length: TEST_CONFIG.nestedArraySize}, (_, i) => ({20+ id: i,21+ name: String(i)22+ }))23+ }24+ })),25+ otherData: Array.from({length: arraySize}, (_, i) => ({26+ id: i,27+ name: `name-${i}`,28+ isActive: i % 2 === 029+ }))30+ }31+ return initialState32+}33+34+// Utility functions for calculating array indices based on size35+const getValidIndex = (arraySize = TEST_CONFIG.arraySize) => {36+ // Return a valid index (not the last one to avoid edge cases)37+ return Math.min(arraySize - 2, Math.max(0, arraySize - 2))38+}39+40+const getValidId = (arraySize = TEST_CONFIG.arraySize) => {41+ // Return a valid ID that exists in the array42+ return Math.min(arraySize - 2, Math.max(0, arraySize - 2))43+}44+45+const getHighIndex = (offset = 0, arraySize = TEST_CONFIG.arraySize) => {46+ // Calculate high index (80% through the array + offset)47+ return Math.floor(arraySize * 0.8) + (offset % Math.floor(arraySize * 0.2))48+}49+50+// Action creators51+const add = index => ({52+ type: "test/addItem",53+ payload: {id: index, value: index, nested: {data: index}}54+})55+56+const remove = index => ({type: "test/removeItem", payload: index})57+58+const filter = percentToKeep => ({59+ type: "test/filterItem",60+ payload: percentToKeep61+})62+63+const update = index => ({64+ type: "test/updateItem",65+ payload: {id: index, value: index, nestedData: index}66+})67+68+const concat = index => ({69+ type: "test/concatArray",70+ payload: Array.from({length: 50}, (_, i) => ({id: i, value: index}))71+})72+73+const updateHigh = index => ({74+ type: "test/updateHighIndex",75+ payload: {76+ id: getHighIndex(index),77+ value: index,78+ nestedData: index79+ }80+})81+82+const updateMultiple = index => ({83+ type: "test/updateMultiple",84+ payload: Array.from({length: TEST_CONFIG.multiUpdateCount}, (_, i) => ({85+ id: (index + i) % TEST_CONFIG.arraySize,86+ value: index + i,87+ nestedData: index + i88+ }))89+})90+91+const removeHigh = index => ({92+ type: "test/removeHighIndex",93+ payload: getHighIndex(index)94+})95+96+const sortByIdReverse = () => ({97+ type: "test/sortByIdReverse"98+})99+100+const reverseArray = () => ({101+ type: "test/reverseArray"102+})103+104+const actions = {105+ add,106+ remove,107+ filter,108+ update,109+ concat,110+ updateHigh,111+ updateMultiple,112+ removeHigh,113+ sortByIdReverse,114+ reverseArray115+}116+117+// Vanilla reducer for comparison118+const vanillaReducer = (state = createInitialState(), action) => {119+ switch (action.type) {120+ case "test/addItem":121+ return {122+ ...state,123+ largeArray: [...state.largeArray, action.payload]124+ }125+ case "test/removeItem": {126+ const newArray = state.largeArray.slice()127+ newArray.splice(action.payload, 1)128+ return {129+ ...state,130+ largeArray: newArray131+ }132+ }133+ case "test/filterItem": {134+ const length = state.largeArray.length135+ const newArray = state.largeArray.filter(136+ (item, i) => i / length < action.payload137+ )138+ return {139+ ...state,140+ largeArray: newArray141+ }142+ }143+ case "test/updateItem": {144+ return {145+ ...state,146+ largeArray: state.largeArray.map(item =>147+ item.id === action.payload.id148+ ? {149+ ...item,150+ value: action.payload.value,151+ nested: {...item.nested, data: action.payload.nestedData}152+ }153+ : item154+ )155+ }156+ }157+ case "test/concatArray": {158+ const length = state.largeArray.length159+ const newArray = action.payload.concat(state.largeArray)160+ newArray.length = length161+ return {162+ ...state,163+ largeArray: newArray164+ }165+ }166+ case "test/updateHighIndex": {167+ return {168+ ...state,169+ largeArray: state.largeArray.map(item =>170+ item.id === action.payload.id171+ ? {172+ ...item,173+ value: action.payload.value,174+ nested: {...item.nested, data: action.payload.nestedData}175+ }176+ : item177+ )178+ }179+ }180+ case "test/updateMultiple": {181+ const updates = new Map(action.payload.map(p => [p.id, p]))182+ return {183+ ...state,184+ largeArray: state.largeArray.map(item => {185+ const update = updates.get(item.id)186+ return update187+ ? {188+ ...item,189+ value: update.value,190+ nested: {...item.nested, data: update.nestedData}191+ }192+ : item193+ })194+ }195+ }196+ case "test/removeHighIndex": {197+ const newArray = state.largeArray.slice()198+ const indexToRemove = newArray.findIndex(199+ item => item.id === action.payload200+ )201+ if (indexToRemove !== -1) {202+ newArray.splice(indexToRemove, 1)203+ }204+ return {205+ ...state,206+ largeArray: newArray207+ }208+ }209+ case "test/sortByIdReverse": {210+ const newArray = state.largeArray.slice()211+ newArray.sort((a, b) => b.id - a.id) // Sort by ID in reverse order212+ return {213+ ...state,214+ largeArray: newArray215+ }216+ }217+ case "test/reverseArray": {218+ const newArray = state.largeArray.slice()219+ newArray.reverse()220+ return {221+ ...state,222+ largeArray: newArray223+ }224+ }225+ default:226+ return state227+ }228+}229+230+// Immer reducer231+const createImmerReducer = produce => {232+ const immerReducer = (state = createInitialState(), action) =>233+ produce(state, draft => {234+ switch (action.type) {235+ case "test/addItem":236+ draft.largeArray.push(action.payload)237+ break238+ case "test/removeItem":239+ draft.largeArray.splice(action.payload, 1)240+ break241+ case "test/filterItem": {242+ const keepPercentage = action.payload / 10243+ const length = state.largeArray.length244+ draft.largeArray = draft.largeArray.filter(245+ (item, i) => i / length < action.payload246+ )247+ break248+ }249+ case "test/updateItem": {250+ const item = draft.largeArray.find(251+ item => item.id === action.payload.id252+ )253+ if (item) {254+ item.value = action.payload.value255+ item.nested.data = action.payload.nestedData256+ }257+ break258+ }259+ case "test/concatArray": {260+ const length = state.largeArray.length261+ const newArray = action.payload.concat(state.largeArray)262+ newArray.length = length263+ draft.largeArray = newArray264+ break265+ }266+ case "test/updateHighIndex": {267+ const item = draft.largeArray.find(268+ item => item.id === action.payload.id269+ )270+ if (item) {271+ item.value = action.payload.value272+ item.nested.data = action.payload.nestedData273+ }274+ break275+ }276+ case "test/updateMultiple": {277+ action.payload.forEach(update => {278+ const item = draft.largeArray.find(item => item.id === update.id)279+ if (item) {280+ item.value = update.value281+ item.nested.data = update.nestedData282+ }283+ })284+ break285+ }286+ case "test/removeHighIndex": {287+ const indexToRemove = draft.largeArray.findIndex(288+ item => item.id === action.payload289+ )290+ if (indexToRemove !== -1) {291+ draft.largeArray.splice(indexToRemove, 1)292+ }293+ break294+ }295+ case "test/sortByIdReverse": {296+ draft.largeArray.sort((a, b) => b.id - a.id)297+ break298+ }299+ case "test/reverseArray": {300+ draft.largeArray.reverse()301+ break302+ }303+ }304+ })305+306+ return immerReducer307+}308+309+const immerReducer = createImmerReducer(produce)310+311+describe("Update Scenarios - Single Operations", () => {312+ let initialState313+314+ beforeEach(() => {315+ initialState = createInitialState()316+ })317+318+ test("add scenario", () => {319+ const action = actions.add(0)320+ const vanillaResult = vanillaReducer(initialState, action)321+ const immerResult = immerReducer(initialState, action)322+323+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)324+ expect(immerResult.largeArray[immerResult.largeArray.length - 1]).toEqual(325+ action.payload326+ )327+ })328+329+ test("remove scenario", () => {330+ const action = actions.remove(getValidIndex())331+ const vanillaResult = vanillaReducer(initialState, action)332+ const immerResult = immerReducer(initialState, action)333+334+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)335+ expect(immerResult.largeArray.length).toBe(336+ initialState.largeArray.length - 1337+ )338+ })339+340+ test("filter scenario", () => {341+ // Keep 60% of the items342+ const percentage = 0.6343+ const action = actions.filter(percentage)344+ const vanillaResult = vanillaReducer(initialState, action)345+ const immerResult = immerReducer(initialState, action)346+347+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)348+ expect(immerResult.largeArray.length).toBe(349+ initialState.largeArray.length * percentage350+ )351+ })352+353+ test("update scenario", () => {354+ const targetId = getValidId()355+ const action = actions.update(targetId)356+ const vanillaResult = vanillaReducer(initialState, action)357+ const immerResult = immerReducer(initialState, action)358+359+ const updatedItem = immerResult.largeArray.find(360+ item => item.id === targetId361+ )362+ expect(updatedItem.value).toBe(targetId)363+ expect(updatedItem.nested.data).toBe(targetId)364+ })365+366+ test("concat scenario", () => {367+ const action = actions.concat(1)368+ const vanillaResult = vanillaReducer(initialState, action)369+ const immerResult = immerReducer(initialState, action)370+371+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)372+ expect(immerResult.largeArray.length).toBe(initialState.largeArray.length)373+ })374+375+ test("updateHigh scenario", () => {376+ const action = actions.updateHigh(2)377+ const vanillaResult = vanillaReducer(initialState, action)378+ const immerResult = immerReducer(initialState, action)379+380+ const targetId = action.payload.id381+ const updatedItem = immerResult.largeArray.find(382+ item => item.id === targetId383+ )384+ expect(updatedItem.value).toBe(2)385+ expect(updatedItem.nested.data).toBe(2)386+ })387+388+ test("updateMultiple scenario", () => {389+ const action = actions.updateMultiple(3)390+ const vanillaResult = vanillaReducer(initialState, action)391+ const immerResult = immerReducer(initialState, action)392+393+ action.payload.forEach(update => {394+ const updatedItem = immerResult.largeArray.find(395+ item => item.id === update.id396+ )397+ expect(updatedItem.value).toBe(update.value)398+ expect(updatedItem.nested.data).toBe(update.nestedData)399+ })400+ })401+402+ test("removeHigh scenario", () => {403+ const action = actions.removeHigh(1)404+ const vanillaResult = vanillaReducer(initialState, action)405+ const immerResult = immerReducer(initialState, action)406+407+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)408+ const removedItem = immerResult.largeArray.find(409+ item => item.id === action.payload410+ )411+ expect(removedItem).toBeUndefined()412+ })413+414+ test("sortByIdReverse scenario", () => {415+ const action = actions.sortByIdReverse()416+ const vanillaResult = vanillaReducer(initialState, action)417+ const immerResult = immerReducer(initialState, action)418+419+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)420+ expect(immerResult.largeArray.length).toBe(initialState.largeArray.length)421+422+ // Verify sorting - first element should have highest ID423+ expect(immerResult.largeArray[0].id).toBe(TEST_CONFIG.arraySize - 1)424+ expect(immerResult.largeArray[immerResult.largeArray.length - 1].id).toBe(0)425+426+ // Verify arrays match427+ expect(immerResult.largeArray.map(item => item.id)).toEqual(428+ vanillaResult.largeArray.map(item => item.id)429+ )430+ })431+432+ test("reverseArray scenario", () => {433+ const action = actions.reverseArray()434+ const vanillaResult = vanillaReducer(initialState, action)435+ const immerResult = immerReducer(initialState, action)436+437+ expect(immerResult.largeArray.length).toBe(vanillaResult.largeArray.length)438+ expect(immerResult.largeArray.length).toBe(initialState.largeArray.length)439+440+ // Verify reversal - first element should be what was last441+ expect(immerResult.largeArray[0].id).toBe(TEST_CONFIG.arraySize - 1)442+ expect(immerResult.largeArray[immerResult.largeArray.length - 1].id).toBe(0)443+444+ // Verify arrays match445+ expect(immerResult.largeArray.map(item => item.id)).toEqual(446+ vanillaResult.largeArray.map(item => item.id)447+ )448+ })449+})450+451+describe("Update Scenarios - State Reuse", () => {452+ test("update-reuse scenario", () => {453+ let currentState = createInitialState()454+455+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {456+ currentState = immerReducer(currentState, actions.update(i))457+ }458+459+ // Verify the final state has all updates applied460+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {461+ const item = currentState.largeArray.find(item => item.id === i)462+ expect(item.value).toBe(i)463+ expect(item.nested.data).toBe(i)464+ }465+ })466+467+ test("updateHigh-reuse scenario", () => {468+ let currentState = createInitialState()469+470+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {471+ currentState = immerReducer(currentState, actions.updateHigh(i))472+ }473+474+ // Verify updates were applied to high indices475+ expect(currentState.largeArray.length).toBe(TEST_CONFIG.arraySize)476+ })477+478+ test("remove-reuse scenario", () => {479+ let currentState = createInitialState()480+ const originalLength = currentState.largeArray.length481+482+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {483+ currentState = immerReducer(currentState, actions.remove(0)) // Always remove first item484+ }485+486+ expect(currentState.largeArray.length).toBe(487+ originalLength - TEST_CONFIG.reuseStateIterations488+ )489+ })490+491+ test("removeHigh-reuse scenario", () => {492+ let currentState = createInitialState()493+ const originalLength = currentState.largeArray.length494+495+ for (let i = 0; i < TEST_CONFIG.reuseStateIterations; i++) {496+ currentState = immerReducer(currentState, actions.removeHigh(i))497+ }498+499+ expect(currentState.largeArray.length).toBeLessThan(originalLength)500+ })501+})502+503+describe("Update Scenarios - Mixed Sequence", () => {504+ test("mixed-sequence scenario", () => {505+ let state = createInitialState()506+ const originalLength = state.largeArray.length507+508+ // Perform a sequence of different operations (typical workflow)509+ state = immerReducer(state, actions.add(1))510+ expect(state.largeArray.length).toBe(originalLength + 1)511+512+ const targetId = getValidId()513+ state = immerReducer(state, actions.update(targetId))514+ const updatedItem = state.largeArray.find(item => item.id === targetId)515+ expect(updatedItem.value).toBe(targetId)516+517+ state = immerReducer(state, actions.updateHigh(2))518+ state = immerReducer(state, actions.updateMultiple(3))519+520+ state = immerReducer(state, actions.remove(getValidIndex()))521+ expect(state.largeArray.length).toBe(originalLength) // +1 from add, -1 from remove522+523+ // Verify final state integrity524+ expect(state.largeArray).toBeDefined()525+ expect(state.otherData).toBeDefined()526+ expect(state.largeArray.every(item => item.id !== undefined)).toBe(true)527+ })528+})529+530+describe("Update Scenarios - Performance Focused", () => {531+ test("large array operations", () => {532+ const largeState = createInitialState(5000)533+534+ // Test that operations complete without timeout535+ const result1 = immerReducer(largeState, actions.update(1000))536+ expect(result1.largeArray.length).toBe(5000)537+538+ const result2 = immerReducer(largeState, actions.updateHigh(10))539+ expect(result2.largeArray.length).toBe(5000)540+541+ const result3 = immerReducer(largeState, actions.updateMultiple(20))542+ expect(result3.largeArray.length).toBe(5000)543+ })544+545+ test("nested data modifications", () => {546+ const state = createInitialState(100)547+548+ const result = immerReducer(state, actions.update(50))549+ const updatedItem = result.largeArray.find(item => item.id === 50)550+551+ expect(updatedItem.nested.data).toBe(50)552+ expect(updatedItem.moreNested.items).toBeDefined()553+ expect(updatedItem.moreNested.items.length).toBe(554+ TEST_CONFIG.nestedArraySize555+ )556+ })557+})