-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathuseTextStyle.test.tsx
More file actions
168 lines (126 loc) · 5.18 KB
/
useTextStyle.test.tsx
File metadata and controls
168 lines (126 loc) · 5.18 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React from 'react';
import { cleanup, render } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react-hooks';
import useTextStyle from '../useTextStyle';
import {
getAvailableWidth,
getRangeWidth,
getStyle,
getValidContainerElement,
transitionWidth,
} from '../utils';
jest.mock('../utils', () => ({
getValidContainerElement: jest.fn(),
getRangeWidth: jest.fn(),
getStyle: jest.fn(),
getAvailableWidth: jest.fn(),
transitionWidth: jest.fn(),
}));
describe('Test useTextStyle', () => {
const mockGetRangeWidth = getRangeWidth as jest.Mock;
const mockGetStyle = getStyle as jest.Mock;
const mockGetValidContainerElement = getValidContainerElement as jest.Mock;
const mockTransitionWidth = transitionWidth as jest.Mock;
const mockGetAvailableWidth = getAvailableWidth as jest.Mock;
beforeEach(() => {
cleanup();
jest.clearAllMocks();
mockGetValidContainerElement.mockReturnValue(<div style={{ width: 100 }}></div>);
});
it('should return a ref, overflow state, style, and trigger function', () => {
const { result } = renderHook(() => useTextStyle('Test Text'));
const [ref, isOverflow, style, updateTextStyle] = result.current;
expect(ref).toBeInstanceOf(Object);
expect(typeof isOverflow).toBe('boolean');
expect(style).toBeInstanceOf(Object);
expect(typeof updateTextStyle).toBe('function');
});
it('should calculate overflow correctly', () => {
const { result } = renderHook(() => useTextStyle('Test Text'));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockGetRangeWidth.mockReturnValue(150);
mockGetAvailableWidth.mockReturnValue(100);
act(() => {
updateTextStyle();
});
const [, isOverflow, style] = result.current;
expect(mockGetRangeWidth).toHaveBeenCalled();
expect(isOverflow).toBe(true);
expect(style.maxWidth).toBe(100);
});
it('should not overflow if the container width is sufficient', () => {
const { result } = renderHook(() => useTextStyle('Test Text'));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockGetRangeWidth.mockReturnValue(80);
mockGetAvailableWidth.mockReturnValue(100);
act(() => {
updateTextStyle();
});
const [, isOverflow, style] = result.current;
expect(isOverflow).toBe(false);
expect(style.maxWidth).toBe(100);
});
it('should inherit cursor style from parent', () => {
const { result } = renderHook(() => useTextStyle('Test Text'));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockGetStyle.mockReturnValue('pointer');
act(() => {
updateTextStyle();
});
const [, , style] = result.current;
expect(style.cursor).toBe('pointer');
});
it('should have cursor style as default when text is not overflowing and parent cursor is default', () => {
const { result } = renderHook(() => useTextStyle('Test Text'));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockGetRangeWidth.mockReturnValue(80);
mockGetAvailableWidth.mockReturnValue(100);
mockGetStyle.mockReturnValue('default');
act(() => {
updateTextStyle();
});
const [, isOverflow, style] = result.current;
expect(isOverflow).toBe(false);
expect(style.cursor).toBe('default');
});
it('should have cursor style as pointer when text is overflowing and parent cursor is default', () => {
const { result } = renderHook(() => useTextStyle('Test Text'));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockGetRangeWidth.mockReturnValue(150);
mockGetAvailableWidth.mockReturnValue(100);
mockGetStyle.mockReturnValue('default');
act(() => {
updateTextStyle();
});
const [, isOverflow, style] = result.current;
expect(isOverflow).toBe(true);
expect(style.cursor).toBe('pointer');
});
it('should set container width when container width < maxWidth', () => {
const { result } = renderHook(() => useTextStyle('Test Text', 120));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockTransitionWidth.mockReturnValue(100);
act(() => {
updateTextStyle();
});
const [, , style] = result.current;
expect(style.maxWidth).toBe(100);
});
it('should set maxWidth when container width > maxWidth ', () => {
const { result } = renderHook(() => useTextStyle('Test Text', 80));
const [ref, , , updateTextStyle] = result.current;
render(<span ref={ref}></span>);
mockTransitionWidth.mockReturnValue(80);
act(() => {
updateTextStyle();
});
const [, , style] = result.current;
expect(style.maxWidth).toBe(80);
});
});