diff --git a/src/ifElse.js b/src/ifElse.js index 7225adc..72819c5 100644 --- a/src/ifElse.js +++ b/src/ifElse.js @@ -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(); diff --git a/src/ifElse.test.js b/src/ifElse.test.js index 95985e0..1aed025 100644 --- a/src/ifElse.test.js +++ b/src/ifElse.test.js @@ -1,11 +1,63 @@ 'use strict'; +const { ifElse } = require('./ifElse'); + 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); + + expect(conditionTrue).toHaveBeenCalledTimes(1); + expect(conditionTrue).toHaveBeenCalledWith(); + + expect(firstMock).toHaveBeenCalledTimes(1); + expect(firstMock).toHaveBeenCalledWith(); + + expect(secondMock).not.toHaveBeenCalled(); + }); + + it('should call second callback if condition returns false', () => { + ifElse(conditionFalse, firstMock, secondMock); + + expect(conditionFalse).toHaveBeenCalledTimes(1); + expect(conditionFalse).toHaveBeenCalledWith(); - it('should ', () => { + expect(secondMock).toHaveBeenCalledTimes(1); + 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(); + + 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(); + expect(conditionTrue).toHaveBeenCalledTimes(1); + }); }); +