React-dnd: ドラッグソースとドロップターゲットの両方であるコンポーネントをテストできません

作成日 2016年09月09日  ·  2コメント  ·  ソース: react-dnd/react-dnd

次の例のように、アプリにドラッグソースとドロップターゲットの両方のコンポーネントがあります: https

テストバックエンドを使用してドラッグ操作を開始すると、次のエラーが発生します。

Invariant Violation: Expected a valid source ID.

ソースを見ると、 parseRoleFromHandlerIdからの

例から、このユースケースはサポートされているものであることがわかりましたが、テストできると便利です。

最も参考になるコメント

OK、これを行う方法を見つけました。 将来の開発者がエラーメッセージをグーグルで検索するのに役立つことを期待して、ここに文書化します。

解決策は、次のように中間アイテムをエクスポートすることです。

class MyComponent extends React.Component {
  // ...
}

export const MyComponentDragSource = DragSource(/* ... */)(MyComponent);
// ^^^ export the intermediate component

export default const MyComponentDropTarget = DropTarget(/* ... */)(MyComponentDragSource);

次に、次のようにテストで使用できます。

import MyComponent, {MyComponentDragSource} from '../MyComponent';

// ...
      // Inside test
      const root = TestUtils.renderIntoDocument(<MyComponent />);
      const backend = root.getManager().getBackend();

      const item = TestUtils.findRenderedComponentWithType(root, MyComponentDragSource);
      // Notice we find the DragSource, not the regular component:          ^^^^^^^^^^
      backend.simulateBeginDrag([item.getHandlerId()]); // Will work now
// ...

全てのコメント2件

OK、これを行う方法を見つけました。 将来の開発者がエラーメッセージをグーグルで検索するのに役立つことを期待して、ここに文書化します。

解決策は、次のように中間アイテムをエクスポートすることです。

class MyComponent extends React.Component {
  // ...
}

export const MyComponentDragSource = DragSource(/* ... */)(MyComponent);
// ^^^ export the intermediate component

export default const MyComponentDropTarget = DropTarget(/* ... */)(MyComponentDragSource);

次に、次のようにテストで使用できます。

import MyComponent, {MyComponentDragSource} from '../MyComponent';

// ...
      // Inside test
      const root = TestUtils.renderIntoDocument(<MyComponent />);
      const backend = root.getManager().getBackend();

      const item = TestUtils.findRenderedComponentWithType(root, MyComponentDragSource);
      // Notice we find the DragSource, not the regular component:          ^^^^^^^^^^
      backend.simulateBeginDrag([item.getHandlerId()]); // Will work now
// ...

私の神様、ありがとう

このページは役に立ちましたか?
0 / 5 - 0 評価