Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

chore(infrastructure): Better error messages in verifyDefaultAdapter #3516

Merged
merged 12 commits into from
Sep 7, 2018
Prev Previous commit
Next Next commit
WIP: Revert toPlainObject()
  • Loading branch information
acdvorak committed Sep 5, 2018
commit fcec647b1e2ed8118e7c1282d664d60e74750c27
48 changes: 1 addition & 47 deletions test/unit/helpers/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import td from 'testdouble';
*/
export function verifyDefaultAdapter(FoundationClass, expectedMethodNames) {
const {defaultAdapter} = FoundationClass;
const plainObject = toPlainObject(defaultAdapter);
const adapterKeys = Object.keys(plainObject);
const adapterKeys = Object.keys(defaultAdapter);
const actualMethodNames = adapterKeys.filter((key) => typeof defaultAdapter[key] === 'function');

assert.equal(actualMethodNames.length, adapterKeys.length, 'Every adapter key must be a function');
Expand Down Expand Up @@ -83,51 +82,6 @@ export function captureHandlers(adapter, handlerCaptureMethodName) {
return handlers;
}

/**
* @param {!A} adapterInstance
* @return {!A}
* @template A
*/
function toPlainObject(adapterInstance) {
/**
* @param {!Object} obj
* @param {string} name
* @return {boolean}
*/
const hasMethod = (obj, name) => {
const desc = Object.getOwnPropertyDescriptor(obj, name);
return !!desc && typeof desc.value === 'function';
};

/**
* @param {!Object} obj
* @param {!Object.} stopPrototype
* @return {!Array<string>}
*/
const getInstanceMethodNames = (obj, stopPrototype = Object.prototype) => {
const array = [];
let curPrototype = Object.getPrototypeOf(obj);
while (curPrototype && curPrototype !== stopPrototype) {
Object.getOwnPropertyNames(curPrototype).forEach((name) => {
if (name !== 'constructor' && hasMethod(curPrototype, name)) {
array.push(name);
}
});
curPrototype = Object.getPrototypeOf(curPrototype);
}
return array;
};

const adapterMethods = {};
Object.getOwnPropertyNames(adapterInstance).forEach((name) => {
adapterMethods[name] = adapterInstance[name];
});
getInstanceMethodNames(adapterInstance).forEach((name) => {
adapterMethods[name] = adapterInstance[name];
});
return adapterMethods;
}

/**
* @param {!Array<string>} actualArray
* @param {!Array<string>} expectedArray
Expand Down