1

我正在MuiPickersUtilsProvider使用@material-ui/pickers@ v-3.2.10

并使用如下:

import DateFnsUtils from '@date-io/date-fns'
import { DateTimePicker, MuiPickersUtilsProvider } from '@material-ui/pickers'

<MuiPickersUtilsProvider utils={DateFnsUtils}>
                <DateTimePicker .../>
</MuiPickersUtilsProvider>

它适用于 UI,但在运行测试用例时会引发如下错误:

TypeError:Utils 不是构造函数

但是一旦我使用import * as DateFnsUtils from '@date-io/date-fns'我们的测试用例,但同样的错误出现在 UI 上,

当我使用import {default as DateFnsUtils} from '@date-io/date-fns'我们的测试用例时,但同样的错误出现在两个地方(UI 和测试用例)

我在下面的链接上找到了类似的讨论,但没有帮助

材料-ui-pickers/issues/805

// setup file
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-16');
require('jest-canvas-mock')
enzyme.configure({ adapter: new Adapter() });

crypto = require('@trust/webcrypto')

// fetch undefined
const fetchPolifill = require('whatwg-fetch')
global.fetch = fetchPolifill.fetch
global.Request = fetchPolifill.Request
global.Headers = fetchPolifill.Headers
global.Response = fetchPolifill.Response

function noOp () { }
if (typeof window.URL.createObjectURL === 'undefined') {
  Object.defineProperty(window.URL, 'createObjectURL', { value: noOp})
}
4

2 回答 2

1

我遇到了同样的问题,我通过以下代码解决了,

"<MuiPickersUtilsProvider utils={DateFnsUtils}>"


import React, { useState } from 'react'
import DateFnsUtils from '@date-io/date-fns';
import { MuiPickersUtilsProvider, KeyboardDatePicker } from '@material-ui/pickers';

const ProblemStatement = () => {
 const [selectedDate, setSelectedDate] = useState(new Date('2014-08-18T21:11:54'))
    const handleDateChange = (date) => {
        setSelectedDate(date)
    }
    return (
        <div>
            ProblemStatement
            <h2>datepicker</h2>
            //this one
            <MuiPickersUtilsProvider utils={DateFnsUtils}>
                <KeyboardDatePicker
                    margin="normal"
                    id="date-picker-dialog"
                    label="Date picker dialog"
                    format="MM/dd/yyyy"
                    value={selectedDate}
                    onChange={handleDateChange}
                    KeyboardButtonProps={{
                        'aria-label': 'change date',
                    }}
                />
            </MuiPickersUtilsProvider>


        </div>
    )
}
于 2021-05-12T04:23:45.503 回答
0

LocalizationProvider为了使用muiDatePicker的组件而使用时出现此错误。就我而言,问题与prop 中的拼写错误有关。我通过而不是. 修复错字后问题解决:dateAdapterdataAdapter={AdapterDateFns}dateAdapter={AdapterDateFns}

import AdapterDateFns from '@mui/lab/AdapterDateFns';
import LocalizationProvider from '@mui/lab/LocalizationProvider';

export default function App() {

  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
        <Main />
    </LocalizationProvider>
  );
}
于 2021-10-18T16:56:34.237 回答