-
Notifications
You must be signed in to change notification settings - Fork 102
/
stampit-shortcuts.js
126 lines (95 loc) · 3.77 KB
/
stampit-shortcuts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const test = require('tape');
const _ = require('lodash');
const stampit = require('../src/stampit');
// stampit.methods, stampit.init, stampit.props, etc.
test('stampit.methods shortcut', (t) => {
const methods = {method1() {}};
const stamp1 = stampit({methods: methods});
const stamp2 = stampit.methods(methods);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('stampit.init shortcut', (t) => {
const init = () => {};
const stamp1 = stampit({init: init});
const stamp2 = stampit.init(init);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('stampit.composers shortcut', (t) => {
const composer = () => {};
const stamp1 = stampit({composers: composer});
const stamp2 = stampit.composers(composer);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('stampit.props shortcut', (t) => {
const props = {method1() {}};
const stamp1 = stampit({props: props});
const stamp2 = stampit.props(props);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('stampit.statics shortcut', (t) => {
const statics = {method1() {}};
const stamp1 = stampit({statics: statics});
const stamp2 = stampit.statics(statics);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('stampit.statics(arg1, arg2) shortcut', (t) => {
const stamp1 = stampit.statics({foo: 1}, {bar: '2'});
t.ok(stamp1.foo);
t.ok(stamp1.bar);
t.end();
});
test('stampit.propertyDescriptors shortcut', (t) => {
const propertyDescriptors = {x: {writable: true}};
const stamp1 = stampit({propertyDescriptors: propertyDescriptors});
const stamp2 = stampit.propertyDescriptors(propertyDescriptors);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('stampit.staticPropertyDescriptors shortcut', (t) => {
const staticPropertyDescriptors = {x: {writable: true}};
const stamp1 = stampit({staticPropertyDescriptors: staticPropertyDescriptors});
const stamp2 = stampit.staticPropertyDescriptors(staticPropertyDescriptors);
t.deepEqual(_.toPlainObject(stamp1.compose), _.toPlainObject(stamp2.compose));
t.end();
});
test('all shortcuts combined', (t) => {
const {compose, methods, init} = stampit;
const HasFoo = compose({
properties: {
foo: 'default foo!'
}
}).methods().properties().initializers().deepProperties()
.staticProperties().staticDeepProperties()
.configuration().deepConfiguration()
.propertyDescriptors().staticPropertyDescriptors()
.props().init().composers().deepProps()
.statics().deepStatics().conf().deepConf();
const PrintFoo = methods({
printFoo() {
// console.log(this.foo || 'There is no foo');
}
}).methods().properties().initializers().deepProperties()
.staticProperties().staticDeepProperties()
.configuration().deepConfiguration()
.propertyDescriptors().staticPropertyDescriptors()
.props().init().composers().deepProps()
.statics().deepStatics().conf().deepConf();
const Init = init(function ({foo}) {
this.foo = foo;
}).methods().properties().initializers().deepProperties()
.staticProperties().staticDeepProperties()
.configuration().deepConfiguration()
.propertyDescriptors().staticPropertyDescriptors()
.props().init().composers().deepProps()
.statics().deepStatics().conf().deepConf();
const Foo = compose(HasFoo, PrintFoo, Init);
t.equal(Foo.compose.properties.foo, 'default foo!', 'the method should exit');
t.ok(typeof Foo.compose.methods.printFoo === 'function', 'the method should exit');
t.equal(Foo.compose.initializers.length, 1, 'should be single initializer');
t.end();
});