Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała authored and Mariusz Przodała committed Apr 5, 2023
1 parent d9a3e96 commit e106242
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"react/jsx-wrap-multilines": 2,
"react/no-string-refs": 0,
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"@typescript-eslint/no-unused-vars": "off",
"no-redeclare": "off",
"@typescript-eslint/no-redeclare": ["error"],
"jest/no-disabled-tests": "warn",
Expand All @@ -52,4 +52,4 @@
"globals": {
"JSX": true
}
}
}
16 changes: 8 additions & 8 deletions src/Eventrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ describe('Eventrix', () => {
const scopedInstance4 = scopedInstance3.create({ stateScope: 'scope4' });

expect(scopedInstance4.getStatePathWithScope()).toEqual('scope1.scope2.scope3.scope4');
expect(scopedInstance4.getParent().getStatePathWithScope()).toEqual('scope1.scope2.scope3');
expect(scopedInstance4.getParent().getParent().getStatePathWithScope()).toEqual('scope1.scope2');
expect(scopedInstance4.getParent().getParent().getParent().getStatePathWithScope()).toEqual('scope1');
expect(scopedInstance4.getParent().getParent().getParent().getParent().getStatePathWithScope()).toEqual(undefined);
expect(scopedInstance4.getParent()?.getStatePathWithScope()).toEqual('scope1.scope2.scope3');
expect(scopedInstance4.getParent()?.getParent()?.getStatePathWithScope()).toEqual('scope1.scope2');
expect(scopedInstance4.getParent()?.getParent()?.getParent()?.getStatePathWithScope()).toEqual('scope1');
expect(scopedInstance4.getParent()?.getParent()?.getParent()?.getParent()?.getStatePathWithScope()).toEqual(undefined);
expect(scopedInstance4.getFirstParent().getStatePathWithScope()).toEqual(undefined);
});
it('should get correct event name with scope', () => {
Expand All @@ -127,10 +127,10 @@ describe('Eventrix', () => {
const scopedInstance4 = scopedInstance3.create({ eventScope: 'scope4' });

expect(scopedInstance4.getEventNameWithScope('')).toEqual('scope1:scope2:scope3:scope4');
expect(scopedInstance4.getParent().getEventNameWithScope('')).toEqual('scope1:scope2:scope3');
expect(scopedInstance4.getParent().getParent().getEventNameWithScope('')).toEqual('scope1:scope2');
expect(scopedInstance4.getParent().getParent().getParent().getEventNameWithScope('')).toEqual('scope1');
expect(scopedInstance4.getParent().getParent().getParent().getParent().getEventNameWithScope('')).toEqual('');
expect(scopedInstance4.getParent()?.getEventNameWithScope('')).toEqual('scope1:scope2:scope3');
expect(scopedInstance4.getParent()?.getParent()?.getEventNameWithScope('')).toEqual('scope1:scope2');
expect(scopedInstance4.getParent()?.getParent()?.getParent()?.getEventNameWithScope('')).toEqual('scope1');
expect(scopedInstance4.getParent()?.getParent()?.getParent()?.getParent()?.getEventNameWithScope('')).toEqual('');
expect(scopedInstance4.getFirstParent().getEventNameWithScope('')).toEqual('');
});
});
10 changes: 5 additions & 5 deletions src/EventrixDebugger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import get from 'lodash/get';
import EventsReceiver from './EventsReceiver';
import { EventrixI, EventsReceiverI, StateManagerI } from './interfaces';
import { EventrixI, EventsListenerI, EventsReceiverI, ReceiverI, StateManagerI } from './interfaces';

interface DebuggerConfigI {
live?: boolean;
Expand Down Expand Up @@ -60,13 +60,13 @@ class EventrixDebugger {
}

getEventsReceiversCount(eventName: string) {
const receivers = get(this.eventrix, 'stateManager.receivers', {});
const receivers = get(this.eventrix, 'stateManager.receivers', {}) as { [key: string]: ReceiverI };
const receiversList = receivers[eventName];
return Array.isArray(receiversList) ? receiversList.length : 0;
}

getAllEventsReceiversCount() {
const receivers = get(this.eventrix, 'stateManager.receivers', {});
const receivers = get(this.eventrix, 'stateManager.receivers', {}) as { [key: string]: ReceiverI };
const eventsNames = Object.keys(receivers);
return eventsNames.map((eventName) => {
const receiversList = receivers[eventName];
Expand All @@ -78,13 +78,13 @@ class EventrixDebugger {
}

getEventListenersCount(eventName: string) {
const listeners = get(this.eventrix, 'stateManager.eventsEmitter.listeners', {});
const listeners = get(this.eventrix, 'stateManager.eventsEmitter.listeners', {}) as { [key: string]: EventsListenerI };
const listenersList = listeners[eventName];
return Array.isArray(listenersList) ? listenersList.length : 0;
}

getAllEventsListenersCount() {
const listeners = get(this.eventrix, 'stateManager.eventsEmitter.listeners', {});
const listeners = get(this.eventrix, 'stateManager.eventsEmitter.listeners', {}) as { [key: string]: EventsListenerI };
const eventsNames = Object.keys(listeners);
return eventsNames.map((eventName) => {
const listenersList = listeners[eventName];
Expand Down
2 changes: 2 additions & 0 deletions src/EventsEmitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('EventsEmitter', () => {

it('should not register listener if is not a function', () => {
console.warn = jest.fn();
// @ts-ignore
eventsEmitter.listen('testEvent', {});
expect(eventsEmitter.listeners.testEvent).toHaveLength(0);
expect(console.warn).toBeCalledWith('EventsEmitter->listen - "testEvent" listener is not a function');
Expand Down Expand Up @@ -49,6 +50,7 @@ describe('EventsEmitter', () => {

it('should not unregistered listener if event dont have registred listeners', () => {
console.warn = jest.fn();
// @ts-ignore
eventsEmitter.listen('testEvent2', {});
eventsEmitter.unlisten('testEvent2', mockListener);
expect(eventsEmitter.listeners.testEvent2).toHaveLength(0);
Expand Down
9 changes: 5 additions & 4 deletions src/StateManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import StateManager from './StateManager';
import EventsReceiver from './EventsReceiver';

describe('StateManager', () => {
let eventsEmitter = {
let eventsEmitter: any = {
listeners: {},
listen: jest.fn(),
unlisten: jest.fn(),
Expand All @@ -13,9 +13,9 @@ describe('StateManager', () => {
runListeners: jest.fn(),
};
let stateManager = new StateManager(eventsEmitter);
let initialState;
let initialState: object;
let eventsReceiver;
let eventsReceivers;
let eventsReceivers: any;

beforeEach(() => {
initialState = {
Expand Down Expand Up @@ -59,6 +59,7 @@ describe('StateManager', () => {
});
it('should not register events receiver when dont have handleEvent function', () => {
console.warn = jest.fn();
// @ts-ignore
stateManager.useReceiver({ getEventsNames: () => ['testEvent', 'secondTestEvent'], receiver: jest.fn(), eventsNames: [] });
expect(console.warn).toHaveBeenCalledWith(`Store->registerReceiver - "testEvent" receiver is not a function`);
expect(console.warn).toHaveBeenCalledWith(`Store->registerReceiver - "secondTestEvent" receiver is not a function`);
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('StateManager', () => {
const receiver = jest.fn(() => Promise.resolve({ asyncData: {} }));
const asyncReceiver = new EventsReceiver('testEvent', receiver);
stateManager.useReceiver(asyncReceiver);
return stateManager.runReceivers('testEvent', { eventData: {} }).then((results) => {
return stateManager.runReceivers('testEvent', { eventData: {} }).then((results: any) => {
expect(results).toEqual([{ asyncData: {} }, { testData: {} }]);
expect(receiver).toHaveBeenCalledWith('testEvent', { eventData: {} }, stateManager);
});
Expand Down
6 changes: 3 additions & 3 deletions src/decorators/fetchToState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('fetchToState', () => {
class FetchToStateTestClass {
eventrix: EventrixI;

constructor(props) {
constructor(props: any) {
this.eventrix = props.eventrix;
}

Expand All @@ -40,12 +40,12 @@ describe('fetchToState', () => {
class FetchToStateTestClass2 {
eventrix: EventrixI;

constructor(props) {
constructor(props: { eventrix: EventrixI }) {
this.eventrix = props.eventrix;
}

@fetchToState(EXTEND_LIST_EVENT_NAME, STATE_PATH)
extendList(eventData, state) {
extendList(eventData: any, state: { [x: string]: any }) {
return Promise.resolve([...state[STATE_PATH], 'test3']);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/decorators/receiver.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useEventrix from './useEventrix';
import receiver from './receiver';
import Eventrix from '../Eventrix';
import { EventrixI } from '../interfaces';
import { EventrixI, StateManagerI } from '../interfaces';

describe('receiver', () => {
const GET_LIST_EVENT_NAME = 'Test:loadList';
Expand All @@ -19,7 +19,7 @@ describe('receiver', () => {
}

@receiver(GET_LIST_EVENT_NAME)
getList(eventName, eventDate, stateManager) {
getList(eventName: string, eventDate: unknown, stateManager: StateManagerI) {
stateManager.setState(STATE_PATH, listResponse);
}
}
Expand All @@ -45,7 +45,7 @@ describe('receiver', () => {
}

@receiver(EXTEND_LIST_EVENT_NAME)
extendList(eventName, eventDate, stateManager) {
extendList(eventName: string, eventDate: any, stateManager: StateManagerI) {
const list = stateManager.getState(STATE_PATH);
stateManager.setState(STATE_PATH, [...list, 'test3']);
}
Expand Down
2 changes: 2 additions & 0 deletions src/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('helpers', () => {
};
const newValue = 'test';
setValue(state, 'a.b.c.d', newValue);
// @ts-ignore
expect(state.a.b.c.d).toEqual(newValue);
});
it('should fill object when object dont have property and end of path is array', () => {
Expand All @@ -114,6 +115,7 @@ describe('helpers', () => {
};
const newValue = 'test';
setValue(state, 'a.b.c.d.0', newValue);
// @ts-ignore
expect(state.a.b.c.d[0]).toEqual(newValue);
});
it('should create new reference for all element on path', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/persistStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const getStorageData = (storage: SyncStorage | AsyncStorage, storageKey: string)
return JSON.parse(storageData || defaultData).data;
};

const getStateKeys = <StateI>(storeState: StateI): StateKeysList<StateI> => {
const getStateKeys = <StateI extends {}>(storeState: StateI): StateKeysList<StateI> => {
return Object.keys(storeState) as StateKeysList<StateI>;
};

const connectPersistStore = <StateI>(eventrix: EventrixI, config: PersistStoreConfig<StateI>): void => {
const connectPersistStore = <StateI extends {}>(eventrix: EventrixI, config: PersistStoreConfig<StateI>): void => {
const {
blackList,
whiteList,
Expand Down

0 comments on commit e106242

Please sign in to comment.