From 2f22972e7f8e3baac6294a3f6427fa0e5cb0b54a Mon Sep 17 00:00:00 2001 From: Anna Nikiforova Date: Mon, 22 Dec 2025 22:18:46 +0400 Subject: [PATCH] qa ifElse function solution --- src/ifElse.test.js | 67 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/src/ifElse.test.js b/src/ifElse.test.js index 95985e0..b39518a 100644 --- a/src/ifElse.test.js +++ b/src/ifElse.test.js @@ -1,11 +1,72 @@ 'use strict'; describe('ifElse', () => { - // const { ifElse } = require('./ifElse'); + const { ifElse } = require('./ifElse'); - it('should ', () => { + let condition; + let first; + let second; + beforeEach(() => { + condition = jest.fn(); + first = jest.fn(); + second = jest.fn(); }); - // write tests here + it('should be declared', () => { + expect(ifElse).toBeInstanceOf(Function); + }); + + it('should call first if condition returns true', () => { + condition.mockReturnValue(true); + + ifElse(condition, first, second); + + expect(first).toHaveBeenCalledTimes(1); + expect(second).not.toHaveBeenCalled(); + + expect(first).toHaveBeenCalledTimes(1); + expect(first).toHaveBeenCalledWith(); + + expect(second).not.toHaveBeenCalled(); + }); + + it('should call second if condition returns false', () => { + condition.mockReturnValue(false); + + ifElse(condition, first, second); + + expect(condition).toHaveBeenCalledTimes(1); + expect(condition).toHaveBeenCalledWith(); + + expect(first).not.toHaveBeenCalled(); + + expect(second).toHaveBeenCalledTimes(1); + expect(second).toHaveBeenCalledWith(); + }); + + it('should call condition without arguments', () => { + condition.mockReturnValue(true); + + ifElse(condition, first, second); + + expect(condition).toHaveBeenCalledTimes(1); + expect(condition).toHaveBeenCalledWith(); + }); + + it('should call callbacks without arguments', () => { + condition.mockReturnValue(false); + + ifElse(condition, first, second); + + expect(second).toHaveBeenCalledWith(); + }); + + it('should not return any value', () => { + condition.mockReturnValue(true); + + const result = ifElse(condition, first, second); + + expect(result).toBeUndefined(); + }); });