Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Fix aliasing with optional tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisgl authored and fusion-bot[bot] committed Nov 2, 2018
1 parent 262aa5f commit 2d9610b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
83 changes: 83 additions & 0 deletions src/__tests__/dependency-resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,89 @@ tape('dependency registration with aliases', t => {
t.end();
});

tape('optional dependency registration with aliases', t => {
const app = new App('el', el => el);
t.ok(app, 'creates an app');
const counters = {
a: 0,
b: 0,
c: 0,
d: 0,
};

const PluginA: FusionPlugin<void, AType> = createPlugin({
provides: () => {
counters.a++;
t.equal(counters.a, 1, 'only instantiates once');
return {
a: 'PluginA',
};
},
});
const PluginB: FusionPlugin<{a: Token<AType>}, BType> = createPlugin({
deps: {
a: TokenA,
},
provides: deps => {
counters.b++;
t.equal(deps.a.a, 'PluginA');
t.equal(counters.b, 1, 'only instantiates once');
return {
b: 'PluginB',
};
},
});

type PluginCType = FusionPlugin<
{a: typeof TokenA, b: typeof TokenB.optional},
CType
>;
const PluginC: PluginCType = createPlugin({
deps: {
a: TokenA,
b: TokenB.optional,
},
provides: deps => {
counters.c++;
t.equal(deps.a.a, 'PluginA');
t.equal(deps.b && deps.b.b, 'PluginD', 'uses correct alias');
t.equal(counters.c, 1, 'only instantiates once');
return {
c: 'PluginC',
};
},
});

const PluginD: FusionPlugin<{a: Token<AType>}, BType> = createPlugin({
deps: {
a: TokenA,
},
provides: deps => {
counters.d++;
t.equal(deps.a.a, 'PluginA');
t.equal(counters.d, 1, 'only instantiates once');
return {
b: 'PluginD',
};
},
});

app.register(TokenA, PluginA);
app.register(TokenB, PluginB);
app.register(TokenC, PluginC).alias(TokenB, TokenD);
app.register(TokenD, PluginD);
t.equal(counters.a, 0, 'does not instantiate until resolve is called');
t.equal(counters.b, 0, 'does not instantiate until resolve is called');
t.equal(counters.c, 0, 'does not instantiate until resolve is called');
t.equal(counters.d, 0, 'does not instantiate until resolve is called');
app.resolve();
t.equal(counters.a, 1, 'only instantiates once');
t.equal(counters.b, 1, 'only instantiates once');
t.equal(counters.c, 1, 'only instantiates once');
t.equal(counters.d, 1, 'only instantiates once');
t.end();
});

tape('dependency registration with aliasing non-plugins', t => {
const app = new App('el', el => el);
t.ok(app, 'creates an app');
Expand Down
6 changes: 3 additions & 3 deletions src/base-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class FusionApp {
const alias = (sourceToken: *, destToken: *) => {
this._dependedOn.add(getTokenRef(destToken));
if (aliases) {
aliases.set(sourceToken, destToken);
aliases.set(getTokenRef(sourceToken), destToken);
}
return {alias};
};
Expand Down Expand Up @@ -140,8 +140,8 @@ class FusionApp {
const appliedEnhancers = [];
const resolveToken = (token: Token<TResolved>, tokenAliases) => {
// Base: if we have already resolved the type, return it
if (tokenAliases && tokenAliases.has(token)) {
const newToken = tokenAliases.get(token);
if (tokenAliases && tokenAliases.has(getTokenRef(token))) {
const newToken = tokenAliases.get(getTokenRef(token));
if (newToken) {
token = newToken;
}
Expand Down

0 comments on commit 2d9610b

Please sign in to comment.