Skip to content

Commit 27d43f4

Browse files
committed
feat!: Support for multiple events and events as entrypoint
tests: Adding tests for arguments and Event functions
1 parent e47ccf0 commit 27d43f4

File tree

6 files changed

+926
-37
lines changed

6 files changed

+926
-37
lines changed

src/Arguments.test.ts

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import { FormArguments } from './Arguments';
2+
import { FormArgumentError } from './Errors';
3+
4+
describe('Arguments.FormArguments', () => {
5+
it('Sets values', () => {
6+
const formArgs = new FormArguments();
7+
formArgs.set('abc', 'true');
8+
9+
expect(formArgs.getAll()).toEqual({
10+
abc: 'true',
11+
});
12+
});
13+
14+
it('Gets values', () => {
15+
const formArgs = new FormArguments();
16+
formArgs.set('abc', 'true');
17+
expect(formArgs.get('abc')).toEqual('true');
18+
});
19+
20+
it('Can set multiple values', () => {
21+
const formArgs = new FormArguments();
22+
formArgs.setAll({
23+
abc: 'true',
24+
cde: 44,
25+
hij: false,
26+
});
27+
28+
expect(formArgs.get('abc')).toEqual('true');
29+
expect(formArgs.get('cde')).toEqual(44);
30+
expect(formArgs.get('hij')).toEqual(false);
31+
});
32+
33+
it('Can resolve a string path', () => {
34+
const formArgs = new FormArguments();
35+
formArgs.setAll({
36+
simple: '123',
37+
first: {
38+
second: '456',
39+
},
40+
complex: {
41+
more: {
42+
stuff: {
43+
here: '789',
44+
},
45+
},
46+
branch: {
47+
here: 'foobar',
48+
},
49+
},
50+
});
51+
52+
expect(formArgs.resolvePath('simple')).toBe('123');
53+
expect(formArgs.resolvePath('first.second')).toBe('456');
54+
expect(formArgs.resolvePath('complex.more.stuff.here')).toBe('789');
55+
expect(formArgs.resolvePath('complex.branch.here')).toBe('foobar');
56+
57+
// or objects
58+
expect(formArgs.resolvePath('first')).toEqual({
59+
second: '456',
60+
});
61+
expect(formArgs.resolvePath('complex')).toEqual({
62+
more: {
63+
stuff: {
64+
here: '789',
65+
},
66+
},
67+
branch: {
68+
here: 'foobar',
69+
},
70+
});
71+
});
72+
73+
it('Throws if it can not resolve a path', () => {
74+
const formArgs = new FormArguments();
75+
formArgs.setAll({
76+
simple: '123',
77+
first: {
78+
second: '456',
79+
},
80+
complex: {
81+
more: {
82+
stuff: {
83+
here: '789',
84+
},
85+
},
86+
branch: {
87+
here: 'foobar',
88+
},
89+
},
90+
});
91+
92+
expect(() => formArgs.resolvePath('not.here')).toThrow(FormArgumentError);
93+
expect(() => formArgs.resolvePath('first.third')).toThrow(FormArgumentError);
94+
expect(() => formArgs.resolvePath('complex.more.typo')).toThrow(FormArgumentError);
95+
});
96+
97+
it('Can resolve template', () => {
98+
const formArgs = new FormArguments();
99+
formArgs.setAll({
100+
simple: '123',
101+
first: {
102+
second: '456',
103+
},
104+
complex: {
105+
more: {
106+
stuff: {
107+
here: '789',
108+
},
109+
},
110+
branch: {
111+
here: 'foobar',
112+
},
113+
},
114+
});
115+
116+
expect(formArgs.resolveTemplate('nothing to resolve')).toBe('nothing to resolve');
117+
expect(formArgs.resolveTemplate('hello {simple}')).toBe('hello 123');
118+
expect(formArgs.resolveTemplate('hello {simple} - how {first.second} and {complex.more.stuff.here}')).toBe(
119+
'hello 123 - how 456 and 789'
120+
);
121+
});
122+
123+
it('Throws if resolving a template with an invalid path', () => {
124+
const formArgs = new FormArguments();
125+
expect(() => formArgs.resolveTemplate('hello {bad}')).toThrow(FormArgumentError);
126+
});
127+
128+
it('Can normalize input by resolving what is needed', () => {
129+
const formArgs = new FormArguments();
130+
formArgs.setAll({
131+
simple: '123',
132+
first: {
133+
second: '456',
134+
},
135+
complex: {
136+
more: {
137+
stuff: {
138+
here: '789',
139+
},
140+
},
141+
branch: {
142+
here: 'foobar',
143+
},
144+
},
145+
});
146+
147+
expect(formArgs.normalize('hello {simple}')).toEqual({
148+
type: 'text',
149+
text: 'hello 123',
150+
});
151+
152+
expect(
153+
formArgs.normalize({
154+
translate: 'hello {first.second}',
155+
args: ['1', '{simple}'],
156+
})
157+
).toEqual({
158+
type: 'translate',
159+
translate: 'hello 456',
160+
args: [
161+
{
162+
type: 'text',
163+
text: '1',
164+
},
165+
{
166+
type: 'text',
167+
text: '123',
168+
},
169+
],
170+
});
171+
172+
expect(
173+
formArgs.normalize([
174+
'hello {simple}',
175+
'how are {first.second}',
176+
['nesting'],
177+
{
178+
translate: 'hello {complex.more.stuff.here}',
179+
args: [
180+
['hello', 'world'],
181+
{
182+
translate: 'messy {simple}',
183+
args: ['1', '2'],
184+
},
185+
],
186+
},
187+
])
188+
).toEqual({
189+
type: 'array',
190+
array: [
191+
{
192+
type: 'text',
193+
text: 'hello 123',
194+
},
195+
{
196+
type: 'text',
197+
text: 'how are 456',
198+
},
199+
{
200+
type: 'array',
201+
array: [
202+
{
203+
type: 'text',
204+
text: 'nesting',
205+
},
206+
],
207+
},
208+
{
209+
type: 'translate',
210+
translate: 'hello 789',
211+
args: [
212+
{
213+
type: 'array',
214+
array: [
215+
{
216+
type: 'text',
217+
text: 'hello',
218+
},
219+
{
220+
type: 'text',
221+
text: 'world',
222+
},
223+
],
224+
},
225+
{
226+
type: 'translate',
227+
translate: 'messy 123',
228+
args: [
229+
{
230+
type: 'text',
231+
text: '1',
232+
},
233+
{
234+
type: 'text',
235+
text: '2',
236+
},
237+
],
238+
},
239+
],
240+
},
241+
],
242+
});
243+
});
244+
});

src/Arguments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class FormArguments {
3838
return this._args[name] as Arg;
3939
}
4040

41-
resolvePath(path: string): string {
41+
resolvePath(path: string): StringResolvable {
4242
let current: StringResolvable = this._args;
4343
const splitPath = path.split('.');
4444
for (const step of splitPath) {
@@ -49,12 +49,12 @@ export class FormArguments {
4949
}
5050
}
5151

52-
return current.toString();
52+
return current;
5353
}
5454

5555
resolveTemplate(template: string) {
5656
return template.replace(/\{\s*([^}\s]+)\s*\}/g, (_, p1) => {
57-
return this.resolvePath(p1);
57+
return this.resolvePath(p1).toString();
5858
});
5959
}
6060

0 commit comments

Comments
 (0)