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
52 changes: 47 additions & 5 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,53 @@
'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);
});

// write tests here
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);
});

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();
});
});