Skip to content

Commit

Permalink
[Misc] LogicMounter helper - fix typing, perf
Browse files Browse the repository at this point in the history
- Use Kea's types instead of trying to rewrite my own LogicFile
- Add an early return for tests that pass `{}` to values as well for performance
  • Loading branch information
cee-chen committed Apr 14, 2021
1 parent 909cf4b commit e628d4d
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,10 @@ export const setMockActions = (actions: object) => {
* unmount();
* });
*/
import { resetContext, Logic, LogicInput } from 'kea';
import { resetContext, LogicWrapper } from 'kea';

type LogicFile = LogicWrapper<any>;

interface LogicFile {
inputs: Array<LogicInput<Logic>>;
build(props?: object): void;
mount(): Function;
}
export class LogicMounter {
private logicFile: LogicFile;
private unmountFn!: Function;
Expand All @@ -101,7 +98,7 @@ export class LogicMounter {

// Reset context with optional default value overrides
public resetContext = (values?: object, props?: object) => {
if (!values) {
if (!values || !Object.keys(values).length) {
resetContext({});
} else {
let { path, key } = this.logicFile.inputs[0];
Expand Down Expand Up @@ -161,7 +158,7 @@ export class LogicMounter {
const { listeners } = this.logicFile.inputs[0];

return typeof listeners === 'function'
? (listeners as Function)(listenersArgs) // e.g., listeners({ values, actions, props }) => ({ ... })
? listeners(listenersArgs) // e.g., listeners({ values, actions, props }) => ({ ... })
: listeners; // handles simpler logic files that just define listeners: { ... }
};
}

0 comments on commit e628d4d

Please sign in to comment.