Skip to content

Commit

Permalink
test: πŸ’ add yarn.lock, refresh Jest deps, fix localStorage test
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Sep 30, 2018
1 parent c4b149d commit 9a1dd99
Show file tree
Hide file tree
Showing 7 changed files with 12,146 additions and 52 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ node_modules/
/.nyc_output/
/coverage/
package-lock.json
yarn.lock
/lib/
/modules/
/.vscode/
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
"enzyme-to-json": "^3.3.4",
"gulp": "3.9.1",
"gulp-typescript": "3",
"jest": "22.1.2",
"jest-environment-jsdom": "^22.1.4",
"jest-environment-jsdom-global": "^1.0.3",
"jest": "^23.6.0",
"jest-environment-jsdom": "^23.4.0",
"jest-environment-jsdom-global": "^1.1.0",
"mocha": "5.0.0",
"git-cz": "^1.7.0",
"react-markdown": "3.1.4",
Expand Down
15 changes: 0 additions & 15 deletions src/LocalStorage/__tests__/__snapshots__/index.test.tsx.snap

This file was deleted.

55 changes: 25 additions & 30 deletions src/LocalStorage/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
import {createElement as h} from 'react';
import {mount} from 'enzyme';
import {LocalStorage} from '..';
import {get, set, del} from '../local-storage';

const glob = global as any;
jest.mock('../local-storage');

const promisify = (assertions, delay = 20) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
assertions();
resolve();
} catch (error) {
reject(error);
}
}, delay);
});
};
const getSpy = get as any as jest.SpyInstance;
const setSpy = set as any as jest.SpyInstance;
const delSpy = del as any as jest.SpyInstance;
const sleep = t => new Promise(r => setTimeout(r, t));

describe('<LocalStorage>', () => {
beforeEach(() => {
glob.localStorage = {};
getSpy.mockClear();
setSpy.mockClear();
delSpy.mockClear();
});

it('is a component', () => {
expect(LocalStorage).toBeInstanceOf(Function);
});

it('puts value to localStorage', () => {
it('puts value to localStorage', async () => {
mount(<LocalStorage name='foo' data='bar' debounce={10} />);
await sleep(20);

return promisify(() => {
expect(glob.localStorage).toMatchSnapshot();
});
expect(set).toHaveBeenCalledTimes(1);
expect(set).toHaveBeenCalledWith('foo', '"bar"');
});

it('serializes an object', () => {
it('serializes an object', async () => {
mount(<LocalStorage name='foo' data={{key: [1, 2, 3]}} debounce={10} />);
await sleep(20);

return promisify(() => {
expect(glob.localStorage).toMatchSnapshot();
});
expect(set).toHaveBeenCalledTimes(1);
expect(set).toHaveBeenCalledWith('foo', '{"key":[1,2,3]}');
});

it('updates on re-render', () => {
it('updates on re-render', async () => {
const wrapper = mount(<LocalStorage name='foo' data={1} debounce={10} />);

wrapper.setProps({
Expand All @@ -51,17 +46,17 @@ describe('<LocalStorage>', () => {
});

wrapper.update();
await sleep(20);

const lastSetArgs = setSpy.mock.calls[setSpy.mock.calls.length - 1];

return promisify(() => {
expect(glob.localStorage.foo).toBe('2');
}, 50);
expect(lastSetArgs).toEqual(['foo', '2']);
});

it('does NOT set initial data when onMount prop is set', () => {
it('does NOT set initial data when onMount prop is set', async () => {
mount(<LocalStorage name='foo' data={1} debounce={10} onMount={() => {}} />);
await sleep(20);

return promisify(() => {
expect(glob.localStorage).toMatchSnapshot();
}, 20);
expect(set).not.toHaveBeenCalled();
});
});
7 changes: 4 additions & 3 deletions src/LocalStorage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Component} from 'react';
import {get, set, del} from './local-storage';
const debounce = require('throttle-debounce/debounce');

export interface ILocalStorageProps {
Expand All @@ -21,7 +22,7 @@ export class LocalStorage extends Component<ILocalStorageProps, any> {
let json = '';

try {
json = localStorage[name];
json = get(name);
} catch {
json = 'null';
}
Expand Down Expand Up @@ -66,14 +67,14 @@ export class LocalStorage extends Component<ILocalStorageProps, any> {
rawData = JSON.stringify(data);
}

localStorage[String(name)] = rawData;
set(name, rawData);
} catch {}
});

remove (name = this.props.name) {
if (!this.props.persist) {
try {
delete localStorage[name];
del(name);
} catch {}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/LocalStorage/local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const get = (key: string) => localStorage[key];
export const set = (key: string, value: string) => localStorage[key] = value;
export const del = (key: string) => {
delete localStorage[key];
};
Loading

1 comment on commit 9a1dd99

@streamich
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build version: 2.0.1-feat-auth.63 🀞 feat-auth on Travis πŸŽ‰

Please sign in to comment.