Skip to content

Commit 547377c

Browse files
author
Evan Greer
committed
feat: adds unit tests for IterableEmbeddedMessageButton
1 parent 1ce1e39 commit 547377c

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { IterableEmbeddedMessageButton } from '../embedded/classes/IterableEmbeddedMessageButton';
2+
import { IterableEmbeddedMessageElementsButtonAction } from '../embedded/classes/IterableEmbeddedMessageElementsButtonAction';
3+
import { Iterable } from '../core/classes/Iterable';
4+
5+
describe('IterableEmbeddedMessageButton', () => {
6+
it('should create an instance with all properties including button action', () => {
7+
Iterable.logger.log(
8+
'iterableEmbeddedMessageButton_fromDict_all_properties'
9+
);
10+
11+
const dict = {
12+
id: 'button-123',
13+
title: 'Click Me!',
14+
action: { type: 'openUrl', data: 'https://example.com' },
15+
};
16+
17+
const button = IterableEmbeddedMessageButton.fromDict(dict);
18+
19+
expect(button).toBeInstanceOf(IterableEmbeddedMessageButton);
20+
expect(button.id).toBe('button-123');
21+
expect(button.title).toBe('Click Me!');
22+
expect(button.action).toBeInstanceOf(
23+
IterableEmbeddedMessageElementsButtonAction
24+
);
25+
expect(button.action?.type).toBe('openUrl');
26+
expect(button.action?.data).toBe('https://example.com');
27+
});
28+
29+
it('should create an instance with only required properties', () => {
30+
Iterable.logger.log('iterableEmbeddedMessageButton_fromDict_required_only');
31+
32+
const dict = { id: 'button-123' };
33+
34+
const button = IterableEmbeddedMessageButton.fromDict(dict);
35+
36+
expect(button).toBeInstanceOf(IterableEmbeddedMessageButton);
37+
expect(button.id).toBe('button-123');
38+
expect(button.title).toBeUndefined();
39+
expect(button.action).toBeUndefined();
40+
});
41+
42+
it('should create an instance with title but no action', () => {
43+
Iterable.logger.log('iterableEmbeddedMessageButton_fromDict_title_only');
44+
45+
const dict = {
46+
id: 'button-123',
47+
title: 'Click Me!',
48+
};
49+
50+
const button = IterableEmbeddedMessageButton.fromDict(dict);
51+
52+
expect(button).toBeInstanceOf(IterableEmbeddedMessageButton);
53+
expect(button.id).toBe('button-123');
54+
expect(button.title).toBe('Click Me!');
55+
expect(button.action).toBeUndefined();
56+
});
57+
58+
it('should throw an error if id is missing in fromDict', () => {
59+
Iterable.logger.log('iterableEmbeddedMessageButton_fromDict_missing_id');
60+
61+
const dict = {
62+
title: 'Click Me!',
63+
action: { type: 'openUrl', data: 'https://example.com' },
64+
};
65+
66+
expect(() => IterableEmbeddedMessageButton.fromDict(dict)).toThrow(
67+
'id is required'
68+
);
69+
});
70+
71+
it('should handle button action with only type', () => {
72+
Iterable.logger.log(
73+
'iterableEmbeddedMessageButton_fromDict_action_type_only'
74+
);
75+
76+
const dict = {
77+
id: 'button-123',
78+
action: { type: 'close' },
79+
};
80+
81+
const button = IterableEmbeddedMessageButton.fromDict(dict);
82+
83+
expect(button).toBeInstanceOf(IterableEmbeddedMessageButton);
84+
expect(button.id).toBe('button-123');
85+
expect(button.action).toBeInstanceOf(
86+
IterableEmbeddedMessageElementsButtonAction
87+
);
88+
expect(button.action?.type).toBe('close');
89+
expect(button.action?.data).toBeUndefined();
90+
});
91+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { IterableEmbeddedMessageElementsButtonAction } from '../embedded/classes/IterableEmbeddedMessageElementsButtonAction';
2+
import { Iterable } from '../core/classes/Iterable';
3+
4+
describe('IterableEmbeddedMessageDefaultAction', () => {
5+
it('should create an instance with the correct properties', () => {
6+
Iterable.logger.log(
7+
'iterableEmbeddedMessageElementsButtonAction_fromDict_valid_dictionary'
8+
);
9+
10+
const dict = { type: 'openUrl', data: 'https://example.com' };
11+
const action = IterableEmbeddedMessageElementsButtonAction.fromDict(dict);
12+
expect(action).toBeInstanceOf(IterableEmbeddedMessageElementsButtonAction);
13+
expect(action.type).toBe('openUrl');
14+
expect(action.data).toBe('https://example.com');
15+
});
16+
17+
it('should create an instance from a dictionary with data omitted', () => {
18+
Iterable.logger.log(
19+
'iterableEmbeddedMessageElementsButtonAction_fromDict_valid_dictionary_with_data_omitted'
20+
);
21+
22+
const dict = { type: 'action://join', data: '' };
23+
const action = IterableEmbeddedMessageElementsButtonAction.fromDict(dict);
24+
expect(action).toBeInstanceOf(IterableEmbeddedMessageElementsButtonAction);
25+
expect(action.type).toBe('action://join');
26+
expect(action.data).toBe('');
27+
});
28+
29+
it('should throw an error if type is missing in fromDict', () => {
30+
Iterable.logger.log(
31+
'iterableEmbeddedMessageElementsButtonAction_fromDict_invalid_dictionary_missing_type'
32+
);
33+
34+
const dict = { data: 'foo' };
35+
36+
expect(() =>
37+
IterableEmbeddedMessageElementsButtonAction.fromDict(dict)
38+
).toThrow('type is required');
39+
});
40+
});

src/embedded/classes/IterableEmbeddedMessageButton.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export class IterableEmbeddedMessageButton {
2121
if (!dict.id) {
2222
throw new Error('id is required');
2323
}
24-
return new IterableEmbeddedMessageButton(dict.id, dict.title, dict.action);
24+
const action = dict.action
25+
? IterableEmbeddedMessageElementsButtonAction.fromDict(dict.action)
26+
: undefined;
27+
return new IterableEmbeddedMessageButton(dict.id, dict.title, action);
2528
}
2629
}
2730

0 commit comments

Comments
 (0)