test: rewrite "revoked proxy" tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by Alec Larson · parent efd47f6 · Browse files · 1 file·+54 added−33 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+54 added−33 removed
- tests:+54 added−33 removed
Provenance
- Commit
b4964ad- Tree
305a31a- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
efd47f6
1 file changed+54 added−33 removed
modified:__tests__/base.js+54 added-33 removed
356 unmodified lines357 expect(nextState).not.toBe(baseState)358 expect(nextState.aProp).toBe(undefined)359 })360 361- // ES implementation does't protect against all outside modifications, just some..362- if (useProxies) {363- it("should revoke the proxy of the baseState after immer function is executed", () => {364- let proxy365- const nextState = produce(baseState, s => {366- proxy = s367- s.aProp = "hello"368- })369- expect(nextState).not.toBe(baseState)370- expect(baseState.aProp).toBe("hi")371- expect(nextState.aProp).toBe("hello")361+ // NOTE: ES5 drafts only protect existing properties when revoked.362+ it("revokes the draft once produce returns", () => {363+ const expectRevoked = (fn, shouldThrow = true) => {364+ if (shouldThrow) expect(fn).toThrowError(/revoked/)365+ else expect(fn).not.toThrow()366+ }367 373- expect(() => {374- proxy.aProp = "Hallo"375- }).toThrowError(/revoked/)376- expect(() => {377- const aProp = proxy.aProp378- }).toThrowError(/revoked/)368+ // Test object drafts:369+ let draft370+ produce({a: 1, b: 1}, s => {371+ draft = s372+ delete s.b373+ })374 380- expect(nextState).not.toBe(baseState)381- expect(baseState.aProp).toBe("hi")382- expect(nextState.aProp).toBe("hello")375+ // Access known property on object draft.376+ expectRevoked(() => {377+ draft.a378 })384- }379 386- it("should revoke the proxy of the baseState after immer function is executed - 2", () => {387- let proxy388- const nextState = produce(baseState, s => {389- proxy = s.anObject380+ // Assign known property on object draft.381+ expectRevoked(() => {382+ draft.a = true383 })391- expect(nextState).toBe(baseState)392- expect(() => {393- // In ES5 implemenation only protects existing props, but alas..394- proxy.coffee = "Hallo"395- }).toThrowError(/revoked/)396- expect(() => {397- const test = proxy.coffee398- }).toThrowError(/revoked/)384+385+ // Access unknown property on object draft.386+ expectRevoked(() => {387+ draft.z388+ }, useProxies)389+390+ // Assign unknown property on object draft.391+ expectRevoked(() => {392+ draft.z = true393+ }, useProxies)394+395+ // Test array drafts:396+ produce([1, 2], s => {397+ draft = s398+ s.pop()399+ })400+401+ // Access known index of an array draft.402+ expectRevoked(() => {403+ draft[0]404+ })405+406+ // Assign known index of an array draft.407+ expectRevoked(() => {408+ draft[0] = true409+ })410+411+ // Access unknown index of an array draft.412+ expectRevoked(() => {413+ draft[1]414+ }, useProxies)415+416+ // Assign unknown index of an array draft.417+ expectRevoked(() => {418+ draft[1] = true419+ }, useProxies)420 })421 422 it("can access a child draft that was created before the draft was modified", () => {423 produce({a: {}}, s => {906 unmodified lines