Skip to content

Commit

Permalink
Make sure merge keys properly without "undefined" values.
Browse files Browse the repository at this point in the history
  • Loading branch information
zewish committed Nov 16, 2023
1 parent 3d9b0ee commit 9448ea7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"no-unused-vars": 0,
"no-underscore-dangle": 0,
"no-self-compare": 0,
"no-prototype-builtins": 0,
"function-paren-newline": 0,
"no-new-wrappers": 0,
"guard-for-in": 0,
Expand Down
47 changes: 46 additions & 1 deletion src/__tests__/rehydrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ describe('rehydrate.ts', () => {
});

describe('rehydrate()', () => {
let mockState = {};
const mockStore = {
dispatch: jest.fn()
dispatch: jest.fn(),
getState: jest.fn(() => mockState)
};

let rememberedKeys: string[];
Expand All @@ -195,7 +197,10 @@ describe('rehydrate.ts', () => {

beforeEach(() => {
rememberedKeys = ['3', '2', '1'];

mockState = {};
mockStore.dispatch = jest.fn();
mockStore.getState = jest.fn(() => mockState);

mockDriver = {
getItem: jest.fn((key) => `"${key}"`),
Expand Down Expand Up @@ -231,6 +236,18 @@ describe('rehydrate.ts', () => {
global.console.warn = consoleWarn;
});

it('calls store.getState()', async () => {
await exec({
driver: {
getItem: async () => ({})
},
unserialize: (o: any) => o,
persistWholeStore: true
});

expect(mockStore.getState).toBeCalledTimes(1);
});

it('calls store.dispatch()', async () => {
await exec({
unserialize: (o: any) => JSON.parse(o)
Expand Down Expand Up @@ -265,5 +282,33 @@ describe('rehydrate.ts', () => {
}
});
});

it('merges with existing state', async () => {
mockState = {
1: 'prev-state-1',
2: 'prev-state-2'
};

await exec({
driver: {
getItem: async () => ({
3: 'number-3',
2: 'number-2',
100: 'skip-me'
})
},
unserialize: (o: any) => o,
persistWholeStore: true
});

expect(mockStore.dispatch).nthCalledWith(1, {
type: REMEMBER_REHYDRATED,
payload: {
3: 'number-3',
2: 'number-2',
1: 'prev-state-1'
}
});
});
});
});
11 changes: 11 additions & 0 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describe('utils.ts', () => {
third: 'get me too'
});
});

it('does not copy non-existent properties', () => {
const src = {
first: 'get the first only',
skipped: 'I will be skipped :(',
};

expect(utils.pick(src, ['first', 'third'] as any)).toEqual({
first: 'get the first only'
});
});
});

describe('throttle()', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const pick = <T extends Record<string, any>, K extends keyof T>(

while (++index < keys.length) {
const key = keys[index];
dest[key] = src[key];

if (src.hasOwnProperty(key)) {
dest[key] = src[key];
}
}

return dest;
Expand Down

0 comments on commit 9448ea7

Please sign in to comment.