💾Unit Test

How to use Unit Test shortcut.

The "Unit Test" button is able to suggest you new code to create the unit test of the code you highlighted.

Let's see an example. We decided to put a high-complexity suggestion to show how poweful Safurai can be:

Request: the code you se above.

Safurai Suggestion: "Sure, here's an example of how you could write unit tests for the changeDiff function using the Jest testing framework:

describe('changeDiff', () => {
  it('resets expertHighScore and track selection when Standard is selected', () => {
    const setOptionSelected = jest.fn();
    const setTrackSelection = jest.fn();
    let optionSelected = 'Standard';
    let expertHighScore = '123';
    let currentHighScore = '456';

    changeDiff({ preventDefault: jest.fn() }, setOptionSelected, setTrackSelection, optionSelected, expertHighScore, currentHighScore);

    expect(setOptionSelected).toHaveBeenCalledWith(false);
    expect(expertHighScore).toBe('');
    expect(setTrackSelection).toHaveBeenCalledWith([]);
    expect(currentHighScore).toBe('456');
  });

  it('resets currentHighScore and track selection when Expert is selected', () => {
    const setOptionSelected = jest.fn();
    const setTrackSelection = jest.fn();
    let optionSelected = 'Expert';
    let expertHighScore = '123';
    let currentHighScore = '456';

    changeDiff({ preventDefault: jest.fn() }, setOptionSelected, setTrackSelection, optionSelected, expertHighScore, currentHighScore);

    expect(setOptionSelected).toHaveBeenCalledWith(false);
    expect(currentHighScore).toBe('');
    expect(setTrackSelection).toHaveBeenCalledWith([]);
    expect(expertHighScore).toBe('123');
  });
});

In this test suite, we're creating two test cases that cover the different paths through the

changeDiff function. We're using Jest's fn() mock function to create mock implementations of the setOptionSelected and setTrackSelection functions that are passed in as arguments to the changeDiff function.

We're also passing in the initial values of optionSelected, expertHighScore, and currentHighScore as arguments to the changeDiff function, so we can test that the function correctly updates these values based on the selected difficulty.

Each test case uses Jest's expect function to make assertions about the state of the component after the changeDiff function has been called. For example, in the first test case, we expect that setOptionSelected has been called with the value false, that expertHighScore has been set to an empty string, and that setTrackSelection called with an empty array. If any of these assertions fail, Jest will report a test failure and provide helpful feedback about what went wrong.

Note that in order to test this function, you'll need to modify it slightly to accept the

setOptionSelected, setTrackSelection, optionSelected, expertHighScore, and currentHighScore values as arguments, rather than relying on them being defined in the component scope. Here's an example of how you could modify the function to do this:

const changeDiff = (e, setOptionSelected, setTrackSelection, optionSelected, expertHighScore, currentHighScore) => {
  e.preventDefault();
  setOptionSelected(false);
  if (optionSelected === 'Standard') {
    expertHighScore = '';
    setTrackSelection([]);
  } else if (optionSelected === 'Expert') {
    currentHighScore = '';
    setTrackSelection([]);
  }
};

By making these changes, you can now test the

changeDiff function in isolation from the rest of your component code, which makes it much easier to write and debug your tests.

I hope this helps! Let me know if you have any other questions."

Last updated