Skip to content

Commit 535ff3b

Browse files
Fix missing global types (#87)
1 parent 4ff8e7a commit 535ff3b

File tree

6 files changed

+5887
-5508
lines changed

6 files changed

+5887
-5508
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
node: [20]
1919

2020
steps:
21-
- uses: actions/checkout@v2
21+
- uses: actions/checkout@v4
2222
- name: Use Node.js ${{ matrix.node }}
2323
uses: actions/setup-node@v4
2424
with:

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ import { myApiFetcher } from '.';
7272
it('should call the API', async () => {
7373
// injectable() needs the original implementation as first argument
7474
// and the replacement implementation as second
75-
const fetchApiDi = injectable(fetchApi, jest.fn().mockResolvedValue({ data: 'mock' }));
75+
const fetchApiDi = injectable(
76+
fetchApi,
77+
jest.fn().mockResolvedValue({ data: 'mock' })
78+
);
7679

7780
const result = await runWithDi(() => myApiFetcher(), [fetchApiDi]);
7881

@@ -291,6 +294,7 @@ The rules are exported from `react-magnetic-di/eslint-plugin`. Unfortunately ESL
291294
- Does not replace default props (or default parameters in general): so dependencies provided as default parameters (eg `function MyComponent ({ modal = Modal }) { ... }`) will be ignored. If you accept the dependency as prop/argument you should inject it via prop/argument, as having a double injection strategy is just confusing.
292295
- Injecting primitive values (strings, booleans, numbers, ...) can be unreliable as we only have the actual value as reference, and so the library might not exactly know what to replace. In cases where multiple values might be replaced, a warning will be logged and we recommend you declare an inject a getter instead of the value itself.
293296
- Targeting only works on named functions/classes, so it won't work on anonymous scopes (eg `export default () => { ... }` or `memo(() => { ... })`)
297+
- If you define an injectable as `global` then you lose the ability to "scope" that injectable to a section of the tree, so the override will apply "globally". As a result, when defining multiple global replacements for the same dependency, only the last one evaluated will apply. So be mindful when using it in a multi DiProvider setting.
294298

295299
## FAQ
296300

@@ -303,7 +307,7 @@ import { debug } from 'react-magnetic-di';
303307
// ...
304308
console.log(debug(myApiFetcher));
305309
// It will print ['fetchApi']
306-
````
310+
```
307311

308312
One possible reason for it to happen is that the context has been lost. Typical occurrences are async or deeply nested functions (especially in React).
309313
The solution is setting the prop `global` on `DiProvider` (or the same injectable config) to better handle those scenarios (but refrain from abusing it).

0 commit comments

Comments
 (0)