Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions superglue/lib/hooks/useSetFragment.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useDispatch, useSelector } from 'react-redux'
import { produce } from 'immer'
import { Immer } from 'immer'
import { saveFragment } from '../actions'
import { RootState, Fragment } from '../types'
import { Unproxy } from '../types'
import { FragmentProxy } from './useContent'

const immer = new Immer()
immer.setAutoFreeze(false)

/**
* Utility type to extract the data type from a Fragment wrapper
* @public
Expand Down Expand Up @@ -75,7 +78,7 @@ export function useSetFragment() {
throw new Error(`Fragment with id "${fragmentId}" not found`)
}

const updatedFragment = produce(currentFragment, updater)
const updatedFragment = immer.produce(currentFragment, updater)

dispatch(
saveFragment({
Expand Down
9 changes: 4 additions & 5 deletions superglue/spec/lib/useSetFragment.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ describe('useSetFragment', () => {
})
})

// Verify the fragment was updated in the store
const updatedState = store.getState()
expect(updatedState.fragments['user_123']).toEqual({
id: 123,
name: 'Jane',
email: '[email protected]',
})

expect(Object.isFrozen(updatedState.fragments['user_123'])).toBe(false)
})

it('should update existing fragment with string reference', () => {
Expand All @@ -83,19 +84,19 @@ describe('useSetFragment', () => {
const set = result.current

act(() => {
// Using string directly instead of object
set('user_123', (draft) => {
draft.name = 'Jane'
})
})

// Verify the fragment was updated in the store
const updatedState = store.getState()
expect(updatedState.fragments['user_123']).toEqual({
id: 123,
name: 'Jane',
email: '[email protected]',
})

expect(Object.isFrozen(updatedState.fragments['user_123'])).toBe(false)
})

it('should handle nested object updates', () => {
Expand Down Expand Up @@ -184,7 +185,6 @@ describe('useSetFragment', () => {
const set = result.current

act(() => {
// Using string directly
set('posts_collection', (draft) => {
draft.push({ title: 'Post 3' })
})
Expand Down Expand Up @@ -229,7 +229,6 @@ describe('useSetFragment', () => {
set({ __id: 'user_123' }, (userDraft) => {
userDraft.name = 'Jane'

// Nested set call
set(userDraft.profile, (profileDraft) => {
profileDraft.avatar = '/new.jpg'
})
Expand Down