react-component / react-component/dialog

Cannot test with enzyme and jest

Open
#264 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
455
Forks
206
Avg merge
12m
Merged PRs (30d)
1

Description

Hi, I was trying to test the dialog component but it's not working.
Here is my component code

import Toast from '@/utils/Toast';
import PropTypes from 'prop-types';
import Dialog from 'rc-dialog';
import React from 'react';
import enhanceMessage from './enhanceMessage';

function ErrorDialog({ dismissError, error, breakoutFullText }) {
  const { message, code, name } = error || {};
  const enhancedMessage = enhanceMessage(message, code, breakoutFullText, name);
  const styles = {
    dialogContent: {
      display: ' flex',
      flexDirection: 'column',
      justifyContent: 'center',
      alignItems: 'center',
    },
  };
  if (code === 53105) {
    Toast.info(breakoutFullText);
    return true;
  }
  return error !== null ? (
    <Dialog
      title="Error"
      className="main-dialog"
      style={{ height: 200, width: 400, top: '20%' }}
      onClose={() => dismissError()}
      visible={error !== null}
      destroyOnClose
      animation="zoom"
      maskAnimation="fade"
      footer={
        <button type="button" className="btn button" key="close" onClick={dismissError}>
          OK
        </button>
      }>
      <div style={styles.dialogContent}>
        <h3>{name === 'NotFoundError' ? 'No camera found' : enhancedMessage}</h3>
        <div>
          {code && code !== 53105 && (
            <pre>
              <code>
                Error Code:
                {code}
              </code>
            </pre>
          )}
        </div>
      </div>
    </Dialog>
  ) : null;
}

Here is my test code

import ErrorDialog from '@/components/TwilioVideoMeeting/ErrorDialog/ErrorDialog';
import { shallow } from 'enzyme';
import Dialog from 'rc-dialog';
import React from 'react';

describe('Testing the ErrorDialog component', () => {
  const message = 'Fake Error message';
 
  it('should display error message but not error code is the later does not exist', () => {
    const error = { message };
    const wrapper = shallow(<ErrorDialog dismissError={() => {}} error={error} />);
    expect(wrapper.find(Dialog).text()).toContain(message);
    expect(wrapper.find('code').exists()).toBe(false);
  });
});

It throws following error
the ErrorDialog component › should display error message but not error code is the later does not exist

expect(received).toContain(expected) // indexOf

Expected substring: "Fake Error message"
Received string:    "< />"

Any Help would be appreciated

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ErrorDialog component and its test at the paths shown in the issue, then inspect how the imported rc-dialog component is rendered by Enzyme shallow. Reproduce the reported text output and determine whether the test or component behavior is incorrect. Done means the test reliably observes the expected message and code absence, or the issue is clarified with the required project change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.