Skip to content

Commit

Permalink
chore: refactor code quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
akshgpt7 committed Feb 24, 2021
1 parent f82501b commit 864ee31
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 36 deletions.
17 changes: 17 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version = 1

test_patterns = ["src/__tests__/**"]

exclude_patterns = ["dist/*.map"]

[[analyzers]]
name = "javascript"
enabled = true

[analyzers.meta]
environment = [
"nodejs",
"browser"
]
dialect = "typescript"
plugins = ["react"]
3 changes: 1 addition & 2 deletions src/__tests__/Array.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('array: should rerender used', async () => {
let renderTimes = 0
Expand Down Expand Up @@ -290,4 +289,4 @@ test('array: should not rerender unused self', async () => {
expect(result.current.get().length).toEqual(2);
expect(Object.keys(result.current)).toEqual(['0', '1']);
expect(Object.keys(result.current.get())).toEqual(['0', '1']);
});
});
3 changes: 1 addition & 2 deletions src/__tests__/Batch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, none, State, postpone } from '../';
import { useState, none, postpone } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('object: should rerender used via nested batch update', async () => {
let renderTimes = 0
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/Complex.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, createState, none } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('complex: should rerender used', async () => {
let renderTimes = 0
Expand Down Expand Up @@ -199,4 +198,4 @@ test('complex: should auto save latest state for unmounted', async () => {
expect(renderTimes).toStrictEqual(2);
expect(unmountedLink.field1.get()).toStrictEqual(2);
expect(result.current[0].get().field1).toStrictEqual(2);
});
});
3 changes: 1 addition & 2 deletions src/__tests__/Error.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';

test('error: should not allow set to another state value', async () => {
const state1 = renderHook(() => {
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/Merge.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, none } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('primitive: should rerender used after merge update', async () => {
let renderTimes = 0
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/Object.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, createState, none } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('object: should rerender used', async () => {
let renderTimes = 0
Expand Down
43 changes: 21 additions & 22 deletions src/__tests__/Plugin.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, createState, Plugin, DevToolsID, DevTools, DevToolsExtensions, PluginCallbacks } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

const TestPlugin = Symbol('TestPlugin')
const TestPluginUnknown = Symbol('TestPluginUnknown')
Expand Down Expand Up @@ -51,7 +50,7 @@ test('plugin: common flow callbacks', async () => {
result.current.set([{ f1: 0, f2: 'str2' }]);
});
expect(renderTimes).toStrictEqual(2);
expect(messages.slice(1)).toEqual(['onSet called, []: [{\"f1\":0,\"f2\":\"str2\"}], [{\"f1\":0,\"f2\":\"str\"}] => [{\"f1\":0,\"f2\":\"str2\"}], undefined'])
expect(messages.slice(1)).toEqual(['onSet called, []: [{"f1":0,"f2":"str2"}], [{"f1":0,"f2":"str"}] => [{"f1":0,"f2":"str2"}], undefined'])

expect(result.current.get()[0].f1).toStrictEqual(0);
expect(result.current.get()[0].f2).toStrictEqual('str2');
Expand All @@ -63,7 +62,7 @@ test('plugin: common flow callbacks', async () => {
result.current[0].f1.set(p => p + 1);
});
expect(renderTimes).toStrictEqual(3);
expect(messages.slice(2)).toEqual(['onSet called, [0,f1]: [{\"f1\":1,\"f2\":\"str2\"}], 0 => 1, undefined'])
expect(messages.slice(2)).toEqual(['onSet called, [0,f1]: [{"f1":1,"f2":"str2"}], 0 => 1, undefined'])

expect(result.current.get()[0].f1).toStrictEqual(1);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -74,7 +73,7 @@ test('plugin: common flow callbacks', async () => {
result.current[0].merge(p => ({ f1 : p.f1 + 1 }));
});
expect(renderTimes).toStrictEqual(4);
expect(messages.slice(3)).toEqual(['onSet called, [0]: [{\"f1\":2,\"f2\":\"str2\"}], {\"f1\":2,\"f2\":\"str2\"} => {\"f1\":2,\"f2\":\"str2\"}, {\"f1\":2}'])
expect(messages.slice(3)).toEqual(['onSet called, [0]: [{"f1":2,"f2":"str2"}], {"f1":2,"f2":"str2"} => {"f1":2,"f2":"str2"}, {"f1":2}'])

expect(result.current.get()[0].f1).toStrictEqual(2);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -87,7 +86,7 @@ test('plugin: common flow callbacks', async () => {
result.current.batch((s) => {
messages.push(`batch executed, state: ${JSON.stringify(s.get())}`)
}, 'custom context')
expect(messages.slice(5)).toEqual(['onBatchStart called, []: [{\"f1\":2,\"f2\":\"str2\"}], context: \"custom context\"', 'batch executed, state: [{\"f1\":2,\"f2\":\"str2\"}]', 'onBatchFinish called, []: [{\"f1\":2,\"f2\":\"str2\"}], context: \"custom context\"'])
expect(messages.slice(5)).toEqual(['onBatchStart called, []: [{"f1":2,"f2":"str2"}], context: "custom context"', 'batch executed, state: [{"f1":2,"f2":"str2"}]', 'onBatchFinish called, []: [{"f1":2,"f2":"str2"}], context: "custom context"'])
expect(result.current.attach(TestPluginUnknown)[0] instanceof Error).toEqual(true)

expect(result.current.get()[0].f1).toStrictEqual(2);
Expand All @@ -98,7 +97,7 @@ test('plugin: common flow callbacks', async () => {
controls.setUntracked([{ f1: 0, f2: 'str3' }])
})
expect(renderTimes).toStrictEqual(4);
expect(messages.slice(8)).toEqual(['onSet called, []: [{\"f1\":0,\"f2\":\"str3\"}], [{\"f1\":2,\"f2\":\"str2\"}] => [{\"f1\":0,\"f2\":\"str3\"}], undefined']);
expect(messages.slice(8)).toEqual(['onSet called, []: [{"f1":0,"f2":"str3"}], [{"f1":2,"f2":"str2"}] => [{"f1":0,"f2":"str3"}], undefined']);

expect(result.current.get()[0].f1).toStrictEqual(0);
expect(result.current.get()[0].f2).toStrictEqual('str3');
Expand Down Expand Up @@ -152,7 +151,7 @@ test('plugin: common flow callbacks', async () => {
expect(messages.slice(10)).toEqual([]);

unmount()
expect(messages.slice(10)).toEqual(['onDestroy called, [{\"f1\":0,\"f2\":\"str3str2\"}]'])
expect(messages.slice(10)).toEqual(['onDestroy called, [{"f1":0,"f2":"str3str2"}]'])

expect(result.current.get()[0].f1).toStrictEqual(0);
expect(messages.slice(11)).toEqual([])
Expand Down Expand Up @@ -202,16 +201,16 @@ test('plugin: common flow callbacks global state', async () => {

expect(renderTimes).toStrictEqual(1);
expect(messages).toEqual(
['onInit called, initial: [{\"f1\":0,\"f2\":\"str\"}]'])
['onInit called, initial: [{"f1":0,"f2":"str"}]'])
expect(result.current[0].get().f1).toStrictEqual(0);
expect(messages).toEqual(
['onInit called, initial: [{\"f1\":0,\"f2\":\"str\"}]'])
['onInit called, initial: [{"f1":0,"f2":"str"}]'])

act(() => {
result.current[0].f1.set(p => p + 1);
});
expect(renderTimes).toStrictEqual(2);
expect(messages.slice(1)).toEqual(['onSet called, [0,f1]: [{\"f1\":1,\"f2\":\"str\"}], 0 => 1, undefined'])
expect(messages.slice(1)).toEqual(['onSet called, [0,f1]: [{"f1":1,"f2":"str"}], 0 => 1, undefined'])

expect(result.current.get()[0].f1).toStrictEqual(1);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -222,7 +221,7 @@ test('plugin: common flow callbacks global state', async () => {
result.current[0].merge(p => ({ f1 : p.f1 + 1 }));
});
expect(renderTimes).toStrictEqual(3);
expect(messages.slice(2)).toEqual(['onSet called, [0]: [{\"f1\":2,\"f2\":\"str\"}], {\"f1\":2,\"f2\":\"str\"} => {\"f1\":2,\"f2\":\"str\"}, {\"f1\":2}'])
expect(messages.slice(2)).toEqual(['onSet called, [0]: [{"f1":2,"f2":"str"}], {"f1":2,"f2":"str"} => {"f1":2,"f2":"str"}, {"f1":2}'])

expect(result.current.get()[0].f1).toStrictEqual(2);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -244,10 +243,10 @@ test('plugin: common flow callbacks global state', async () => {
result.current[0].f1.set(p => p + 1)
});
expect(renderTimes).toStrictEqual(3);
expect(messages.slice(4)).toEqual(['onSet called, [0,f1]: [{\"f1\":3,\"f2\":\"str\"}], 2 => 3, undefined'])
expect(messages.slice(4)).toEqual(['onSet called, [0,f1]: [{"f1":3,"f2":"str"}], 2 => 3, undefined'])

stateInf.destroy()
expect(messages.slice(5)).toEqual(['onDestroy called, [{\"f1\":3,\"f2\":\"str\"}]'])
expect(messages.slice(5)).toEqual(['onDestroy called, [{"f1":3,"f2":"str"}]'])

act(() => {
expect(() => result.current[0].f1.set(p => p + 1)).toThrow(
Expand Down Expand Up @@ -302,7 +301,7 @@ test('plugin: common flow callbacks devtools', async () => {
result.current[0].f1.set(p => p + 1);
});
expect(renderTimes).toStrictEqual(2);
expect(messages.slice(1)).toEqual(['LABELLED onSet called, [0,f1]: [{\"f1\":1,\"f2\":\"str\"}], 0 => 1, undefined'])
expect(messages.slice(1)).toEqual(['LABELLED onSet called, [0,f1]: [{"f1":1,"f2":"str"}], 0 => 1, undefined'])

expect(result.current.get()[0].f1).toStrictEqual(1);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -313,7 +312,7 @@ test('plugin: common flow callbacks devtools', async () => {
result.current[0].merge(p => ({ f1 : p.f1 + 1 }));
});
expect(renderTimes).toStrictEqual(3);
expect(messages.slice(2)).toEqual(['LABELLED onSet called, [0]: [{\"f1\":2,\"f2\":\"str\"}], {\"f1\":2,\"f2\":\"str\"} => {\"f1\":2,\"f2\":\"str\"}, {\"f1\":2}'])
expect(messages.slice(2)).toEqual(['LABELLED onSet called, [0]: [{"f1":2,"f2":"str"}], {"f1":2,"f2":"str"} => {"f1":2,"f2":"str"}, {"f1":2}'])

expect(result.current.get()[0].f1).toStrictEqual(2);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -326,7 +325,7 @@ test('plugin: common flow callbacks devtools', async () => {
expect(result.current.attach(TestPluginUnknown)[0] instanceof Error).toEqual(true)

unmount()
expect(messages.slice(4)).toEqual(['LABELLED onDestroy called, [{\"f1\":2,\"f2\":\"str\"}]'])
expect(messages.slice(4)).toEqual(['LABELLED onDestroy called, [{"f1":2,"f2":"str"}]'])

expect(result.current.get()[0].f1).toStrictEqual(2);
expect(messages.slice(5)).toEqual([])
Expand Down Expand Up @@ -381,16 +380,16 @@ test('plugin: common flow callbacks global state devtools', async () => {
});
expect(renderTimes).toStrictEqual(1);
expect(messages).toEqual(
['undefined onInit called, initial: [{\"f1\":0,\"f2\":\"str\"}]'])
['undefined onInit called, initial: [{"f1":0,"f2":"str"}]'])
expect(result.current[0].get().f1).toStrictEqual(0);
expect(messages).toEqual(
['undefined onInit called, initial: [{\"f1\":0,\"f2\":\"str\"}]'])
['undefined onInit called, initial: [{"f1":0,"f2":"str"}]'])

act(() => {
result.current[0].f1.set(p => p + 1);
});
expect(renderTimes).toStrictEqual(2);
expect(messages.slice(1)).toEqual(['onSet called, [0,f1]: [{\"f1\":1,\"f2\":\"str\"}], 0 => 1, undefined'])
expect(messages.slice(1)).toEqual(['onSet called, [0,f1]: [{"f1":1,"f2":"str"}], 0 => 1, undefined'])

expect(result.current.get()[0].f1).toStrictEqual(1);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -401,7 +400,7 @@ test('plugin: common flow callbacks global state devtools', async () => {
result.current[0].merge(p => ({ f1 : p.f1 + 1 }));
});
expect(renderTimes).toStrictEqual(3);
expect(messages.slice(2)).toEqual(['onSet called, [0]: [{\"f1\":2,\"f2\":\"str\"}], {\"f1\":2,\"f2\":\"str\"} => {\"f1\":2,\"f2\":\"str\"}, {\"f1\":2}'])
expect(messages.slice(2)).toEqual(['onSet called, [0]: [{"f1":2,"f2":"str"}], {"f1":2,"f2":"str"} => {"f1":2,"f2":"str"}, {"f1":2}'])

expect(result.current.get()[0].f1).toStrictEqual(2);
expect(Object.keys(result.current[0])).toEqual(['f1', 'f2']);
Expand All @@ -427,10 +426,10 @@ test('plugin: common flow callbacks global state devtools', async () => {
result.current[0].f1.set(p => p + 1)
});
expect(renderTimes).toStrictEqual(3);
expect(messages.slice(5)).toEqual(['onSet called, [0,f1]: [{\"f1\":3,\"f2\":\"str\"}], 2 => 3, undefined'])
expect(messages.slice(5)).toEqual(['onSet called, [0,f1]: [{"f1":3,"f2":"str"}], 2 => 3, undefined'])

stateRef.destroy()
expect(messages.slice(6)).toEqual(['onDestroy called, [{\"f1\":3,\"f2\":\"str\"}]'])
expect(messages.slice(6)).toEqual(['onDestroy called, [{"f1":3,"f2":"str"}]'])

act(() => {
expect(() => result.current[0].f1.set(p => p + 1)).toThrow(
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/Promised.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, createState, none } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('primitive: should rerender used on promise resolve', async () => {
let renderTimes = 0
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/Scoped.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, Downgraded } from '../';

import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';

test('object: should rerender used via scoped updates by child', async () => {
let parentRenderTimes = 0
Expand Down Expand Up @@ -216,4 +215,4 @@ test('object: should support late disabled tracking', async () => {
// // correct if parent is rerendered, child should not
// // as it is rerendered as a child of parent:
// expect(childRenderTimes).toStrictEqual(1);
});
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ const DestroyedEdition = -1
type Writeable<T> = { -readonly [P in keyof T]: T[P] };

class Store implements Subscribable {
private _edition: number = 0;
private _edition = 0;

private _subscribers: Set<Subscriber> = new Set();
private _setSubscribers: Set<Required<PluginCallbacks>['onSet']> = new Set();
Expand Down

0 comments on commit 864ee31

Please sign in to comment.