Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return res when is not object #1952

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
59 changes: 42 additions & 17 deletions packages/hooks/src/useReactive/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { fireEvent, render } from '@testing-library/react';
import React from 'react';
import { act } from 'react-dom/test-utils';
import useReactive from '../';
import { renderHook } from '@testing-library/react-hooks';

const Demo = () => {
let state: {
const state: {
count: number;
val: any;
foo?: string;
Expand Down Expand Up @@ -83,11 +84,11 @@ const Demo = () => {

describe('test useReactive feature', () => {
it('test count ', () => {
let wrap = render(<Demo />);
const wrap = render(<Demo />);

let count = wrap.getByRole('addCount');
let addCountBtn = wrap.getByRole('addCountBtn');
let subCountBtn = wrap.getByRole('subCountBtn');
const count = wrap.getByRole('addCount');
const addCountBtn = wrap.getByRole('addCountBtn');
const subCountBtn = wrap.getByRole('subCountBtn');

act(() => {
fireEvent.click(addCountBtn);
Expand Down Expand Up @@ -116,12 +117,12 @@ describe('test useReactive feature', () => {
});

it('test array', () => {
let wrap = render(<Demo />);
let testArray = wrap.getByRole('test-array');
let pushbtn = wrap.getByRole('pushbtn');
let popbtn = wrap.getByRole('popbtn');
let shiftbtn = wrap.getByRole('shiftbtn');
let unshiftbtn = wrap.getByRole('unshiftbtn');
const wrap = render(<Demo />);
const testArray = wrap.getByRole('test-array');
const pushbtn = wrap.getByRole('pushbtn');
const popbtn = wrap.getByRole('popbtn');
const shiftbtn = wrap.getByRole('shiftbtn');
const unshiftbtn = wrap.getByRole('unshiftbtn');
act(() => {
fireEvent.click(pushbtn);
});
Expand All @@ -141,10 +142,10 @@ describe('test useReactive feature', () => {
});

it('test input1', () => {
let wrap = render(<Demo />);
const wrap = render(<Demo />);

let input = wrap.getByRole('input1');
let inputVal = wrap.getByRole('inputVal1');
const input = wrap.getByRole('input1');
const inputVal = wrap.getByRole('inputVal1');
act(() => {
fireEvent.change(input, { target: { value: 'a' } });
});
Expand All @@ -157,15 +158,39 @@ describe('test useReactive feature', () => {
});

it('delete object property', () => {
let wrap = render(<Demo />);
const wrap = render(<Demo />);

let deleteProperty = wrap.getByRole('deleteProperty');
let deletePropertyBtn = wrap.getByRole('deletePropertyBtn');
const deleteProperty = wrap.getByRole('deleteProperty');
const deletePropertyBtn = wrap.getByRole('deletePropertyBtn');
expect(deleteProperty.textContent).toBe('foo');

act(() => {
fireEvent.click(deletePropertyBtn);
});
expect(deleteProperty.textContent).toBe('');
});

it('access from self to prototype chain', () => {
const parent = {
name: 'parent',
get value() {
return this.name;
},
};

const child: Record<string, string> = {
name: 'child',
};

const { result } = renderHook(() => useReactive(parent));
const proxy = result.current;

Object.setPrototypeOf(child, proxy);

expect(child.value).toBe('child');

delete child.name;

expect(child.value).toBe('parent');
});
});
2 changes: 1 addition & 1 deletion packages/hooks/src/useReactive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function observer<T extends Record<string, any>>(initialVal: T, cb: () => void):
const proxy = new Proxy<T>(initialVal, {
get(target, key, receiver) {
const res = Reflect.get(target, key, receiver);
return isObject(res) ? observer(res, cb) : Reflect.get(target, key);
return isObject(res) ? observer(res, cb) : res;
},
set(target, key, val) {
const ret = Reflect.set(target, key, val);
Expand Down