简体   繁体   English

开玩笑的 mocking 和 function 不起作用

[英]mocking a function with jest isn't working

I've written a basic test to mock a date change function with jest.我写了一个基本测试来模拟日期更改 function 开玩笑。

like this,像这样,

import React from 'react';
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import Calendar from './calendar';
/*
Mocks Calendar by providing fake date inputs.


*/
const testData = [
  {
    value: 164,
    day: '2016-02-07',
  },
  {
    value: 152,
    day: '2016-10-15',
  },
  {
    value: 132,
    day: '2018-04-17',
  },
];
//Should mock Dates for tests
const setSelectedDate = jest.fn();

describe('Dashbboard Calendar Graph', () => {
  it('should render the Sentiment graph with no loader element.', () => {
    render(<Calendar data={testData} setSelectedDate={setSelectedDate} startDate={d.getTime()} endDate ={Date.now()} />);
    const heading = screen.getByTestId('calendar');
    expect(heading).toBeInTheDocument();
  });

  it('should show loader for the Sentiment graph if no data is present', () => {
    render(<Calendar data={null} setSelectedDate={setSelectedDate} startDate={d.getTime()} endDate ={Date.now()} />);
    const loader = screen.getByTestId('calendar');
    expect(loader).toBeInTheDocument();
  });
});

The component is like this,组件是这样的,

import React from 'react';
import { ResponsiveCalendar } from '@nivo/calendar';
import Loader from './loader';



export default function Calendar({ data, setSelectedDate,startDate,endDate }) {
  return (
    <div               data-testid="calendar"
    style={{ height: 400 }}>
                  {!calendarData && <Loader />}

      <ResponsiveCalendar
        data={!data ? [] : data}
        from={startDate}
        to={endDate}
        emptyColor="#eeeeee"
        colors={['#61cdbb', '#97e3d5', '#e8c1a0', '#f47560']}
        margin={{ top: 20, right: 40, bottom: 20, left: 40 }}
        yearSpacing={40}
        monthBorderColor="#ffffff"
        dayBorderWidth={2}
        dayBorderColor="#ffffff"
        isInteractive={true}
        onClick={({ day }) => setSelectedDate(day)}
        legends={[
          {
            anchor: 'bottom-right',
            direction: 'row',
            translateY: 36,
            itemCount: 4,
            itemWidth: 42,
            itemHeight: 36,
            itemsSpacing: 14,
            itemDirection: 'right-to-left',
          },
        ]}
      />
    </div>
  );
}

Usually the function being passed in is this,通常传入的function是这个,

const [selectedDate, setSelectedDate] = useState(
    format(new Date(), "yyyy-MM-dd")
  );

It's returning an error like this,它返回这样的错误,

Dashbboard Calendar Graph > should show loader for the Sentiment graph if no data is present
-----
ReferenceError: d is not defined

Unsure what I'm doing wrong though - I just want to be able to know if the calendar is loading and changing based upon the date range, and date choice really.不确定我做错了什么 - 我只想知道日历是否根据日期范围加载和更改,以及日期选择。 Confusing!令人困惑!

As the error states d is not defined.由于错误状态d is not defined.

On your tests you should use getTime() from a Date object not from a d undefined variable.在您的测试中,您应该使用来自 Date object 而不是来自d未定义变量的getTime()

One way would be to initialize d variable with a new Date() like this一种方法是用这样的new Date()初始化d变量

//Should mock Dates for tests
const setSelectedDate = jest.fn();

const d = new Date(); // d is now defined with a Date object.

describe('Dashbboard Calendar Graph', () => {
  it('should render the Sentiment graph with no loader element.', () => {
    render(<Calendar data={testData} setSelectedDate={setSelectedDate} startDate={d.getTime()} endDate ={Date.now()} />);
    const heading = screen.getByTestId('calendar');
    expect(heading).toBeInTheDocument();
  });

  it('should show loader for the Sentiment graph if no data is present', () => {
    render(<Calendar data={null} setSelectedDate={setSelectedDate} startDate={d.getTime()} endDate ={Date.now()} />);
    const loader = screen.getByTestId('calendar');
    expect(loader).toBeInTheDocument();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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