gell-dispatch 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,151 @@
1
+ const assert = require('assert');
2
+
3
+ const Registry = require('../registry');
4
+
5
+ const KIND = {
6
+ kind: 'query',
7
+ nameOf: d => d && d.name,
8
+ describe: d => ({ description: d.description }),
9
+ };
10
+
11
+ const ONE = { name: 'q.one', description: 'one' };
12
+ const TWO = { name: 'q.two', description: 'two' };
13
+
14
+ describe('registry', function() {
15
+ let registry;
16
+
17
+ beforeEach(function() {
18
+ registry = new Registry(KIND);
19
+ })
20
+
21
+ it('requires a kind with nameOf', function() {
22
+ assert.throws(() => new Registry(), { name: 'AssertionError' });
23
+ assert.throws(() => new Registry({ kind: 'query' }), { name: 'AssertionError' });
24
+ })
25
+
26
+ describe('register', function() {
27
+ it('registers and returns the original declaration', function() {
28
+ assert.strictEqual(registry.register(ONE), ONE);
29
+ assert.strictEqual(registry.get('q.one'), ONE);
30
+ assert.strictEqual(registry.has('q.one'), true);
31
+ })
32
+
33
+ it('returns undefined / false for unknown names', function() {
34
+ assert.strictEqual(registry.get('q.none'), undefined);
35
+ assert.strictEqual(registry.has('q.none'), false);
36
+ })
37
+
38
+ it('rejects a declaration that is not an object', function() {
39
+ assert.throws(() => registry.register(null), { name: 'AssertionError' });
40
+ assert.throws(() => registry.register('q.one'), { name: 'AssertionError' });
41
+ })
42
+
43
+ it('rejects a declaration without a name', function() {
44
+ assert.throws(() => registry.register({}), { name: 'AssertionError', message: 'query must define a name to be registered' });
45
+ })
46
+
47
+ it('rejects a duplicate name', function() {
48
+ registry.register(ONE);
49
+
50
+ const message = 'query (name=q.one) is already registered';
51
+
52
+ assert.throws(() => registry.register({ name: 'q.one' }), { name: 'AssertionError', message });
53
+ assert.throws(() => registry.register(ONE), { name: 'AssertionError', message });
54
+ })
55
+
56
+ it('validates with the kind', function() {
57
+ registry = new Registry({ ...KIND, validate: d => assert(d.description, 'description required') });
58
+
59
+ assert.throws(() => registry.register({ name: 'q.one' }), { message: 'description required' });
60
+ assert.strictEqual(registry.has('q.one'), false);
61
+ })
62
+ })
63
+
64
+ describe('names', function() {
65
+ it('lists names in registration order', function() {
66
+ registry.register(TWO);
67
+ registry.register(ONE);
68
+
69
+ assert.deepStrictEqual(registry.names(), ['q.two', 'q.one']);
70
+ })
71
+
72
+ it('returns a copy of names', function() {
73
+ registry.register(ONE);
74
+
75
+ registry.names().push('q.other');
76
+
77
+ assert.deepStrictEqual(registry.names(), ['q.one']);
78
+ })
79
+ })
80
+
81
+ describe('registerAll', function() {
82
+ it('registers every value of an object', function() {
83
+ const registered = registry.registerAll({ a: ONE, b: TWO });
84
+
85
+ assert.deepStrictEqual(registry.names(), ['q.one', 'q.two']);
86
+ assert.strictEqual(registered[0], ONE);
87
+ assert.strictEqual(registered[1], TWO);
88
+ })
89
+
90
+ it('registers every item of an iterable', function() {
91
+ registry.registerAll([ONE]);
92
+ registry.registerAll(new Set([TWO]));
93
+
94
+ assert.deepStrictEqual(registry.names(), ['q.one', 'q.two']);
95
+ })
96
+
97
+ it('registers nothing if a later declaration is invalid', function() {
98
+ assert.throws(() => registry.registerAll([ONE, {}]), { name: 'AssertionError' });
99
+
100
+ assert.strictEqual(registry.has('q.one'), false);
101
+ })
102
+
103
+ it('registers nothing if a batch repeats a name', function() {
104
+ assert.throws(() => registry.registerAll([ONE, { ...ONE }]), { name: 'AssertionError', message: 'query (name=q.one) is already registered' });
105
+
106
+ assert.deepStrictEqual(registry.names(), []);
107
+ })
108
+
109
+ it('registers nothing if a batch repeats a registered name', function() {
110
+ registry.register(ONE);
111
+
112
+ assert.throws(() => registry.registerAll([TWO, ONE]), { name: 'AssertionError' });
113
+
114
+ assert.deepStrictEqual(registry.names(), ['q.one']);
115
+ })
116
+
117
+ it('rejects null, undefined or a string', function() {
118
+ assert.throws(() => registry.registerAll(null), { name: 'AssertionError' });
119
+ assert.throws(() => registry.registerAll(undefined), { name: 'AssertionError' });
120
+ assert.throws(() => registry.registerAll('q.one'), { name: 'AssertionError' });
121
+ })
122
+ })
123
+
124
+ describe('describe', function() {
125
+ beforeEach(function() {
126
+ registry.registerAll([ONE, TWO]);
127
+ })
128
+
129
+ it('describes a declaration with its name', function() {
130
+ assert.deepStrictEqual(registry.describe('q.one'), { description: 'one', name: 'q.one' });
131
+ })
132
+
133
+ it('describes with only the name if the kind has no describe', function() {
134
+ registry = new Registry({ kind: 'query', nameOf: d => d.name });
135
+ registry.register(ONE);
136
+
137
+ assert.deepStrictEqual(registry.describe('q.one'), { name: 'q.one' });
138
+ })
139
+
140
+ it('returns undefined for unknown names', function() {
141
+ assert.strictEqual(registry.describe('q.none'), undefined);
142
+ })
143
+
144
+ it('describes all declarations in order', function() {
145
+ assert.deepStrictEqual(registry.describeAll(), [
146
+ { description: 'one', name: 'q.one' },
147
+ { description: 'two', name: 'q.two' },
148
+ ]);
149
+ })
150
+ })
151
+ })
@@ -0,0 +1,30 @@
1
+ const assert = require('assert');
2
+
3
+ const { normalizeParameters } = require('../../util/parameters');
4
+
5
+ describe('normalizeParameters', function() {
6
+ it('normalizes a type name', function() {
7
+ assert.deepStrictEqual(normalizeParameters({ parentId: 'uuid' }), [{ name: 'parentId', type: 'uuid', optional: false }]);
8
+ })
9
+
10
+ it('normalizes a spec object', function() {
11
+ assert.deepStrictEqual(normalizeParameters({ type: { type: 'string', optional: true } }), [{ name: 'type', type: 'string', optional: true }]);
12
+ })
13
+
14
+ it('defaults optional to false', function() {
15
+ assert.deepStrictEqual(normalizeParameters({ count: { type: 'number' } }), [{ name: 'count', type: 'number', optional: false }]);
16
+ })
17
+
18
+ it('keeps declaration order', function() {
19
+ assert.deepStrictEqual(normalizeParameters({ b: 'string', a: 'string' }).map(p => p.name), ['b', 'a']);
20
+ })
21
+
22
+ it('returns an empty array for missing parameters', function() {
23
+ assert.deepStrictEqual(normalizeParameters(), []);
24
+ assert.deepStrictEqual(normalizeParameters(null), []);
25
+ })
26
+
27
+ it('does not copy other spec attributes', function() {
28
+ assert.deepStrictEqual(normalizeParameters({ m: { type: 'string', maxLength: 10 } }), [{ name: 'm', type: 'string', optional: false }]);
29
+ })
30
+ })
@@ -0,0 +1,22 @@
1
+ const _ = require('lodash');
2
+
3
+ /**
4
+ * Normalizes parameter specs (e.g. trigger params, named query parameters) into a uniform description
5
+ * - a spec is either a type name ('uuid') or an object ({ type, optional })
6
+ * - other spec attributes (e.g. maxLength) are not included
7
+ *
8
+ * @param {*} params map of parameter name to spec
9
+ * @returns array of { name, type, optional }, in declaration order
10
+ */
11
+ function normalizeParameters(params) {
12
+ if (!params) return [];
13
+
14
+ return Object.keys(params).map(name => {
15
+ const spec = params[name];
16
+ const { type, optional=false } = _.isString(spec) ? { type: spec } : (spec || {});
17
+
18
+ return { name, type, optional: Boolean(optional) };
19
+ });
20
+ }
21
+
22
+ module.exports = { normalizeParameters };