Skip to content
Open
Changes from 1 commit
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
42 changes: 39 additions & 3 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
'use strict';

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

describe('ifElse', () => {
// const { ifElse } = require('./ifElse');
it('calls the condition callback once', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(condition).toHaveBeenCalledTimes(1);

Choose a reason for hiding this comment

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

This assertion correctly verifies that condition is called. The requirements also state that it should be run with no arguments. You can strengthen this test by also asserting it was called with no arguments, for example, by using .toHaveBeenCalledWith().

});

it('runs the first callback when condition returns true', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

it('should ', () => {
expect(first).toHaveBeenCalledTimes(1);

Choose a reason for hiding this comment

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

Good job checking that the first callback is executed. To make the test fully align with the implied behavior from the task description's example, you could also add an assertion that first is called with no arguments.

expect(second).not.toHaveBeenCalled();
});

it('runs the second callback when condition returns false', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(second).toHaveBeenCalledTimes(1);

Choose a reason for hiding this comment

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

This correctly tests that the second callback is run. The requirements explicitly state that second should be run 'with no arguments'. It would be best to add an assertion to verify this, for instance, with .toHaveBeenCalledWith().

expect(first).not.toHaveBeenCalled();
});

// write tests here
it('returns undefined', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

const result = ifElse(condition, first, second);

expect(result).toBeUndefined();
});
});