使用Jest测试ReactJS组件时如何模拟UUID值

时间:2019-03-25 16:29:10

标签: javascript reactjs testing mocking

我有一个在其构造函数中调用动作的组件:

this.props.filterGroupCreated({filterGroupId: uuidV1(), dataSource})

在对组件进行快照测试时,如何模拟uuidV1()值?

目前测试如下:

import React from "react";
import { shallow } from "enzyme"
import {MultistepFilterSection} from "../../../src/components/MultistepFilter/MultistepFilterSection"

const mockFilterGroupsCreated = jest.fn()

describe("<MultistepFilterSection />", () => {
    let wrapper
    const getAudience = jest.fn()
    const dataSource = "yougov"

    beforeEach(() => {
        wrapper = shallow(
            <MultistepFilterSection
                getAudience={getAudience}
                dataSource={dataSource}
                filterGroupCreated={ mockFilterGroupsCreated}
                filterGroups={{"yougov":{}}}
            />
        )
    })

    it("renders correctly", () => {
        expect(wrapper).toMatchSnapshot()
    })

    // todo add more test cases!
})

快照每次都会生成一个新的uuid,从而导致测试失败。

0 个答案:

没有答案