From 5cfb4e5edf904e22a63ae16b5f105ac028cb1896 Mon Sep 17 00:00:00 2001 From: Gaurav Munjal Date: Tue, 26 Nov 2024 16:09:35 -0500 Subject: [PATCH] get it working --- addon/configuration.js | 73 +++++++++++++++----------------- tests/unit/configuration-test.js | 42 ++++++++++++------ 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/addon/configuration.js b/addon/configuration.js index 05ec86c..2bfd1ba 100644 --- a/addon/configuration.js +++ b/addon/configuration.js @@ -11,34 +11,46 @@ const getValue = function (configKey) { return get(configuration, propertyPath); }; +function configurableValue(configKey, defaultValue, originalGetter) { + const value = getValue.call(this, configKey); + if (typeof value === 'undefined') { + if (typeof defaultValue !== 'undefined') { + if (typeof defaultValue === 'function') { + return defaultValue.call(this); + } else { + return defaultValue; + } + } else if (typeof originalGetter === 'function') { + return originalGetter.call(this); + } else { + throw new Error( + 'Expected configuration value ' + + configKey + + ' to be defined for provider named ' + + this.get('name') + ); + } + } + return value; +} + function configurable(configKey, defaultValue) { - // want to somehow determine if should return legacy or new decorator - return configurableLegacy(configKey, defaultValue); + const legacy = configurableLegacy(configKey, defaultValue); + const modern = configurableDecorator(configKey, defaultValue); + Object.assign(legacy, modern); + return legacy; } function configurableDecorator(configKey, defaultValue) { return function (_target, _key, descriptor) { const originalGetter = descriptor.get; const getter = function () { - const value = getValue.call(this, configKey); - - if (typeof value === 'undefined') { - if (typeof defaultValue !== 'undefined') { - if (typeof defaultValue === 'function') { - return defaultValue.call(this); - } else { - return defaultValue; - } - } else if (typeof originalGetter === 'function') { - return originalGetter.call(this); - } else { - throw new Error( - `Expected configuration value ${configKey} to be defined for provider named ${this.name}` - ); - } - } - - return value; + return configurableValue.call( + this, + configKey, + defaultValue, + originalGetter + ); }; return { @@ -51,24 +63,7 @@ function configurableDecorator(configKey, defaultValue) { function configurableLegacy(configKey, defaultValue) { return computed(function configurableComputed() { - const value = getValue.call(this, configKey); - if (typeof value === 'undefined') { - if (typeof defaultValue !== 'undefined') { - if (typeof defaultValue === 'function') { - return defaultValue.call(this); - } else { - return defaultValue; - } - } else { - throw new Error( - 'Expected configuration value ' + - configKey + - ' to be defined for provider named ' + - this.get('name') - ); - } - } - return value; + return configurableValue.call(this, configKey, defaultValue); }); } diff --git a/tests/unit/configuration-test.js b/tests/unit/configuration-test.js index 30a878c..c756eaa 100644 --- a/tests/unit/configuration-test.js +++ b/tests/unit/configuration-test.js @@ -1,17 +1,14 @@ /* eslint-disable ember/no-classic-classes, ember/no-get */ import EmberObject from '@ember/object'; import { run } from '@ember/runloop'; -import { - configurable, - configurableDecorator, - configure, -} from 'torii/configuration'; +import { configurable, configure } from 'torii/configuration'; import { module, test } from 'qunit'; module('Unit | Configuration', function (hooks) { let testable1, testable2, testables; const Testable = EmberObject.extend({ + type: 'Testable', name: 'test', required: configurable('apiKey'), defaulted: configurable('scope', 'email'), @@ -22,13 +19,14 @@ module('Unit | Configuration', function (hooks) { }); const Testable2 = class extends EmberObject { + type = 'Testable2'; name = 'test'; - @configurableDecorator('apiKey') required; - @configurableDecorator('scope', 'email') defaulted; + @configurable('apiKey') required; + @configurable('scope', 'email') defaulted; defaultedFunctionValue = 'found-via-get'; // not supported by @computed due to limitations in decorators - @configurableDecorator('redirectUri') + @configurable('redirectUri') get defaultedFunction() { return this.get('defaultedFunctionValue'); } @@ -66,8 +64,8 @@ module('Unit | Configuration', function (hooks) { message = e.message; } - assert.ok(threw, 'read threw'); - assert.ok( + assert.true(threw, `${testable.type} read threw`); + assert.true( /Expected configuration value "?apiKey"? to be defined for provider named "?test"?/.test( message ), @@ -91,7 +89,11 @@ module('Unit | Configuration', function (hooks) { const value = testable.get('required'); - assert.equal(value, 'item val'); + assert.equal( + value, + 'item val', + `${testable.type}.get('required') should equal 'item val'` + ); } ); @@ -108,7 +110,11 @@ module('Unit | Configuration', function (hooks) { const value = testable.get('defaulted'); - assert.equal(value, 'email'); + assert.equal( + value, + 'email', + `${testable.type}.get('defaulted') should equal 'email'` + ); } ); @@ -127,7 +133,11 @@ module('Unit | Configuration', function (hooks) { const value = testable.get('defaulted'); - assert.equal(value, 'baz'); + assert.equal( + value, + 'baz', + `${testable.type}.get('defaulted') should equal 'baz'` + ); } ); @@ -138,7 +148,11 @@ module('Unit | Configuration', function (hooks) { let testable = testables[testableToUse]; const value = testable.get('defaultedFunction'); - assert.equal(value, 'found-via-get'); + assert.equal( + value, + 'found-via-get', + `${testable.type}.get('defaultedFunction') should equal 'found-via-get'` + ); } ); });