Skip to content

Commit

Permalink
Improve code coverage and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zewish committed Oct 5, 2021
1 parent 505c0b3 commit e558b58
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ API reference
2. **rememberedKeys** *(required)* - an array of persistable keys - if an empty array is provided nothing will get persisted;
3. **options** *(optional)* - plain object of extra options:
- **prefix**: storage key prefix *(default: `'@@remember-'`)*;
- **serialize** - a plain function that takes unserialized store state and its key (`serialize(state, keyStr)`) and returns serialized state to be persisted *(default: `JSON.stringify`)*;
- **unserialize** - a plain function that takes serialized persisted state and its key (`serialize(serializedStr, keyStr)`) and returns unserialized to be set in the store *(default: `JSON.parse`)*;
- **serialize** - a plain function that takes unserialized store state and its key (`serialize(state, stateKey)`) and returns serialized state to be persisted *(default: `JSON.stringify`)*;
- **unserialize** - a plain function that takes serialized persisted state and its key (`serialize(state, stateKey)`) and returns unserialized to be set in the store *(default: `JSON.parse`)*;
- **persistThrottle** - how much time should the persistence be throttled in milliseconds *(default: `100`)*
- **persistWholeStore** - a boolean which specifies if the whole store should be persisted at once. Generally only use this if you're using your own storage driver which has gigabytes of storage limits. Don't use this when using window.localStorage, window.sessionStorage or AsyncStorage as their limits are quite small. When using this option, key won't be passed to `serialize` nor `unserialize` functions - *(default: `false`)*;
- Returns - an enhancer to be used with Redux
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Action, AnyAction, Reducer, StoreEnhancer } from 'redux';
declare const REMEMBER_REHYDRATED = "@@REMEMBER_REHYDRATED";
declare const REMEMBER_PERSISTED = "@@REMEMBER_PERSISTED";
type SerializeFunction = (data: any, key?: string) => any;
type UnserializeFunction = (data: any, key?: string) => any;
type SerializeFunction = (data: any, key: string) => any;
type UnserializeFunction = (data: any, key: string) => any;
type Driver = {
getItem: (key: string) => any;
setItem: (key: string, value: any) => any;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-remember",
"version": "2.1.4",
"version": "2.2.0",
"description": "Saves and loads your redux state from a key-value store of your choice",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
30 changes: 19 additions & 11 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as indexModule from '../index';
import * as actionTypes from '../action-types';
import { AnyAction, Reducer, StoreEnhancer } from 'redux';
import { Driver, Options } from '../types';
import { Reducer, StoreCreator } from 'redux';
import { Options } from '../types';

describe('index.js', () => {
let mockRehydrate = {
Expand Down Expand Up @@ -111,8 +111,7 @@ describe('index.js', () => {

it('calls createStore function and returns its result', () => {
const store = 'dummy store!!!';
const createStore = jest.fn(() => store);

const createStore = jest.fn(() => store) as StoreCreator;
const rootReducer = () => 'beep';
const initialState = 'dummyInitialState';
const enhancer: any = 'dummyEnhancer';
Expand All @@ -125,7 +124,8 @@ describe('index.js', () => {
[]
);

const res = enhancerInstance(createStore)(
const storeMaker: StoreCreator = enhancerInstance(createStore);
const res = storeMaker(
rootReducer, initialState, enhancer
);

Expand Down Expand Up @@ -158,7 +158,11 @@ describe('index.js', () => {
unserialize: (o) => o
};

index.rememberEnhancer(driver, rememberedKeys, opts)(() => store)(
const storeMaker: StoreCreator = index.rememberEnhancer(
driver, rememberedKeys, opts
)((() => store) as StoreCreator);

storeMaker(
rootReducer, initialState, enhancer
);

Expand All @@ -174,7 +178,7 @@ describe('index.js', () => {
mockInit.mockImplementationOnce((store, rememberedKeys, opts) => {
optionDefaults = opts;
});
const store = 'the store!!!';
const store = 'some-store';

const driver = {
getItem() {},
Expand All @@ -187,7 +191,11 @@ describe('index.js', () => {
const initialState = 'yup, initial state';
const enhancer: any = 'another enhancer';

index.rememberEnhancer(driver, rememberedKeys)(() => store)(
const storeMaker: StoreCreator = index.rememberEnhancer(
driver, rememberedKeys
)((() => store) as StoreCreator);

storeMaker(
rootReducer, initialState, enhancer
);

Expand All @@ -196,12 +204,12 @@ describe('index.js', () => {
rememberedKeys,
{ driver, ...optionDefaults }
)

const stringifySpy = jest.spyOn(JSON, 'stringify');
const parseSpy = jest.spyOn(JSON, 'parse');

expect(optionDefaults).toMatchObject({
prefix : '@persist-',
prefix : '@@remember-',
persistThrottle : 100,
persistWholeStore : false
});
Expand Down
24 changes: 12 additions & 12 deletions src/__tests__/persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('persist.js', () => {
{
prefix: 'yada.',
driver: mockDriver,
serialize: JSON.stringify
serialize: (string, key) => JSON.stringify(string)
}
);

Expand Down Expand Up @@ -154,8 +154,9 @@ describe('persist.js', () => {
state,
oldState,
{
prefix: 'bla',
driver: mockDriver,
serialize() {}
serialize(state, key) {}
}
);

Expand Down Expand Up @@ -189,7 +190,7 @@ describe('persist.js', () => {
}
);

expect(mockSerialize).toBeCalledWith(state);
expect(mockSerialize).toBeCalledWith(state, '@@yada-state@@');
});

it('calls driver.setItem()', async () => {
Expand Down Expand Up @@ -247,15 +248,7 @@ describe('persist.js', () => {
{ key1: 'old' },
{
driver: mockDriver,
serialize: (o: any) => o
}
);

await mod.persist(
{ key1: 'new' },
{ key1: 'old' },
{
driver: mockDriver,
prefix: 'one',
persistWholeStore: false,
serialize: (o: any) => o
}
Expand All @@ -266,6 +259,7 @@ describe('persist.js', () => {
{ key1: 'old' },
{
driver: mockDriver,
prefix: 'two',
persistWholeStore: true,
serialize: (o: any) => o
}
Expand All @@ -278,6 +272,7 @@ describe('persist.js', () => {
undefined,
{
driver: mockDriver,
prefix: 'three',
persistWholeStore: false,
serialize: (o: any) => o
}
Expand All @@ -288,6 +283,7 @@ describe('persist.js', () => {
undefined,
{
driver: mockDriver,
prefix: 'four',
persistWholeStore: true,
serialize: (o: any) => o
}
Expand All @@ -304,6 +300,7 @@ describe('persist.js', () => {
const error2 = 'DUMMY ERROR 2!!!';

const mockDriver = {
getItem: (key: string) => {},
setItem: (
jest.fn()
.mockRejectedValueOnce(error1)
Expand All @@ -318,6 +315,8 @@ describe('persist.js', () => {
{ key1: 'yay' },
{ key1: 'cool' },
{
prefix: 'beep',
persistWholeStore: false,
driver: mockDriver,
serialize: (o: any) => o
}
Expand All @@ -327,6 +326,7 @@ describe('persist.js', () => {
{ key1: 'yay' },
{ key1: 'cool' },
{
prefix: 'boop',
driver: mockDriver,
persistWholeStore: true,
serialize: (o: any) => o
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const rememberEnhancer = <Ext = {}, StateExt = {}>(
driver: Driver,
rememberedKeys: string[],
{
prefix = '@persist-',
prefix = '@@remember-',
serialize = (data) => JSON.stringify(data),
unserialize = (data) => JSON.parse(data),
persistThrottle = 100,
Expand Down
2 changes: 1 addition & 1 deletion src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const saveAll = (
if (!isEqual(state, oldState)) {
return driver.setItem(
`${prefix}state@@`,
serialize(state)
serialize(state, `${prefix}state@@`)
);
}
};
Expand Down
7 changes: 3 additions & 4 deletions src/rehydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ export const loadAll = async ({
prefix,
unserialize
}: LoadAllOptions) => {
const data = await driver.getItem(
`${prefix}state@@`
);
const key = `${prefix}state@@`;
const data = await driver.getItem(key);

if (data === null || data == undefined) {
return {};
}

return pick(
unserialize(data),
unserialize(data, key),
rememberedKeys
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type SerializeFunction = (data: any, key?: string) => any;
export type UnserializeFunction = (data: any, key?: string) => any;
export type SerializeFunction = (data: any, key: string) => any;
export type UnserializeFunction = (data: any, key: string) => any;

export type Driver = {
getItem: (key: string) => any;
Expand Down

0 comments on commit e558b58

Please # to comment.