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
68 changes: 65 additions & 3 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
'use strict';

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

it('should ', () => {});
let truthyCondition, falsyCondition, first, second;

// write tests here
beforeAll(() => {
truthyCondition = jest.fn(() => true);
falsyCondition = jest.fn(() => false);

first = jest.fn(() => 'First');
second = jest.fn(() => 'Second');
});

afterEach(() => {
first.mockClear();
second.mockClear();
truthyCondition.mockClear();
falsyCondition.mockClear();
});

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

it('should return nothing', () => {
expect(ifElse(truthyCondition, first, second)).toEqual(undefined);
});

it('should invoke first cb if truthy contidion', () => {
ifElse(truthyCondition, first, second);

expect(first).toHaveBeenCalled();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This assertion is correct. To make it more precise according to the requirements, you could also verify that the callback is called with no arguments. You can do this by changing the assertion to expect(first).toHaveBeenCalledWith();.

});

it('should invoke second cb if falsy condition', () => {
ifElse(falsyCondition, first, second);

expect(second).toHaveBeenCalled();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similar to the previous test, you can make this check more specific by verifying that the second callback is called with no arguments, as required. Using expect(second).toHaveBeenCalledWith(); would accomplish this.

});

it('should invoke one of functions in any case', () => {
const randomCondition = jest.fn().mockReturnValue(!!(Math.random() > 0.5));

ifElse(randomCondition, first, second);

const totalCalls = first.mock.calls.length + second.mock.calls.length;

expect(totalCalls).toBe(1);
});

it('should not work without condition', () => {
expect(() => {
ifElse(undefined, first, second);
}).toThrow();
});

it('should invoke first or second functions without args', () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a great step forward! You're now correctly checking that first and second are called without arguments.

To fully meet the requirements, you also need to verify that the condition callback is called with no arguments.

A good way to organize this would be to merge these checks into the tests for the truthy and falsy paths. For example, the 'should invoke first cb' test could verify that both truthyCondition and first were called with .toHaveBeenCalledWith(), which would make this separate test case unnecessary.

ifElse(truthyCondition, first, second);
expect(first).toHaveBeenCalledWith();

ifElse(falsyCondition, first, second);
expect(second).toHaveBeenLastCalledWith();
});

it('should invoke condition without any args', () => {
ifElse(truthyCondition, first, second);
expect(truthyCondition).toHaveBeenCalledWith();
});
});
Loading