Skip to content

Commit 94665c5

Browse files
committed
test: implement unit tests for useDeviceOrientation hook to ensure accurate orientation detection
1 parent 7b77235 commit 94665c5

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import { IterableUtil } from './IterableUtil';
2+
3+
/**
4+
* Tests for IterableUtil class.
5+
*
6+
* Note: The current implementation of readBoolean has a limitation - it doesn't actually
7+
* validate that the value is a boolean. It returns any truthy value as-is, or false for falsy values.
8+
* This behavior is tested below, but the implementation may need to be updated to properly
9+
* validate boolean types as suggested in the TODO comment in the source code.
10+
*/
11+
describe('IterableUtil', () => {
12+
describe('readBoolean', () => {
13+
it('should return true when the key exists and value is true', () => {
14+
// GIVEN a dictionary with a true boolean value
15+
const dict = { testKey: true };
16+
17+
// WHEN reading the boolean value
18+
const result = IterableUtil.readBoolean(dict, 'testKey');
19+
20+
// THEN it should return true
21+
expect(result).toBe(true);
22+
});
23+
24+
it('should return false when the key exists and value is false', () => {
25+
// GIVEN a dictionary with a false boolean value
26+
const dict = { testKey: false };
27+
28+
// WHEN reading the boolean value
29+
const result = IterableUtil.readBoolean(dict, 'testKey');
30+
31+
// THEN it should return false
32+
expect(result).toBe(false);
33+
});
34+
35+
it('should return false when the key does not exist', () => {
36+
// GIVEN a dictionary without the key
37+
const dict = { otherKey: true };
38+
39+
// WHEN reading a non-existent key
40+
const result = IterableUtil.readBoolean(dict, 'testKey');
41+
42+
// THEN it should return false
43+
expect(result).toBe(false);
44+
});
45+
46+
it('should return false when the key exists but value is undefined', () => {
47+
// GIVEN a dictionary with undefined value
48+
const dict = { testKey: undefined };
49+
50+
// WHEN reading the boolean value
51+
const result = IterableUtil.readBoolean(dict, 'testKey');
52+
53+
// THEN it should return false
54+
expect(result).toBe(false);
55+
});
56+
57+
it('should return false when the key exists but value is null', () => {
58+
// GIVEN a dictionary with null value
59+
const dict = { testKey: null };
60+
61+
// WHEN reading the boolean value
62+
const result = IterableUtil.readBoolean(dict, 'testKey');
63+
64+
// THEN it should return false
65+
expect(result).toBe(false);
66+
});
67+
68+
it('should return false when the key exists but value is 0', () => {
69+
// GIVEN a dictionary with 0 value
70+
const dict = { testKey: 0 };
71+
72+
// WHEN reading the boolean value
73+
const result = IterableUtil.readBoolean(dict, 'testKey');
74+
75+
// THEN it should return false
76+
expect(result).toBe(false);
77+
});
78+
79+
it('should return false when the key exists but value is empty string', () => {
80+
// GIVEN a dictionary with empty string value
81+
const dict = { testKey: '' };
82+
83+
// WHEN reading the boolean value
84+
const result = IterableUtil.readBoolean(dict, 'testKey');
85+
86+
// THEN it should return false
87+
expect(result).toBe(false);
88+
});
89+
90+
it('should return false when the key exists but value is NaN', () => {
91+
// GIVEN a dictionary with NaN value
92+
const dict = { testKey: NaN };
93+
94+
// WHEN reading the boolean value
95+
const result = IterableUtil.readBoolean(dict, 'testKey');
96+
97+
// THEN it should return false
98+
expect(result).toBe(false);
99+
});
100+
101+
it('should return truthy string as boolean when key exists', () => {
102+
// GIVEN a dictionary with truthy string value
103+
const dict = { testKey: 'true' };
104+
105+
// WHEN reading the boolean value
106+
const result = IterableUtil.readBoolean(dict, 'testKey');
107+
108+
// THEN it should return the string cast to boolean (truthy)
109+
expect(result).toBe('true');
110+
});
111+
112+
it('should return truthy number as boolean when key exists', () => {
113+
// GIVEN a dictionary with truthy number value
114+
const dict = { testKey: 1 };
115+
116+
// WHEN reading the boolean value
117+
const result = IterableUtil.readBoolean(dict, 'testKey');
118+
119+
// THEN it should return the number cast to boolean (truthy)
120+
expect(result).toBe(1);
121+
});
122+
123+
it('should return truthy object as boolean when key exists', () => {
124+
// GIVEN a dictionary with truthy object value
125+
const dict = { testKey: {} };
126+
127+
// WHEN reading the boolean value
128+
const result = IterableUtil.readBoolean(dict, 'testKey');
129+
130+
// THEN it should return the object cast to boolean (truthy)
131+
expect(result).toEqual({});
132+
});
133+
134+
it('should return truthy array as boolean when key exists', () => {
135+
// GIVEN a dictionary with truthy array value
136+
const dict = { testKey: [] };
137+
138+
// WHEN reading the boolean value
139+
const result = IterableUtil.readBoolean(dict, 'testKey');
140+
141+
// THEN it should return the array cast to boolean (truthy)
142+
expect(result).toEqual([]);
143+
});
144+
145+
it('should return truthy function as boolean when key exists', () => {
146+
// GIVEN a dictionary with truthy function value
147+
const dict = { testKey: () => {} };
148+
149+
// WHEN reading the boolean value
150+
const result = IterableUtil.readBoolean(dict, 'testKey');
151+
152+
// THEN it should return the function cast to boolean (truthy)
153+
expect(result).toBeInstanceOf(Function);
154+
});
155+
156+
it('should handle empty dictionary', () => {
157+
// GIVEN an empty dictionary
158+
const dict = {};
159+
160+
// WHEN reading a key from empty dictionary
161+
const result = IterableUtil.readBoolean(dict, 'testKey');
162+
163+
// THEN it should return false
164+
expect(result).toBe(false);
165+
});
166+
167+
it('should handle dictionary with multiple keys', () => {
168+
// GIVEN a dictionary with multiple keys
169+
const dict = {
170+
key1: true,
171+
key2: false,
172+
key3: 'string',
173+
key4: 123
174+
};
175+
176+
// WHEN reading different keys
177+
const result1 = IterableUtil.readBoolean(dict, 'key1');
178+
const result2 = IterableUtil.readBoolean(dict, 'key2');
179+
const result3 = IterableUtil.readBoolean(dict, 'key3');
180+
const result4 = IterableUtil.readBoolean(dict, 'key4');
181+
const result5 = IterableUtil.readBoolean(dict, 'nonExistentKey');
182+
183+
// THEN it should return correct values
184+
expect(result1).toBe(true);
185+
expect(result2).toBe(false);
186+
expect(result3).toBe('string'); // truthy string is returned as-is
187+
expect(result4).toBe(123); // truthy number is returned as-is
188+
expect(result5).toBe(false); // key doesn't exist
189+
});
190+
191+
it('should handle special boolean values', () => {
192+
// GIVEN a dictionary with special boolean values
193+
const dict = {
194+
trueValue: true,
195+
falseValue: false
196+
};
197+
198+
// WHEN reading boolean values
199+
const trueResult = IterableUtil.readBoolean(dict, 'trueValue');
200+
const falseResult = IterableUtil.readBoolean(dict, 'falseValue');
201+
202+
// THEN it should return the actual boolean values
203+
expect(trueResult).toBe(true);
204+
expect(falseResult).toBe(false);
205+
});
206+
207+
it('should handle case sensitivity in keys', () => {
208+
// GIVEN a dictionary with case-sensitive keys
209+
const dict = { TestKey: true, testkey: false };
210+
211+
// WHEN reading with different case
212+
const result1 = IterableUtil.readBoolean(dict, 'TestKey');
213+
const result2 = IterableUtil.readBoolean(dict, 'testkey');
214+
const result3 = IterableUtil.readBoolean(dict, 'TESTKEY');
215+
216+
// THEN it should be case sensitive
217+
expect(result1).toBe(true);
218+
expect(result2).toBe(false);
219+
expect(result3).toBe(false); // key doesn't exist
220+
});
221+
});
222+
});

0 commit comments

Comments
 (0)