taro icon indicating copy to clipboard operation
taro copied to clipboard

nerv-test-utils的scryRenderedComponentsWithType出错

Open masteroy opened this issue 6 years ago • 6 comments

在做单元测试时,发现nerv-test-utils提供的scryRenderedComponentsWithType没正常工作,找不到对应的组件。 下面的例子,期望能够找到2个Button组件,但scryRenderedComponentsWithType返回0。

// Counter.tsx
import Taro, { useState, useEffect } from "@tarojs/taro";
import { View, Button, Text } from "@tarojs/components";

interface Props {
  initCount: number
}

export function Counter({ initCount }: Props) {
  const [counter, setCounter] = useState(initCount);

  useEffect(() => {
    console.log('use Effect');
  }, []);

  return (
    <View>
      <Button onClick={() => setCounter(counter + 1)} >+1</Button>
      <Text>{counter}</Text>
      <Button onClick={() => setCounter(counter - 1)}>-1</Button>
    </View>
  )
}
// Counter.test.tsx
import { Counter } from './Counter';
import { renderIntoDocument, scryRenderedComponentsWithType } from 'nerv-test-utils';
import { Button } from '@tarojs/components';

describe('<Counter />', () => {
    it('should render properly', () => {
        const component = renderIntoDocument(<Counter initCount={0} />);
        expect(component).toMatchSnapshot();

        const buttons = scryRenderedComponentsWithType(component, Button);
        expect(buttons.length).toBe(2); // 期望输出2,但输出0
    });
});

复现repo:https://github.com/masteroy/taro-unit-test

masteroy avatar Jan 13 '20 09:01 masteroy

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

taro-bot avatar Jan 13 '20 09:01 taro-bot

我读nerv-test-utils发现一个问题

const EMPTY_OBJ = {};
function isComponent(instance) {
    return !isInvalid(instance) && instance.isReactComponent === EMPTY_OBJ;
}

这段代码里,instance.isReactComponent是由nervjs创建的,而这个EMPTY_OBJ是在nerv-test-utils创建的,这个instance.isReactComponent === EMPTY_OBJ只会返回false

masteroy avatar Jan 13 '20 10:01 masteroy

CC @yuche

taro-bot avatar Jan 13 '20 10:01 taro-bot

@masteroy

抱歉过年延迟了回复。

EMPTY_OBJ 应该都是通过 nerv-shared 引入的,他们应该是同一个东西。

你的仓库不包含测试用例,根据 https://github.com/NervJS/nerv/commit/69e5c1ea39b5d623cf2e101852890d5aa665bc52 来看,这个用例应该是没问题的。

yuche avatar Feb 03 '20 10:02 yuche

@yuche https://github.com/masteroy/taro-unit-test/blob/master/src/components/Counter.test.tsx 这就是测试用例呀

masteroy avatar Feb 17 '20 07:02 masteroy

请问 nerv-test-utils 有相关文档吗?scryRenderedComponentsWithType 这个函数看起来就怪。

@masteroy 你的例子里面,添加如下

describe('<Counter />', () => {
    it('should render properly', () => {
        const component = renderIntoDocument(<Counter initCount={0} />);
        expect(component).toMatchSnapshot();

        console.log(JSON.stringify(component._rendered.component.constructor.name);
        console.log(component._rendered.type);

        const buttons = scryRenderedComponentsWithType(component, Button);
        expect(buttons.length).toBe(2);
    });
});

运行 yarn run test:ci

  console.log .temp/components/Counter.test.js:10
    "View"

  console.log .temp/components/Counter.test.js:11
    [Function: View] { displayName: 'View' }

所以如果 scryRenderedComponentsWithType 是为了根据名字找控件,应该使用 component.constructor.name 而不是 type (https://github.com/NervJS/nerv-test-utils/blob/master/src/index.ts#L44)

那么就是一个bug。@yuche

silencej avatar Nov 11 '20 09:11 silencej