Skip to content

Commit

Permalink
Merge pull request #338 from stampit-org/stamp-name
Browse files Browse the repository at this point in the history
Add another non-spec metadata - "name" to change factory name
  • Loading branch information
koresar committed Sep 16, 2018
2 parents 378a234 + bbbcef4 commit 338bb3f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/stampit.js
Expand Up @@ -157,17 +157,17 @@
var2 = descr[_deepStatics];
var4[_staticDeepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;

var4[_staticPropertyDescriptors] = descr[_staticPropertyDescriptors];
var1 = descr[_staticPropertyDescriptors];
var2 = descr.name && {name: {value: descr.name}};
var4[_staticPropertyDescriptors] = isObject(var2 || var1) ? assign({}, var1, var2) : _undefined;

var1 = descr[_configuration];
var2 = descr.conf;
var4[_configuration] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;

var1 = descr[_deepConfiguration];
var2 = descr[_deepConf];
var3 = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;

var4[_deepConfiguration] = var3;
var4[_deepConfiguration] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;

return var4;
}
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -14,6 +14,7 @@ import './import';
import './index';
import './infected-statics';
import './init';
import './name';
import './props';
import './stampit-api';
import './stampit-shortcuts';
52 changes: 52 additions & 0 deletions test/name.js
@@ -0,0 +1,52 @@
import test from 'tape';
import stampit from '../src/stampit';

try {
const f = Object.defineProperties(
() => {},
{ name: { value: 'test' } }
);

if (f.name === 'test') {
// Here we are sure that the environment supports function name change (ES6)

test('name metadata can be set', (t) => {
const stamp = stampit({
name: 'MyFactory'
});

t.equal(stamp.name, 'MyFactory',
'Should produce stamp with non-default name');

t.end();
});

test('name metadata is inherited', (t) => {
const stamp = stampit({
name: 'MyFactory'
});
const derived = stamp.compose({ staticPropertyDescriptors: { value: { x: 'whatever' } } });

t.equal(derived.name, 'MyFactory',
'Should inherit name from previous stamp');
t.equal(derived.compose.staticPropertyDescriptors.value.x, 'whatever',
'Should not loose other data');

t.end();
});

test('name metadata can be overwritten', (t) => {
const stamp = stampit({
name: 'MyFactory'
});
const derived = stamp.compose({ name: 'SecondOne' });

t.equal(derived.name, 'SecondOne',
'Should overwrite previous stamp name');

t.end();
});
}
} catch (e) {
// skip these tests in this environment
}

0 comments on commit 338bb3f

Please sign in to comment.