-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathifElse.test.js
More file actions
96 lines (72 loc) · 2.71 KB
/
ifElse.test.js
File metadata and controls
96 lines (72 loc) · 2.71 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
/* eslint-disable max-len */
describe('ifElse', () => {
const { ifElse } = require('./ifElse');
it('should be declared', () => {
expect(ifElse).toBeInstanceOf(Function);
});
it('should call the condition function exactly 1 time', () => {
const condition = jest.fn();
const first = jest.fn();
const second = jest.fn();
ifElse(condition, first, second);
expect(condition).toHaveBeenCalled();
expect(condition).toHaveBeenCalledTimes(1);
});
it('should call the first function exactly 1 time when condition returns true', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();
ifElse(condition, first, second);
expect(first).toHaveBeenCalled();
expect(first).toHaveBeenCalledTimes(1);
});
it('should not call second function when condition returns true', () => {
const condition = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();
ifElse(condition, first, second);
expect(first).toHaveBeenCalled();
expect(first).toHaveBeenCalledTimes(1);
expect(second).not.toHaveBeenCalled();
});
it('should call the second function exactly 1 time when condition returns false', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();
ifElse(condition, first, second);
expect(second).toHaveBeenCalled();
expect(second).toHaveBeenCalledTimes(1);
});
it('should not call first function when condition returns false', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();
ifElse(condition, first, second);
expect(second).toHaveBeenCalled();
expect(second).toHaveBeenCalledTimes(1);
expect(first).not.toHaveBeenCalled();
});
it('should not return anything', () => {
const condition = jest.fn(() => false);
const first = jest.fn();
const second = jest.fn();
expect(ifElse(condition, first, second)).toBeUndefined();
});
it('should call all the callbacks with no arguments', () => {
const condition1 = jest.fn(() => false);
const condition2 = jest.fn(() => true);
const first = jest.fn();
const second = jest.fn();
expect(ifElse(condition1, first, second)).toBeUndefined();
expect(condition1).toHaveBeenCalledTimes(1);
expect(condition1).toHaveBeenCalledWith();
expect(second).toHaveBeenCalledTimes(1);
expect(second).toHaveBeenCalledWith();
expect(ifElse(condition2, first, second)).toBeUndefined();
expect(condition2).toHaveBeenCalledTimes(1);
expect(condition2).toHaveBeenCalledWith();
expect(first).toHaveBeenCalledTimes(1);
expect(first).toHaveBeenCalledWith();
});
});