-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathifElse.test.js
More file actions
63 lines (44 loc) · 1.68 KB
/
ifElse.test.js
File metadata and controls
63 lines (44 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const { ifElse } = require('./ifElse');
describe('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();
expect(secondMock).toHaveBeenCalledTimes(1);
expect(secondMock).toHaveBeenCalledWith();
expect(firstMock).not.toHaveBeenCalled();
});
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);
});
});