@@ -2,72 +2,136 @@ import { act, renderHook } from '@testing-library/react';
2
2
3
3
import { useObject } from './useObject' ;
4
4
5
- const INITIAL_OBJECT = { a : 1 , b : 2 , c : 3 } ;
6
-
7
- describe ( 'useObject' , ( ) => {
8
- it ( 'Should use object' , ( ) => {
9
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
10
- const obj = result . current ;
11
-
12
- expect ( obj . state ) . toEqual ( INITIAL_OBJECT ) ;
13
- expect ( obj . set ) . toBeTypeOf ( 'function' ) ;
14
- expect ( obj . get ) . toBeTypeOf ( 'function' ) ;
15
- expect ( obj . reset ) . toBeTypeOf ( 'function' ) ;
16
- expect ( obj . update ) . toBeTypeOf ( 'function' ) ;
17
- expect ( obj . merge ) . toBeTypeOf ( 'function' ) ;
18
- expect ( obj . remove ) . toBeTypeOf ( 'function' ) ;
19
- } ) ;
5
+ it ( 'Should use object' , ( ) => {
6
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
7
+
8
+ expect ( result . current . value ) . toEqual ( { a : 1 , b : 2 , c : 3 } ) ;
9
+ expect ( result . current . set ) . toBeTypeOf ( 'function' ) ;
10
+ expect ( result . current . reset ) . toBeTypeOf ( 'function' ) ;
11
+ expect ( result . current . remove ) . toBeTypeOf ( 'function' ) ;
12
+ expect ( result . current . clear ) . toBeTypeOf ( 'function' ) ;
13
+ expect ( result . current . has ) . toBeTypeOf ( 'function' ) ;
14
+ expect ( result . current . keys ) . toBeTypeOf ( 'object' ) ;
15
+ expect ( result . current . empty ) . toBeTypeOf ( 'boolean' ) ;
16
+ expect ( result . current . size ) . toBeTypeOf ( 'number' ) ;
17
+ } ) ;
20
18
21
- it ( 'Should set property' , ( ) => {
22
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
23
- const obj = result . current ;
19
+ it ( 'Should set initial value' , ( ) => {
20
+ const { result } = renderHook ( ( ) => useObject ( { name : 'John' , age : 30 } ) ) ;
24
21
25
- act ( ( ) => obj . set ( 'a' , 42 ) ) ;
22
+ expect ( result . current . value ) . toEqual ( { name : 'John' , age : 30 } ) ;
23
+ } ) ;
26
24
27
- expect ( result . current . state ) . toEqual ( { a : 42 , b : 2 , c : 3 } ) ;
28
- } ) ;
25
+ it ( 'Should set partial object' , ( ) => {
26
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
29
27
30
- it ( 'Should get property' , ( ) => {
31
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
32
- const obj = result . current ;
28
+ act ( ( ) => result . current . set ( { a : 42 } ) ) ;
33
29
34
- expect ( obj . get ( 'b' ) ) . toBe ( 2 ) ;
35
- } ) ;
30
+ expect ( result . current . value ) . toEqual ( { a : 42 , b : 2 , c : 3 } ) ;
31
+ } ) ;
36
32
37
- it ( 'Should reset object' , ( ) => {
38
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
39
- const obj = result . current ;
33
+ it ( 'Should set multiple properties' , ( ) => {
34
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
40
35
41
- act ( ( ) => obj . set ( 'a' , 99 ) ) ;
42
- act ( ( ) => obj . reset ( ) ) ;
36
+ act ( ( ) => result . current . set ( { a : 42 , c : 99 } ) ) ;
43
37
44
- expect ( result . current . state ) . toEqual ( INITIAL_OBJECT ) ;
45
- } ) ;
38
+ expect ( result . current . value ) . toEqual ( { a : 42 , b : 2 , c : 99 } ) ;
39
+ } ) ;
46
40
47
- it ( 'Should update object' , ( ) => {
48
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
49
- const obj = result . current ;
41
+ it ( 'Should reset object' , ( ) => {
42
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 } ) ) ;
50
43
51
- act ( ( ) => obj . update ( ( prev ) => ( { ...prev , d : 4 } ) as typeof INITIAL_OBJECT & { d : number } ) ) ;
44
+ act ( ( ) => result . current . set ( { a : 99 } ) ) ;
45
+ act ( ( ) => result . current . reset ( ) ) ;
52
46
53
- expect ( result . current . state ) . toEqual ( { a : 1 , b : 2 , c : 3 , d : 4 } ) ;
54
- } ) ;
47
+ expect ( result . current . value ) . toEqual ( { a : 1 , b : 2 } ) ;
48
+ } ) ;
49
+
50
+ it ( 'Should remove property' , ( ) => {
51
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
52
+
53
+ act ( ( ) => result . current . remove ( 'b' ) ) ;
54
+
55
+ expect ( result . current . value ) . toEqual ( { a : 1 , c : 3 } ) ;
56
+ } ) ;
57
+
58
+ it ( 'Should not remove non-existing property' , ( ) => {
59
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 } ) ) ;
60
+
61
+ act ( ( ) => result . current . remove ( 'c' as any ) ) ;
62
+
63
+ expect ( result . current . value ) . toEqual ( { a : 1 , b : 2 } ) ;
64
+ } ) ;
65
+
66
+ it ( 'Should clear object' , ( ) => {
67
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
68
+
69
+ act ( ( ) => result . current . clear ( ) ) ;
70
+
71
+ expect ( result . current . value ) . toEqual ( { } ) ;
72
+ } ) ;
73
+
74
+ it ( 'Should check if property exists' , ( ) => {
75
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 } ) ) ;
76
+
77
+ expect ( result . current . has ( 'a' ) ) . toBe ( true ) ;
78
+ expect ( result . current . has ( 'b' ) ) . toBe ( true ) ;
79
+ expect ( result . current . has ( 'c' as any ) ) . toBe ( false ) ;
80
+ } ) ;
81
+
82
+ it ( 'Should return object keys' , ( ) => {
83
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
55
84
56
- it ( 'Should merge object' , ( ) => {
57
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
58
- const obj = result . current ;
85
+ expect ( result . current . keys ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
86
+ } ) ;
87
+
88
+ it ( 'Should update keys when object changes' , ( ) => {
89
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
90
+
91
+ expect ( result . current . keys ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
92
+
93
+ act ( ( ) => result . current . remove ( 'b' ) ) ;
94
+
95
+ expect ( result . current . keys ) . toEqual ( [ 'a' , 'c' ] ) ;
96
+ } ) ;
97
+
98
+ it ( 'Should check if object is empty' , ( ) => {
99
+ const { result } = renderHook ( ( ) => useObject ( { } ) ) ;
100
+
101
+ expect ( result . current . empty ) . toBe ( true ) ;
102
+
103
+ act ( ( ) => result . current . set ( { a : 1 } ) ) ;
104
+
105
+ expect ( result . current . empty ) . toBe ( false ) ;
106
+ } ) ;
107
+
108
+ it ( 'Should return object size' , ( ) => {
109
+ const { result } = renderHook ( ( ) => useObject ( { a : 1 , b : 2 , c : 3 } ) ) ;
110
+
111
+ expect ( result . current . size ) . toBe ( 3 ) ;
112
+
113
+ act ( ( ) => result . current . remove ( 'b' ) ) ;
114
+
115
+ expect ( result . current . size ) . toBe ( 2 ) ;
116
+ } ) ;
59
117
60
- act ( ( ) => obj . merge ( { b : 10 } ) ) ;
118
+ describe ( 'Empty object' , ( ) => {
119
+ it ( 'Should handle empty initial object' , ( ) => {
120
+ const { result } = renderHook ( ( ) => useObject ( { } ) ) ;
61
121
62
- expect ( result . current . state ) . toEqual ( { a : 1 , b : 10 , c : 3 } ) ;
122
+ expect ( result . current . value ) . toEqual ( { } ) ;
123
+ expect ( result . current . empty ) . toBe ( true ) ;
124
+ expect ( result . current . size ) . toBe ( 0 ) ;
125
+ expect ( result . current . keys ) . toEqual ( [ ] ) ;
63
126
} ) ;
64
127
65
- it ( 'Should remove property' , ( ) => {
66
- const { result } = renderHook ( ( ) => useObject ( INITIAL_OBJECT ) ) ;
67
- const obj = result . current ;
128
+ it ( 'Should add properties to empty object' , ( ) => {
129
+ const { result } = renderHook ( ( ) => useObject ( { } ) ) ;
68
130
69
- act ( ( ) => obj . remove ( 'b' ) ) ;
131
+ act ( ( ) => result . current . set ( { a : 1 , b : 2 } ) ) ;
70
132
71
- expect ( result . current . state ) . toEqual ( { a : 1 , c : 3 } ) ;
133
+ expect ( result . current . value ) . toEqual ( { a : 1 , b : 2 } ) ;
134
+ expect ( result . current . empty ) . toBe ( false ) ;
135
+ expect ( result . current . size ) . toBe ( 2 ) ;
72
136
} ) ;
73
137
} ) ;
0 commit comments