Skip to content

Commit

Permalink
Throw exception on undefined graph (#136)
Browse files Browse the repository at this point in the history
Throw if injectComponent was called with an undefined graph. This could happen due to circular dependencies that prevent the Graph from being resolved.

Co-authored-by: Guy Carmeli <>
  • Loading branch information
guyca authored Mar 30, 2024
1 parent 26b4405 commit 665ece5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/injectors/components/InjectComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from '@testing-library/react';
import React from 'react';
import type { Constructable, ObjectGraph } from 'src';
import MainGraph, { Dependencies } from '../../../test/fixtures/MainGraph';
import { injectComponent } from './InjectComponent';

Expand Down Expand Up @@ -33,4 +34,15 @@ describe('injectComponent', () => {
const { container } = render(<InjectedComponent />);
expect(container.textContent).toBe('error: own prop not provided - Fear kills progress');
});

// it throws an error if the Graph is undefined
it('Throws an error if the Graph is undefined', () => {
const Graph = undefined as unknown as Constructable<ObjectGraph>;
expect(() => injectComponent(component, Graph)).toThrowError(
`injectComponent was called with an undefined Graph.`
+ `This is probably not an issue with Obsidian.`
+ `It's typically caused by circular dependencies.`
+ ` Check the implementation of component.`,
);
});
});
30 changes: 22 additions & 8 deletions src/injectors/components/InjectComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ import { ObjectGraph } from '../../graph/ObjectGraph';
import { Constructable } from '../../types';
import ComponentInjector from './ComponentInjector';

interface Descriminator {
obsidianDescriminator: never;
interface Discriminator {
obsidianDiscriminator: never;
}

const componentInjector = new ComponentInjector();

export const injectComponent = <OwnProps = Descriminator, InjectedProps = Descriminator> (
export const injectComponent = <OwnProps = Discriminator, InjectedProps = Discriminator> (
Target: React.FunctionComponent<
(OwnProps extends infer P ? OwnProps extends Descriminator ? P : OwnProps : never) &
(InjectedProps extends Descriminator ? any : InjectedProps)
(OwnProps extends infer P ? OwnProps extends Discriminator ? P : OwnProps : never) &
(InjectedProps extends Discriminator ? any : InjectedProps)
>,
Graph: Constructable<ObjectGraph>,
) => componentInjector.inject(Target, Graph) as React.FunctionComponent<
InjectedProps extends Descriminator ?
OwnProps extends Descriminator ? Partial<OwnProps> : OwnProps :
) => {
assertGraph(Graph, Target);

return componentInjector.inject(Target, Graph) as React.FunctionComponent<
InjectedProps extends Discriminator ?
OwnProps extends Discriminator ? Partial<OwnProps> : OwnProps :
OwnProps extends InjectedProps ? Partial<OwnProps> : OwnProps & Partial<InjectedProps>
>;
};
function assertGraph(Graph: Constructable<ObjectGraph<unknown>>, Target: any) {
if (!Graph) {
throw new Error(
`injectComponent was called with an undefined Graph.`
+ `This is probably not an issue with Obsidian.`
+ `It's typically caused by circular dependencies.`
+ ` Check the implementation of ${Target.name}.`,
);
}
}

0 comments on commit 665ece5

Please sign in to comment.