简体   繁体   中英

Error while testing React component using Jasmine

I kept getting this error while testing a react component:

Error: Invariant Violation: findComponentRoot(..., .0): Unable to find element. This probably means the DOM was unexpectedly mutated (eg, by the browser), usually due to forgetting a <tbody> when using tables, nesting tags like <form>, <p>, or <a>, or using non-SVG elements in an <svg> parent. Try inspecting the child nodes of the element with React ID ``.

Code I used to test:

describe(`Method: flattenData`, () => {

  const tests = [
    {
      toFlatten: {
        'key1': 'k',
        'key2': 'n',
        'key3': 'o'
      },
      expectedResult: 'key1: k; key2: n; key3: o'
    }
  ];

  tests.forEach((test) => {

    expect(instance.flattenData(test.toFlatten)).toEqual(test.expectedResult);

   });

});

I fixed this bug by adding a parameter to the render call. This forces the component (which is just a <td> </td> to be rendered inside a table.

Before:

[container, instance] = render(TableCell, {
  data: `content`,
  dataTh: `content`,
  key: `content`,
  dataLocator: `content`
});

With the fix:

[container, instance] = render(TableCell, {
  data: `content`,
  dataTh: `content`,
  key: `content`,
  dataLocator: `content`
}, `table`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM