Skip to content
Open
Changes from all 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
91 changes: 88 additions & 3 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,96 @@
'use strict';
/* eslint-disable max-len */

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

it('should ', () => {
it('should be declared', () => {
expect(ifElse).toBeInstanceOf(Function);
});

it('should call the condition function exactly 1 time', () => {
const condition = jest.fn();
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(condition).toHaveBeenCalled();
expect(condition).toHaveBeenCalledTimes(1);
});

it('should call the first function exactly 1 time when condition returns true', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(first).toHaveBeenCalled();
expect(first).toHaveBeenCalledTimes(1);
});

// write tests here
it('should not call second function when condition returns true', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(first).toHaveBeenCalled();
expect(first).toHaveBeenCalledTimes(1);
expect(second).not.toHaveBeenCalled();
});

it('should call the second function exactly 1 time when condition returns false', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(second).toHaveBeenCalled();
expect(second).toHaveBeenCalledTimes(1);
});

it('should not call first function when condition returns false', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();

ifElse(condition, first, second);

expect(second).toHaveBeenCalled();
expect(second).toHaveBeenCalledTimes(1);
expect(first).not.toHaveBeenCalled();
});

it('should not return anything', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();

expect(ifElse(condition, first, second)).toBeUndefined();
});

it('should call all the callbacks with no arguments', () => {
const condition1 = jest.fn(() => false);
const condition2 = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();

expect(ifElse(condition1, first, second)).toBeUndefined();

expect(condition1).toHaveBeenCalledTimes(1);
expect(condition1).toHaveBeenCalledWith();
expect(second).toHaveBeenCalledTimes(1);
expect(second).toHaveBeenCalledWith();

expect(ifElse(condition2, first, second)).toBeUndefined();

expect(condition2).toHaveBeenCalledTimes(1);
expect(condition2).toHaveBeenCalledWith();
expect(first).toHaveBeenCalledTimes(1);
expect(first).toHaveBeenCalledWith();
});
Comment on lines +76 to +95

Choose a reason for hiding this comment

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

Great job adding the checks to ensure callbacks are called with no arguments! You've correctly addressed the main point from the previous review.

However, this single test case is doing too much by testing both the false and true paths for the condition. It's a best practice for each it block to test only one specific scenario. This makes tests easier to read and debug if they fail.

A better approach would be to integrate these toHaveBeenCalledWith() checks into your existing, more specific tests and then remove this one. For example:

  • In the test 'should call the first function...', you can add expect(first).toHaveBeenCalledWith().
  • In the test 'should call the second function...', you can add expect(second).toHaveBeenCalledWith().

});