Permalink
Browse files

test: πŸ’ add yarn.lock, refresh Jest deps, fix localStorage test

  • Loading branch information...
streamich committed Sep 30, 2018
1 parent c4b149d commit 9a1dd9988e82949d8baafd06406b16ddad7da0d1
Showing with 12,146 additions and 52 deletions.
  1. +0 βˆ’1 .gitignore
  2. +3 βˆ’3 package.json
  3. +0 βˆ’15 src/LocalStorage/__tests__/__snapshots__/index.test.tsx.snap
  4. +25 βˆ’30 src/LocalStorage/__tests__/index.test.tsx
  5. +4 βˆ’3 src/LocalStorage/index.ts
  6. +5 βˆ’0 src/LocalStorage/local-storage.ts
  7. +12,109 βˆ’0 yarn.lock
View
@@ -4,7 +4,6 @@ node_modules/
/.nyc_output/
/coverage/
package-lock.json
yarn.lock
/lib/
/modules/
/.vscode/
View
@@ -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",

This file was deleted.

Oops, something went wrong.
@@ -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({
@@ -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();
});
});
@@ -1,4 +1,5 @@
import {Component} from 'react';
import {get, set, del} from './local-storage';
const debounce = require('throttle-debounce/debounce');
export interface ILocalStorageProps {
@@ -21,7 +22,7 @@ export class LocalStorage extends Component<ILocalStorageProps, any> {
let json = '';
try {
json = localStorage[name];
json = get(name);
} catch {
json = 'null';
}
@@ -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 {}
}
}
@@ -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];
};
Oops, something went wrong.

1 comment on commit 9a1dd99

@streamich

This comment has been minimized.

Show comment
Hide comment
@streamich

streamich Sep 30, 2018

Owner

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

Owner

streamich commented on 9a1dd99 Sep 30, 2018

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

Please sign in to comment.