From 26e51ce2b7fdd36a7a3a695d4e5d181b1befe015 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Mon, 3 Aug 2020 12:20:29 -0400 Subject: [PATCH 1/3] feat(reactivity): Support returning an array from toRefs --- packages/reactivity/__tests__/ref.spec.ts | 15 +++++++++++++++ packages/reactivity/src/ref.ts | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index 6bfcceaef54..f808d1ba82b 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -272,6 +272,21 @@ describe('reactivity/ref', () => { toRefs(obj) expect(console.warn).toBeCalled() }) + test('toRefs array', () => { + console.warn = jest.fn() + const arr = ['a', 'b', 'c'] + toRefs(arr) + expect(console.warn).toBeCalled() + }) + test('toRefs reactive array', () => { + const arr = reactive(['a', 'b', 'c']) + const refs = toRefs(arr) + refs[0].value = '1' + expect(arr[0]).toBe('1') + + arr[1] = '2' + expect(refs[1].value).toBe('2') + }) test('customRef', () => { let value = 1 let _trigger: () => void diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 174494e87e9..396e34e47d5 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -1,6 +1,6 @@ import { track, trigger } from './effect' import { TrackOpTypes, TriggerOpTypes } from './operations' -import { isObject, hasChanged } from '@vue/shared' +import { isArray, isObject, hasChanged } from '@vue/shared' import { reactive, isProxy, toRaw, isReactive } from './reactive' import { CollectionTypes } from './collectionHandlers' @@ -121,7 +121,7 @@ export function toRefs(object: T): ToRefs { if (__DEV__ && !isProxy(object)) { console.warn(`toRefs() expects a reactive object but received a plain one.`) } - const ret: any = {} + const ret: any = isArray(object) ? new Array(object.length) : {} for (const key in object) { ret[key] = toRef(object, key) } From 28a26ea8da752575025c10ff725c81af5a66ffbb Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 18 Aug 2020 11:53:01 -0400 Subject: [PATCH 2/3] Update ref.spec.ts --- packages/reactivity/__tests__/ref.spec.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index f808d1ba82b..b250dc396e6 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -266,27 +266,27 @@ describe('reactivity/ref', () => { expect(dummyY).toBe(5) }) - test('toRefs pass a reactivity object', () => { - console.warn = jest.fn() - const obj = { x: 1 } - toRefs(obj) - expect(console.warn).toBeCalled() + test('toRefs should warn on plain object', () => { + toRefs({}) + expect(`toRefs() expects a reactive object`).toHaveBeenWarned() }) - test('toRefs array', () => { - console.warn = jest.fn() - const arr = ['a', 'b', 'c'] - toRefs(arr) - expect(console.warn).toBeCalled() + + test('toRefs should warn on plain array', () => { + toRefs([]) + expect(`toRefs() expects a reactive object`).toHaveBeenWarned() }) + test('toRefs reactive array', () => { const arr = reactive(['a', 'b', 'c']) const refs = toRefs(arr) + refs[0].value = '1' expect(arr[0]).toBe('1') - + arr[1] = '2' expect(refs[1].value).toBe('2') }) + test('customRef', () => { let value = 1 let _trigger: () => void From 1824507a936196603e0a831d09935d0eb8e2ff06 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 18 Aug 2020 11:53:57 -0400 Subject: [PATCH 3/3] Update ref.spec.ts --- packages/reactivity/__tests__/ref.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index b250dc396e6..bae4ec35a31 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -280,6 +280,8 @@ describe('reactivity/ref', () => { const arr = reactive(['a', 'b', 'c']) const refs = toRefs(arr) + expect(Array.isArray(refs)).toBe(true) + refs[0].value = '1' expect(arr[0]).toBe('1')