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
69 changes: 64 additions & 5 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
'use strict';
"use strict";

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

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

it(`the first function must be called`, () => {
const condition = () => true;
let count = null;

const first = () => {
count = 1;
};

const second = () => {
count = 2;
};

ifElse(condition, first, second);
expect(count).toBe(1);
});

it(`if function returns a 'truthy'
the second function must be called`, () => {
Comment on lines +26 to +27

Choose a reason for hiding this comment

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

The title of this test is misleading. In JavaScript, it's standard for conditions to check for 'truthiness', not just strict equality with true. Therefore, if the condition function returns any truthy value (like 'hello'), the first function should be called.

const condition = () => "hello";
let count = null;

const first = () => {
count = 1;
};

const second = () => {
count = 2;
};

ifElse(condition, first, second);
expect(count).toBe(2);

Choose a reason for hiding this comment

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

Based on the standard behavior for truthy values, this should expect count to be 1. Making this change will correctly cause the test to fail, highlighting a small bug in the ifElse function's implementation where it's checking for === true instead of just the truthiness of the result.

});

it(`the second function must be called`, () => {

Choose a reason for hiding this comment

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

This test correctly handles the false case. To make your tests more robust, consider adding another test case that checks what happens when the condition function returns a 'truthy' value that isn't strictly true (e.g., 1 or 'hello'). According to the implementation's === true check, the second callback should still be invoked, and it's important to verify this behavior.

const condition = () => false;
let count = null;

const first = () => {
count = 1;
};

const second = () => {
count = 2;
};

ifElse(condition, first, second);
expect(count).toBe(2);
});

// write tests here
it(`should return undefined`, () => {
const result = ifElse(
() => Math.random() > 0.5,
// eslint-disable-next-line no-console
() => console.log(1),
// eslint-disable-next-line no-console
() => console.log(2),
);

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