Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Throw when accessing non-draftables in strict mode#715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Gelio wants to merge5 commits intoimmerjs:main
base:main
Choose a base branch
Loading
fromGelio:strict-mode-for-non-draftables
Open
Changes from1 commit
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
PrevPrevious commit
NextNext commit
Add tests for strict mode
  • Loading branch information
@Gelio
Gelio committedDec 3, 2020
commitc30fef3bfefcf1feddb01befad067583e7849b0a
106 changes: 106 additions & 0 deletions__tests__/strict-mode.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
"use strict"
import produce, {
setStrictMode,
unsafe,
immerable,
enableMapSet
} from "../src/immer"

enableMapSet()

describe("Strict Mode", () => {
class Foo {}

describe("by default", () => {
it("should not throw an error when accessing a non-draftable class instance", () => {
expect(() =>
produce({instance: new Foo()}, draft => {
draft.instance.value = 5
})
).not.toThrow()
})
})

afterAll(() => {
setStrictMode(false)
})

describe("when disabled", () => {
beforeEach(() => {
setStrictMode(false)
})

it("should allow accessing a non-draftable class instance", () => {
expect(() =>
produce({instance: new Foo()}, draft => {
draft.instance.value = 5
})
).not.toThrow()
})

it("should not throw errors when using the `unsafe` function", () => {
expect(() =>
produce({instance: new Foo()}, draft => {
unsafe(() => {
draft.instance.value = 5
})
})
).not.toThrow()
})
})

describe("when enabled", () => {
beforeEach(() => {
setStrictMode(true)
})

it("should throw an error when accessing a non-draftable class instance", () => {
expect(() =>
produce({instance: new Foo()}, draft => {
draft.instance
})
).toThrow()
})

it("should allow accessing a non-draftable using the `unsafe` function", () => {
expect(() =>
produce({instance: new Foo()}, draft => {
unsafe(() => {
draft.instance.value = 5
})
})
).not.toThrow()
})

describe("with an immerable class", () => {
beforeAll(() => {
Foo[immerable] = true
})

afterAll(() => {
Foo[immerable] = true
})

it("should allow accessing the class instance", () => {
expect(() =>
produce({instance: new Foo()}, draft => {
draft.instance.value = 5
})
).not.toThrow()
})
})

it("should allow accessing draftable properties", () => {
expect(() =>
produce({arr: [], obj: {}, map: new Map(), set: new Set()}, draft => {
draft.arr.push(1)
draft.arr[0] = 1
draft.obj.foo = 5
draft.obj.hasOwnProperty("abc")
draft.map.set("foo", 5)
draft.set.add("foo")
})
).not.toThrow()
})
})
})

[8]ページ先頭

©2009-2025 Movatter.jp