Skip to content

Commit a92e991

Browse files
committed
test(reactant-di): add unexpected multi-inject testing
1 parent b2fa781 commit a92e991

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`multiInject Unexpected multi-Inject 1`] = `"Ambiguous match found for serviceIdentifier: Foo"`;

packages/reactant-di/test/decorators/multiInject.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,25 @@ describe('multiInject', () => {
5454
expect(bar.length).toBe(2);
5555
expect(bar.foos[1]).toBe('test');
5656
});
57+
58+
test('Unexpected multi-Inject', () => {
59+
@injectable()
60+
class Foo {
61+
public get test() {
62+
return 'test';
63+
}
64+
}
65+
66+
@injectable()
67+
class Bar {
68+
constructor(public foos: Foo) {}
69+
}
70+
71+
expect(() => {
72+
createContainer({
73+
ServiceIdentifiers: new Map(),
74+
modules: [Foo, Foo],
75+
}).get(Bar);
76+
}).toThrowErrorMatchingSnapshot();
77+
});
5778
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Unexpected multi-inject: module with multiple module injection with same module or others 1`] = `
4+
"Ambiguous match found for serviceIdentifier: FooIdentifier
5+
Registered bindings:
6+
Foo
7+
Foo
8+
FooTest"
9+
`;

packages/reactant-module/test/index.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,56 @@ test('module with multiple module injection with same module or others', () => {
265265
expect(fooBar.foos[0].num).toBe(3);
266266
expect(computedFn.mock.calls.length).toBe(3);
267267
});
268+
269+
test('Unexpected multi-inject: module with multiple module injection with same module or others', () => {
270+
const computedFn = jest.fn();
271+
@injectable({
272+
name: 'foo',
273+
})
274+
class Foo {
275+
@state
276+
count = 1;
277+
278+
@action
279+
increase() {
280+
this.count += 1;
281+
}
282+
283+
@computed(({ count }: Foo) => [count])
284+
get num() {
285+
computedFn();
286+
return this.count + 1;
287+
}
288+
}
289+
290+
@injectable({
291+
name: 'fooTest',
292+
})
293+
class FooTest {
294+
@state
295+
count = 1;
296+
}
297+
298+
@injectable()
299+
class FooBar {
300+
constructor(@inject('FooIdentifier') public foos: Foo, public foo: Foo) {}
301+
}
302+
303+
const ServiceIdentifiers = new Map();
304+
const modules = [
305+
FooBar,
306+
{ provide: 'FooIdentifier', useClass: Foo },
307+
{ provide: 'FooIdentifier', useClass: Foo },
308+
{ provide: 'FooIdentifier', useClass: FooTest },
309+
];
310+
const container = createContainer({
311+
ServiceIdentifiers,
312+
modules,
313+
options: {
314+
defaultScope: 'Singleton',
315+
},
316+
});
317+
expect(() => {
318+
container.get(FooBar);
319+
}).toThrowErrorMatchingSnapshot();
320+
});

0 commit comments

Comments
 (0)