Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/ifElse.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

/**
* @param condition
* @param first
* @param second
* @param {Function} condition
* @param {Function} first
* @param {Function} second
*/
function ifElse(condition, first, second) {
if (condition() === true) {
if (condition()) {
first();
} else {
second();
Expand Down
58 changes: 55 additions & 3 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
'use strict';

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

Choose a reason for hiding this comment

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

Import/export mismatch: const { ifElse } = require('./ifElse'); expects a named export (an object with an ifElse property). The implementation exports the function directly (module.exports = ifElse), so this destructuring will result in ifElse being undefined and the tests will fail to run. Fix either by changing the import to const ifElse = require('./ifElse'); or by exporting an object from the implementation (module.exports = { ifElse };).

Choose a reason for hiding this comment

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

Import line uses destructuring (const { ifElse } = require('./ifElse');) which matches the current implementation export module.exports = { ifElse } in src/ifElse.js. No change needed to the import/export shape.


describe('ifElse', () => {
// const { ifElse } = require('./ifElse');
let firstMock;
let secondMock;
let conditionTrue;
let conditionFalse;

beforeEach(() => {
firstMock = jest.fn();
secondMock = jest.fn();
conditionTrue = jest.fn(() => true);
conditionFalse = jest.fn(() => false);
});

it('should call first callback if condition returns true', () => {
ifElse(conditionTrue, firstMock, secondMock);

Choose a reason for hiding this comment

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

In the "should call first callback if condition returns true" test you should assert the condition was called exactly once and with no arguments (requirement: "condition is invoked exactly once and without arguments"). After converting conditionTrue to a mock, add expect(conditionTrue).toHaveBeenCalledTimes(1); and expect(conditionTrue).toHaveBeenCalledWith(); before asserting which branch ran.


expect(conditionTrue).toHaveBeenCalledTimes(1);
expect(conditionTrue).toHaveBeenCalledWith();

expect(firstMock).toHaveBeenCalledTimes(1);
expect(firstMock).toHaveBeenCalledWith();

Choose a reason for hiding this comment

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

Add an assertion that conditionTrue was called before firstMock in the true-branch test. The task requires that condition is invoked before deciding which branch to execute (call order). One approach using Jest's mock internals is to compare invocationCallOrder values. Example to add (after the existing assertions for conditionTrue and firstMock):

// ensure condition ran before first
expect(conditionTrue.mock.invocationCallOrder[0]).toBeLessThan(firstMock.mock.invocationCallOrder[0]);

Place this near the true-branch assertions so the test verifies ordering as well as call counts/arguments.

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

it('should call second callback if condition returns false', () => {
ifElse(conditionFalse, firstMock, secondMock);

Choose a reason for hiding this comment

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

In the "should call second callback if condition returns false" test you likewise need to assert the condition was called exactly once and with no args. After converting conditionFalse to a mock, add expect(conditionFalse).toHaveBeenCalledTimes(1); and expect(conditionFalse).toHaveBeenCalledWith();.


expect(conditionFalse).toHaveBeenCalledTimes(1);
expect(conditionFalse).toHaveBeenCalledWith();

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

Choose a reason for hiding this comment

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

In the false-branch test, add an assertion that conditionFalse was called before secondMock. The implementation detail requires condition to be executed prior to the selected branch. You can use the same invocationCallOrder technique. Example to add (before or after the secondMock assertions):

expect(conditionFalse.mock.invocationCallOrder[0]).toBeLessThan(secondMock.mock.invocationCallOrder[0]);

This will ensure the test verifies the required call order for the false branch.

expect(secondMock).toHaveBeenCalledWith();

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

// write tests here
it('should work when condition is a dynamic function', () => {
const dynamicCondition = jest.fn(() => true);

ifElse(dynamicCondition, firstMock, secondMock);

expect(dynamicCondition).toHaveBeenCalledTimes(1);
expect(dynamicCondition).toHaveBeenCalledWith();

Choose a reason for hiding this comment

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

For the dynamic-condition test, add the same ordering assertion to ensure dynamicCondition runs before firstMock (since the dynamic condition returns true in this test). Example:

expect(dynamicCondition.mock.invocationCallOrder[0]).toBeLessThan(firstMock.mock.invocationCallOrder[0]);

This confirms the implementation consistently calls the condition before the chosen branch even for dynamic functions.

expect(firstMock).toHaveBeenCalledTimes(1);
expect(firstMock).toHaveBeenCalledWith();

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

it('should not return any value', () => {
const result = ifElse(conditionTrue, firstMock, secondMock);

expect(result).toBeUndefined();

Choose a reason for hiding this comment

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

The test checking that the function returns no value (expect(result).toBeUndefined();) already addresses the requirement that ifElse does not return a value. Keep this assertion as-is.

expect(conditionTrue).toHaveBeenCalledTimes(1);

Choose a reason for hiding this comment

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

In the "should not return any value" test, you already check result is undefined and that conditionTrue was called once. Also assert that conditionTrue was called with no arguments to fully satisfy the requirement that callbacks are invoked without arguments. Add:

expect(conditionTrue).toHaveBeenCalledWith();

This strengthens the test to verify both call count and that no args were passed to condition when ifElse is invoked.

});
});