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
23 changes: 23 additions & 0 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,29 @@ function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {
) // should throw!
})

autoFreeze &&
test("issue #1190 / #1192 - should not freeze non-draftable objects (class instances and typed arrays)", () => {
class MutableClass {
value = 5
}

const state = {
someValue: 5,
mutableClass: new MutableClass(),
typedArray: new Uint8Array(10)
}

expect(() => {
// Should not throw when producing with autoFreeze enabled
const result = produce(state, draft => {
draft.someValue = 6
})

// Verify the non-draftable class instance is not frozen
state.mutableClass.value = 6
}).not.toThrow()
})

autoFreeze &&
test("issue #469, state not frozen", () => {
const project = produce(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function shallowCopy(base: any, strict: StrictMode) {
*/
export function freeze<T>(obj: T, deep?: boolean): T
export function freeze<T>(obj: any, deep: boolean = false): T {
if (isFrozen(obj) || isDraft(obj)) return obj
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj
if (getArchtype(obj) > 1 /* Map or Set */) {
O.defineProperties(obj, {
set: dontMutateMethodOverride,
Expand Down
Loading