Cannot test with enzyme and jest

未關閉
#264 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
需要釐清
活躍度
停滯
技術堆疊
javascript, react
領域
frontend, testing

研究方向

從 issue 中所示路徑下的 ErrorDialog 元件及其測試開始,接著檢查 Enzyme shallow 如何渲染匯入的 rc-dialog 元件。重現所回報的文字輸出,並判斷是測試還是元件行為不正確。完成的標準是測試能可靠地觀察到預期的訊息和程式碼缺失,或透過所需的專案變更釐清該 issue。

由索引模型根據 Issue 內容生成。

描述

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

主要語言
TypeScript
星號
454
分支
208
平均合併
1 小時 9 分鐘
30 天內合併 PR
1

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

react-component/dialog 的其他 Issue

查看 react-component/dialog 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。