lundi 25 mars 2019

How to mock UUID value when testing ReactJS component using Jest

I have a component which calls an action in its constructor:

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

When snapshot testing the component, how can mock the uuidV1() value?

At the moment the test looks like this:

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!
})

The snapshot generates a new uuid each time thus resulting in a failing test.

Aucun commentaire:

Enregistrer un commentaire