Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
'use strict';

describe('ifElse', () => {
// const { ifElse } = require('./ifElse');

it('should ', () => {
const { ifElse } = require('./ifElse');

it('should return undefined', () => {
expect(
ifElse(
() => Math.random() > 0.5,
() => {},
() => {},
),
).toBeUndefined();
});

// write tests here

it('should call first callback if condition is true', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great improvement! To fully meet the requirements, you also need to verify that the condition callback is called with no arguments. Please add an expectation to check this here and in the test case below.

expect(first).toHaveBeenCalledWith();
expect(second).not.toHaveBeenCalledWith();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good check. To be even more precise, you can use not.toHaveBeenCalled() here.

not.toHaveBeenCalledWith() checks that the mock was not called with these specific arguments, but it could have been called with others. not.toHaveBeenCalled() asserts that the function was not called at all, which is what we want to verify in this if branch.

});

it('should call second callback if condition is false', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);
expect(first).not.toHaveBeenCalledWith();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my other comment, using not.toHaveBeenCalled() would be slightly more accurate here. This ensures the first callback was not invoked at all when the condition is false.

expect(second).toHaveBeenCalledWith();
});
});