Skip to content

Commit 12ac9b6

Browse files
authored
Merge pull request #655 from Iterable/evan/MOB-11550-iterable-embedded-message-button-class
[MOB-11550] creates iterableEmbeddedMessageButton class
2 parents 034f677 + 27af816 commit 12ac9b6

4 files changed

Lines changed: 246 additions & 0 deletions
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { IterableEmbeddedMessageElementsButton } from '../embedded/classes/IterableEmbeddedMessageElementsButton';
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 = IterableEmbeddedMessageElementsButton.fromDict(dict);
18+
19+
expect(button).toBeInstanceOf(IterableEmbeddedMessageElementsButton);
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 = IterableEmbeddedMessageElementsButton.fromDict(dict);
35+
36+
expect(button).toBeInstanceOf(IterableEmbeddedMessageElementsButton);
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 = IterableEmbeddedMessageElementsButton.fromDict(dict);
51+
52+
expect(button).toBeInstanceOf(IterableEmbeddedMessageElementsButton);
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(() => IterableEmbeddedMessageElementsButton.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 = IterableEmbeddedMessageElementsButton.fromDict(dict);
82+
83+
expect(button).toBeInstanceOf(IterableEmbeddedMessageElementsButton);
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+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { IterableEmbeddedMessageElementsButtonAction } from './IterableEmbeddedMessageElementsButtonAction';
2+
3+
/**
4+
* IterableEmbeddedMessageElementsButton represents a button in an embedded message.
5+
*/
6+
export class IterableEmbeddedMessageElementsButton {
7+
/** The ID for the embedded message button */
8+
readonly id: string;
9+
/** The title for the embedded message button */
10+
readonly title?: string;
11+
/** The action for the embedded message button */
12+
readonly action?: IterableEmbeddedMessageElementsButtonAction;
13+
14+
/**
15+
* Creates an instance of IterableEmbeddedMessageButton.
16+
*
17+
* @param id - The ID for the embedded message button.
18+
* @param title - The title for the embedded message button.
19+
* @param action - The action for the embedded message button.
20+
*/
21+
constructor(
22+
id: string,
23+
title?: string,
24+
action?: IterableEmbeddedMessageElementsButtonAction
25+
) {
26+
this.id = id;
27+
this.title = title;
28+
this.action = action;
29+
}
30+
31+
/**
32+
* Creates an instance of `IterableEmbeddedMessageButton` from a dictionary object.
33+
*
34+
* @param dict - The dictionary object containing the properties to initialize the `IterableEmbeddedMessageButton` instance.
35+
* @returns A new instance of `IterableEmbeddedMessageButton` initialized with the provided dictionary properties.
36+
*/
37+
static fromDict(
38+
dict: Partial<EmbeddedMessageElementsButtonDict>
39+
): IterableEmbeddedMessageElementsButton {
40+
if (!dict.id) {
41+
throw new Error('id is required');
42+
}
43+
const action = dict.action
44+
? IterableEmbeddedMessageElementsButtonAction.fromDict(dict.action)
45+
: undefined;
46+
return new IterableEmbeddedMessageElementsButton(
47+
dict.id,
48+
dict.title,
49+
action
50+
);
51+
}
52+
}
53+
54+
/**
55+
* An interface defining the dictionary object containing the properties for the embedded message button.
56+
*/
57+
export interface EmbeddedMessageElementsButtonDict {
58+
id: string;
59+
title?: string;
60+
action?: IterableEmbeddedMessageElementsButtonAction;
61+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* IterableEmbeddedMessageElementsButtonAction represents an action defined as a response to user events
3+
* for an embedded message button.
4+
*/
5+
export class IterableEmbeddedMessageElementsButtonAction {
6+
/**
7+
* The type of iterable action
8+
* For custom actions, the type is `action://` prefix followed by a custom action name
9+
*/
10+
readonly type: string;
11+
12+
/**
13+
* The url for the action when the type is `openUrl`
14+
* For custom actions, data is empty
15+
*/
16+
readonly data?: string;
17+
18+
/**
19+
* Creates an instance of IterableEmbeddedMessageElementsButtonAction.
20+
*
21+
* @param type - The type of the action.
22+
* @param data - Optional data associated with the action.
23+
*/
24+
constructor(type: string, data?: string) {
25+
this.type = type;
26+
this.data = data;
27+
}
28+
29+
/**
30+
* Creates an instance of `IterableEmbeddedMessageElementsButtonAction` from a dictionary object.
31+
*
32+
* @param dict - The dictionary object containing the properties to initialize the `IterableEmbeddedMessageElementsButtonAction` instance.
33+
* @returns A new instance of `IterableEmbeddedMessageElementsButtonAction` initialized with the provided dictionary properties.
34+
*/
35+
static fromDict(
36+
dict: Partial<EmbeddedMessageButtonActionDict>
37+
): IterableEmbeddedMessageElementsButtonAction {
38+
if (!dict.type) {
39+
throw new Error('type is required');
40+
}
41+
return new IterableEmbeddedMessageElementsButtonAction(
42+
dict.type,
43+
dict.data
44+
);
45+
}
46+
}
47+
48+
/**
49+
* An interface defining the dictionary object containing the properties for the embedded message button action.
50+
*/
51+
export interface EmbeddedMessageButtonActionDict {
52+
type: string;
53+
data?: string;
54+
}

0 commit comments

Comments
 (0)