Skip to content

Commit

Permalink
fix(reactivity): explicitly do type conversions in warning strings (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
月迷津渡 authored and yyx990803 committed Oct 6, 2019
1 parent 3d97524 commit 5eacfaf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
31 changes: 30 additions & 1 deletion packages/reactivity/__tests__/readonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
} from '../src'
import { mockWarn } from '@vue/runtime-test'

/**
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html
*/
type Writable<T> = { -readonly [P in keyof T]: T[P] }

describe('reactivity/readonly', () => {
mockWarn()

Expand All @@ -38,27 +43,51 @@ describe('reactivity/readonly', () => {
})

it('should not allow mutation', () => {
const observed: any = readonly({ foo: 1, bar: { baz: 2 } })
const qux = Symbol('qux')
const original = {
foo: 1,
bar: {
baz: 2
},
[qux]: 3
}
const observed: Writable<typeof original> = readonly(original)

observed.foo = 2
expect(observed.foo).toBe(1)
expect(
`Set operation on key "foo" failed: target is readonly.`
).toHaveBeenWarnedLast()

observed.bar.baz = 3
expect(observed.bar.baz).toBe(2)
expect(
`Set operation on key "baz" failed: target is readonly.`
).toHaveBeenWarnedLast()

observed[qux] = 4
expect(observed[qux]).toBe(3)
expect(
`Set operation on key "Symbol(qux)" failed: target is readonly.`
).toHaveBeenWarnedLast()

delete observed.foo
expect(observed.foo).toBe(1)
expect(
`Delete operation on key "foo" failed: target is readonly.`
).toHaveBeenWarnedLast()

delete observed.bar.baz
expect(observed.bar.baz).toBe(2)
expect(
`Delete operation on key "baz" failed: target is readonly.`
).toHaveBeenWarnedLast()

delete observed[qux]
expect(observed[qux]).toBe(3)
expect(
`Delete operation on key "Symbol(qux)" failed: target is readonly.`
).toHaveBeenWarnedLast()
})

it('should allow mutation when unlocked', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const readonlyHandlers: ProxyHandler<any> = {
if (LOCKED) {
if (__DEV__) {
console.warn(
`Set operation on key "${key as any}" failed: target is readonly.`,
`Set operation on key "${String(key)}" failed: target is readonly.`,
target
)
}
Expand All @@ -121,7 +121,9 @@ export const readonlyHandlers: ProxyHandler<any> = {
if (LOCKED) {
if (__DEV__) {
console.warn(
`Delete operation on key "${key as any}" failed: target is readonly.`,
`Delete operation on key "${String(
key
)}" failed: target is readonly.`,
target
)
}
Expand Down

0 comments on commit 5eacfaf

Please sign in to comment.