Skip to content

Commit ebfebce

Browse files
committed
🔮 fix tests, update types
1 parent 027a326 commit ebfebce

5 files changed

Lines changed: 46 additions & 56 deletions

File tree

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
"vitest": "^1.1.0"
2828
},
2929
"dependencies": {
30-
"@universal-di/core": "^1.0.3",
31-
"react": "^18.2.0"
30+
"@universal-di/core": "^1.0.3"
31+
},
32+
"peerDependencies": {
33+
"react": "^18.0.0 || ^19.0.0"
3234
},
3335
"bugs": {
3436
"url": "https://github.com/universal-di/react/issues"

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/di-context/di-context.spec.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
import {render} from '@testing-library/react';
2-
import React from 'react';
3-
import {Injector} from '@universal-di/core';
4-
import {DIContext} from './di-context';
5-
import {describe, expect, it} from 'vitest';
6-
7-
describe('DIContext', () => {
8-
it('can access null injector', () => {
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
import { DIContext } from "./di-context";
4+
import { describe, expect, it } from "vitest";
5+
import { DIContainer } from "@universal-di/core/dist/src/di-container/di-container.js";
6+
import { useContext } from "react";
7+
8+
describe("DIContext", () => {
9+
it("can access null injector", () => {
910
const UIComponent = () => {
10-
const {injector} = React.useContext(DIContext);
11+
const { injector } = useContext(DIContext);
1112

12-
expect(injector).toBe(undefined);
13+
expect(injector).toBeNull();
1314

1415
return null;
1516
};
1617

1718
render(
18-
<DIContext.Provider value={{injector: undefined}}>
19-
<UIComponent/>
20-
</DIContext.Provider>,
19+
<DIContext.Provider value={{ injector: null }}>
20+
<UIComponent />
21+
</DIContext.Provider>
2122
);
2223
});
2324

24-
it('can access non-null injector', () => {
25-
const injectorStub = new Injector();
25+
it("can access non-null injector", () => {
26+
const injectorStub = new DIContainer();
2627

2728
const UIComponent = () => {
28-
const {injector} = React.useContext(DIContext);
29+
const { injector } = useContext(DIContext);
2930

3031
expect(injector).toBe(injectorStub);
3132

3233
return null;
3334
};
3435

3536
render(
36-
<DIContext.Provider value={{injector: injectorStub}}>
37-
<UIComponent/>
38-
</DIContext.Provider>,
37+
<DIContext.Provider value={{ injector: injectorStub }}>
38+
<UIComponent />
39+
</DIContext.Provider>
3940
);
4041
});
4142
});

src/di-context/di-context.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import {createContext} from 'react';
2-
import {Injector} from '@universal-di/core';
3-
import {Optional} from "../types";
1+
import { createContext } from "react";
2+
import { Optional } from "../types.js";
3+
import { DIContainer } from "@universal-di/core/dist/src/di-container/di-container.js";
44

5-
export const DIContext = createContext<{ injector: Optional<Injector> }>({injector: undefined});
5+
export const DIContext = createContext<{ injector: Optional<DIContainer> }>({
6+
injector: null,
7+
});
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import "@testing-library/jest-dom";
22
import { render, screen } from "@testing-library/react";
3-
import React from "react";
4-
import { DIContextProvider } from "../di-context/di-context-provider";
5-
import { useInjection } from "./useInjection";
3+
import { InjectionToken, InjectorStub } from "@universal-di/core";
4+
import { DIContainer } from "@universal-di/core/dist/src/di-container/di-container.js";
65
import {
76
afterEach,
87
beforeEach,
@@ -12,32 +11,19 @@ import {
1211
MockInstance,
1312
vi,
1413
} from "vitest";
15-
import { InjectionToken, Injector } from "@universal-di/core";
16-
17-
class TestInjector {
18-
private readonly value: any;
19-
20-
constructor(value: any) {
21-
this.value = value;
22-
}
23-
24-
get() {
25-
return this.value;
26-
}
27-
}
28-
29-
export const InjectorStub = TestInjector;
14+
import { DIContextProvider } from "../di-context/di-context-provider";
15+
import { useInjection } from "./useInjection";
3016

3117
describe("useInjection", () => {
32-
const tokenStub = new InjectionToken<string>("TOKEN_STUB");
18+
const tokenStub = new InjectionToken("TOKEN_STUB");
3319
const valueStub = "valueStub";
3420
const ComponentStub = () => {
35-
const injectedValue: string = useInjection(tokenStub);
21+
const injectedValue = useInjection(tokenStub);
3622

37-
return <div>{injectedValue}</div>;
23+
return <div>{injectedValue as string}</div>;
3824
};
3925

40-
let injector: Injector;
26+
let injector: DIContainer;
4127
let consoleErrorSpy: MockInstance;
4228

4329
beforeEach(() => {
@@ -51,23 +37,22 @@ describe("useInjection", () => {
5137
});
5238

5339
it("renders children with provided injector value", () => {
54-
injector = new InjectorStub(valueStub) as any;
40+
injector = new InjectorStub(valueStub);
5541

5642
render(
5743
<DIContextProvider injector={injector}>
5844
<ComponentStub />
5945
</DIContextProvider>
6046
);
6147

62-
// @ts-ignore
6348
expect(screen.getByText(valueStub)).toBeInTheDocument();
6449
});
6550

6651
it("throws error for no context", () => {
67-
injector = new InjectorStub(valueStub) as any;
52+
injector = new InjectorStub(valueStub);
6853

6954
const renders = () => render(<ComponentStub />);
7055

71-
expect(renders).toThrowError("No DI context provided");
56+
expect(renders).toThrowError("No di context provided");
7257
});
7358
});

0 commit comments

Comments
 (0)