S
S
sergeyviktorovich2021-11-10 17:17:24
React
sergeyviktorovich, 2021-11-10 17:17:24

How to test view components in react?

if I test view components that accept container component methods using snapsheets, where there are no methods and props, then what should I do with the methods passed to this component, just pass functions that do nothing?

let del = jest.fn();
    let openFormNewItemEdit = jest.fn();
    let setPin = jest.fn();
    let addDel = jest.fn();
    let deleteTaskItem = jest.fn();
    let getTime = jest.fn();
    let setColor = jest.fn();
    let props = {
        "data": {
            "name": "Мои заметки № 1",
            "text": "Мои заметки № 1",
            "time": 1636283818894,
            "key": "-Mnu6P7dMJTeVv8VESLh",
            "keychapter": "0"
        },
        "keychapter": "0",
        "keyitem": "-Mnu6P7dMJTeVv8VESLh",
        "index": 1,
        "attrdata": "all",
        "del": false,
    }
    const component = renderer.create(
        <TaskItem {...props} del={del} cbopenFormNewItemEdit={openFormNewItemEdit} cbsetPin={setPin}
            cbaddDel={addDel} cbdeleteTaskItem={deleteTaskItem} cbgetTime={getTime} cbsetColor={setColor} ></TaskItem>
    );
    let componentTree = component.toJSON();
    expect(componentTree).toMatchSnapshot();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-11-10
@Casufi

Yes.
If you need to check if a function is called, mock it with jest.fn and then check if it was called and with what parameters
https://jestjs.io/docs/expect#tohavebeencalled

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question