File tree Expand file tree Collapse file tree 4 files changed +34
-12
lines changed
Expand file tree Collapse file tree 4 files changed +34
-12
lines changed Original file line number Diff line number Diff line change @@ -26,8 +26,10 @@ describe('useMap', () => {
2626
2727 it ( 'should init empty map if not initial object provided' , ( ) => {
2828 const { result } = setup ( ) ;
29-
3029 expect ( [ ...result . current [ 0 ] ] ) . toEqual ( [ ] ) ;
30+
31+ const { result : result2 } = setup ( undefined ) ;
32+ expect ( [ ...result2 . current [ 0 ] ] ) . toEqual ( [ ] ) ;
3133 } ) ;
3234
3335 it ( 'should get corresponding value for initial provided key' , ( ) => {
@@ -131,6 +133,12 @@ describe('useMap', () => {
131133 [ 'foo' , 'foo' ] ,
132134 [ 'a' , 2 ] ,
133135 ] ) ;
136+
137+ act ( ( ) => {
138+ // @ts -ignore
139+ utils . setAll ( ) ;
140+ } ) ;
141+ expect ( [ ...result . current [ 0 ] ] ) . toEqual ( [ ] ) ;
134142 } ) ;
135143
136144 it ( 'remove should be work' , ( ) => {
@@ -141,6 +149,24 @@ describe('useMap', () => {
141149 remove ( 'msg' ) ;
142150 } ) ;
143151 expect ( result . current [ 0 ] . size ) . toBe ( 0 ) ;
152+
153+ const { result : result2 } = setup ( [
154+ [ 'foo' , 'bar' ] ,
155+ [ 'a' , 1 ] ,
156+ [ 'b' , 2 ] ,
157+ [ 'c' , 3 ] ,
158+ ] ) ;
159+ const [ , utils ] = result2 . current ;
160+
161+ act ( ( ) => {
162+ utils . remove ( 'a' ) ;
163+ } ) ;
164+
165+ expect ( [ ...result2 . current [ 0 ] ] ) . toEqual ( [
166+ [ 'foo' , 'bar' ] ,
167+ [ 'b' , 2 ] ,
168+ [ 'c' , 3 ] ,
169+ ] ) ;
144170 } ) ;
145171
146172 it ( 'reset should be work' , ( ) => {
Original file line number Diff line number Diff line change @@ -2,11 +2,8 @@ import { useState } from 'react';
22import useMemoizedFn from '../useMemoizedFn' ;
33
44function useMap < K , T > ( initialValue ?: Iterable < readonly [ K , T ] > ) {
5- const getInitValue = ( ) => {
6- return initialValue === undefined ? new Map ( ) : new Map ( initialValue ) ;
7- } ;
8-
9- const [ map , setMap ] = useState < Map < K , T > > ( ( ) => getInitValue ( ) ) ;
5+ const getInitValue = ( ) => new Map ( initialValue ) ;
6+ const [ map , setMap ] = useState < Map < K , T > > ( getInitValue ) ;
107
118 const set = ( key : K , entry : T ) => {
129 setMap ( ( prev ) => {
Original file line number Diff line number Diff line change @@ -18,8 +18,10 @@ describe('useSet', () => {
1818
1919 it ( 'should init empty set if no initial set provided' , ( ) => {
2020 const { result } = setUp ( ) ;
21-
2221 expect ( result . current [ 0 ] ) . toEqual ( new Set ( ) ) ;
22+
23+ const { result : result1 } = setUp ( undefined ) ;
24+ expect ( result1 . current [ 0 ] ) . toEqual ( new Set ( ) ) ;
2325 } ) ;
2426
2527 it ( 'should have an initially provided key' , ( ) => {
Original file line number Diff line number Diff line change @@ -2,11 +2,8 @@ import { useState } from 'react';
22import useMemoizedFn from '../useMemoizedFn' ;
33
44function useSet < K > ( initialValue ?: Iterable < K > ) {
5- const getInitValue = ( ) => {
6- return initialValue === undefined ? new Set < K > ( ) : new Set ( initialValue ) ;
7- } ;
8-
9- const [ set , setSet ] = useState < Set < K > > ( ( ) => getInitValue ( ) ) ;
5+ const getInitValue = ( ) => new Set ( initialValue ) ;
6+ const [ set , setSet ] = useState < Set < K > > ( getInitValue ) ;
107
118 const add = ( key : K ) => {
129 if ( set . has ( key ) ) {
You can’t perform that action at this time.
0 commit comments