Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception on undefined graph #136

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}.`,
);
}
}
Loading