Skip to content

Commit

Permalink
refactor(tests): add test for apply
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorstenSuckow committed Dec 2, 2022
1 parent a023a87 commit b78f8c7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/ioc/sencha/FactoryProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ Ext.define("coon.core.ioc.sencha.FactoryProxy", {
* Apply handler for proxying calls to Ext.Factory["proxy", "store", "controller", ...]
* factory methods are usually invoked with aliases instead of class names which are
* then resolved by Ext JS' underlying class system. The handler will utilize this
* functionality and assume the alias is available with argumentsList[0],
* functionality and assume the alias is available with the key "type" in argumentsList[0],
* inspect the "requireProperty" available with the resolved class and then
* resolve dependencies with the help of resolveDependencies(),
* if the arguments found in argumentsList[1] do not already contain
* if the object found in argumentsList[0] does not already contain
* configurations for these dependencies.
* The target is the Factory this handler operates on. The "aliasPrefix" of this argument
* will be used to determine the prefix used in conjunction with the "type" to resolve
* the proper class.
*
* @example
* // acme.Request.require = {requestor: "acme.RequestConfigurator"}
* // alias: "acme-request"
* proxy.apply({}, {}, ["acme-request", {}];
* proxy.apply({}, {}, [{type: "acme-request"}, {}];
* // config has no "requestor" configured with arguments, will resolve
* // dependencies using available bindings by calling resolveDependencies()
*
* proxy.apply({}, {}, ["acme-request", {requestor: {}];
* proxy.apply({}, {}, [{type: "acme-request"}, {requestor: {}];
* // config has "requestor" configured with arguments, will not resolve
* // dependencies
*
Expand All @@ -89,11 +92,14 @@ Ext.define("coon.core.ioc.sencha.FactoryProxy", {
const type = cfg.type ? cfg.type : (l8.isString(cfg) ? cfg : undefined);

if (type && target.instance?.aliasPrefix) {
const cls = Ext.ClassManager.getByAlias(`${target.instance.aliasPrefix}${type}`);
if (cls.require) {
const
cls = Ext.ClassManager.getByAlias(`${target.instance.aliasPrefix}${type}`),
requireCfg = cls?.[me.requireProperty];

if (requireCfg) {
cfg = Object.assign(
cfg,
me.resolveDependencies(cls.require, Ext.ClassManager.getName(cls))
me.resolveDependencies(Ext.ClassManager.getName(cls), requireCfg)
);
cfg.type = cfg.type || type;
argumentsList[0] = cfg;
Expand Down
65 changes: 64 additions & 1 deletion tests/src/ioc/sencha/FactoryProxyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,70 @@ StartTest(t => {

t.it("apply()", t => {

t.fail();

let defaultClass = {};

const
proxy = create({}),
getByAliasSpy = t.spyOn(Ext.ClassManager, "getByAlias").and.callFake(() => defaultClass),
getNameSpy = t.spyOn(Ext.ClassManager, "getName").and.callFake(() => "className"),
reflectSpy = t.spyOn(Reflect, "apply").and.callFake(() => "reflect"),
resolveDependenciesSpy = t.spyOn(proxy, "resolveDependencies").and.callFake(() => ({"prop": "resolved"})),
thisArg = {},
assertReflectSpy = (thirdArg) => {
t.expect(reflectSpy.calls.mostRecent().args[0]).toBe(target);
t.expect(reflectSpy.calls.mostRecent().args[1]).toBe(thisArg);

if (thirdArg !== undefined) {
t.expect(reflectSpy.calls.mostRecent().args[2]).toEqual(thirdArg);
}
};

let target = {};

// no "type" passed in object with 1st argument
let argsList = [{}];
t.expect(proxy.apply(target, thisArg, argsList)).toBe(reflectSpy.calls.mostRecent().returnValue);
t.expect(resolveDependenciesSpy.calls.count()).toBe(0);
assertReflectSpy(argsList);

// no "aliasPrefix" available
argsList = [{type: "foo"}];
t.expect(proxy.apply(target, thisArg, argsList)).toBe(reflectSpy.calls.mostRecent().returnValue);
t.expect(resolveDependenciesSpy.calls.count()).toBe(0);
assertReflectSpy(argsList);

// class alias cannot be resolved
target = {instance: {aliasPrefix: "bar"}};
defaultClass = undefined;
argsList = [{type: "foo"}];
t.expect(proxy.apply(target, thisArg, argsList)).toBe(reflectSpy.calls.mostRecent().returnValue);
t.expect(resolveDependenciesSpy.calls.count()).toBe(0);
assertReflectSpy();

// target class has no requireCfg
target = {instance: {aliasPrefix: "bar"}};
defaultClass = {};
argsList = [{type: "foo"}];
t.expect(proxy.apply(target, thisArg, argsList)).toBe(reflectSpy.calls.mostRecent().returnValue);
t.expect(resolveDependenciesSpy.calls.count()).toBe(0);
assertReflectSpy();

// target class requireCfg
const requireConfig = {"config": {}};
defaultClass = {};
defaultClass[proxy.requireProperty] = requireConfig;
argsList = [{type: "foo", width: 800, height: 600}];
t.expect(proxy.apply(target, thisArg, argsList)).toBe(reflectSpy.calls.mostRecent().returnValue);
t.expect(resolveDependenciesSpy.calls.count()).toBe(1);
t.expect(resolveDependenciesSpy.calls.mostRecent().args[0]).toBe(
getNameSpy.calls.mostRecent().returnValue
);
t.expect(resolveDependenciesSpy.calls.mostRecent().args[1]).toBe(requireConfig);
assertReflectSpy([{type: "foo", width: 800, height: 600, prop: "resolved"}]);

[reflectSpy, resolveDependenciesSpy, getNameSpy, getByAliasSpy].map(spy => spy.remove());

});


Expand Down

0 comments on commit b78f8c7

Please sign in to comment.