Skip to content

Commit

Permalink
fix: "get" method in useMap updated to reference latest map object
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Loeschcke committed Dec 3, 2019
1 parent 07076ff commit 044d267
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/useMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const useMap = <T extends object = any>(initialMap: T = {} as T): [T, Actions<T>
},
reset: () => set(initialMap),
}),
[set]
[map, set]
);

return [map, utils];
Expand Down
30 changes: 16 additions & 14 deletions tests/useMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ it('should init empty map if not initial object provided', () => {
expect(result.current[0]).toEqual({});
});

it('should get corresponding value for existing provided key', () => {
it('should get corresponding value for initial provided key', () => {
const { result } = setUp({ foo: 'bar', a: 1 });
const [, utils] = result.current;

Expand All @@ -34,6 +34,21 @@ it('should get corresponding value for existing provided key', () => {
expect(value).toBe(1);
});

it('should get corresponding value for existing provided key', () => {
const { result } = setUp({ foo: 'bar', a: 1 });

act(() => {
result.current[1].set('a', 99);
});

let value;
act(() => {
value = result.current[1].get('a');
});

expect(value).toBe(99);
});

it('should get undefined for non-existing provided key', () => {
const { result } = setUp<{ foo: string; a: number; nonExisting?: any }>({ foo: 'bar', a: 1 });
const [, utils] = result.current;
Expand Down Expand Up @@ -106,16 +121,3 @@ it('should reset map to initial object provided', () => {

expect(result.current[0]).toEqual({ foo: 'bar', a: 1 });
});

it('should memoized its utils methods', () => {
const { result } = setUp({ foo: 'bar', a: 1 });
const [, utils] = result.current;
const { set } = utils;

act(() => {
set('foo', 'baz');
});

expect(result.current[1]).toBe(utils);
expect(result.current[1].set).toBe(set);
});

0 comments on commit 044d267

Please sign in to comment.