-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseChangeBackgroundColor.test.ts
89 lines (73 loc) · 3.73 KB
/
useChangeBackgroundColor.test.ts
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
import { act, renderHook } from '@testing-library/react';
import _ from 'lodash';
import React from 'react';
import { Color, retrieveNewColorOfList, useChangeBackgroundColor } from './useChangeBackgroundColor';
/**
Mock the whole `lodash` library, uses the original implementation (requireActual)
and mock the `shuffle` only (to spy on them later).
*/
jest.mock('lodash', () => ({
...jest.requireActual('lodash'),
shuffle: jest.fn(),
}));
/**
* Spy function for the 'shuffle' method of the `lodash` library.
*
* The 'mockedShuffle' variable is a Jest function spy, which allows you
* to track calls to the `shuffle`-method and provide custom behavior.
*
* @type {jest.SpyInstance}
*/
const mockedShuffle: jest.SpyInstance = jest.spyOn(_, 'shuffle');
describe('useChangeBackgroundColorOfSection', () => {
test('should retrieve the only color from a list with a single color', () => {
// Needs no return-value of the spied `mockedShuffle`-function,
// cause in this case the internal `shuffle`-method wasn't called.
const colors: Color[] = ['green'];
const newColor = retrieveNewColorOfList(colors);
expect(newColor).toEqual('green');
});
test('should retrieve a different color from a list with a multiple colors', () => {
// Mocks the return-values of the spied `mockedShuffle`-function for each internal call of the `shuffle`-method.
mockedShuffle.mockReturnValueOnce(['red']).mockReturnValueOnce(['yellow']).mockReturnValueOnce(['green']);
const colors: Color[] = ['red', 'yellow', 'green'];
const firstNewColor = retrieveNewColorOfList(colors);
const secondNewColor = retrieveNewColorOfList(colors);
const thirdNewColor = retrieveNewColorOfList(colors);
expect(firstNewColor).toEqual('red');
expect(secondNewColor).toEqual('yellow');
expect(thirdNewColor).toEqual('green');
});
test('should not choose the same color twice', () => {
// Mocks the return-values of the spied `mockedShuffle`-function for each internal call of the `shuffle`-method.
mockedShuffle.mockReturnValueOnce(['red']).mockReturnValueOnce(['green']);
const colors: Color[] = ['red', 'yellow', 'green'];
const firstNewColor = retrieveNewColorOfList(colors);
const secondNewColor = retrieveNewColorOfList(colors);
expect(firstNewColor).toBe('red');
expect(secondNewColor).toBe('green');
});
test('should change the background-color of an element', () => {
// Mocks the return-values of the spied `mockedShuffle`-function for each internal call of the `shuffle`-method.
mockedShuffle.mockReturnValueOnce(['red']);
// Mocks the return-value of Reacts `useRef`-function to simulate an element and make the condition truthy.
jest.spyOn(React, 'useRef').mockReturnValue({ current: { style: { backgroundColor: 'white' } } });
const colors: Color[] = ['red', 'yellow', 'green'];
const { result } = renderHook(() => useChangeBackgroundColor(colors));
act(() => result.current.changeBackgroundColor());
expect(result.current.reference.current?.style.backgroundColor).toBe('red');
});
test("should not change any background-color of an element, because there's no reference to an element", () => {
// Mocks the return-values of the spied `mockedShuffle`-function for each internal call of the `shuffle`-method.
mockedShuffle.mockReturnValueOnce(['red']);
// Mocks the return-value of Reacts `useRef`-function to simulate an element and make the condition truthy.
jest.spyOn(React, 'useRef').mockReturnValue({ current: null });
const colors: Color[] = ['red', 'yellow', 'green'];
const { result } = renderHook(() => useChangeBackgroundColor(colors));
act(() => result.current.changeBackgroundColor());
expect(result.current.reference.current?.style.backgroundColor).toBeUndefined();
});
afterEach(() => {
mockedShuffle.mockRestore();
});
});