Skip to content

[MOB-7936] creates IterableEmbeddedMessage class #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions src/__tests__/IterableEmbeddedMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { IterableEmbeddedMessage } from '../embedded/classes/IterableEmbeddedMessage';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

import { IterableEmbeddedMessageMetadata } from '../embedded/classes/IterableEmbeddedMessageMetadata';
import { IterableEmbeddedMessageElements } from '../embedded/classes/IterableEmbeddedMessageElements';
import { Iterable } from '../core/classes/Iterable';

describe('IterableEmbeddedMessage', () => {
it('should create an instance with all properties', () => {
Iterable.logger.log('iterableEmbeddedMessage_fromDict_all_properties');

const dict = {
metadata: {
messageId: 'msg-123',
placementId: 1,
campaignId: 456,
isProof: false,
},
elements: {
title: 'Awesome Title',
body: 'Radical Body Text',
mediaUrl: 'https://example.com/image.jpg',
mediaUrlCaption: 'Check out this sick image!',
defaultAction: {
type: 'openUrl',
data: 'https://example.com',
},
buttons: [
{
id: 'button-1',
title: 'Click Me!',
action: {
type: 'openUrl',
data: 'https://example.com/button1',
},
},
],
text: [
{
id: 'text-1',
text: 'Some cool text',
type: 'body',
},
],
},
payload: {
customKey: 'customValue',
anotherKey: 123,
},
};

const message = IterableEmbeddedMessage.fromDict(dict);

expect(message).toBeInstanceOf(IterableEmbeddedMessage);

// Check metadata
expect(message.metadata).toBeInstanceOf(IterableEmbeddedMessageMetadata);
expect(message.metadata.messageId).toBe('msg-123');
expect(message.metadata.placementId).toBe(1);
expect(message.metadata.campaignId).toBe(456);
expect(message.metadata.isProof).toBe(false);

// Check elements
expect(message.elements).toBeInstanceOf(IterableEmbeddedMessageElements);
expect(message.elements?.title).toBe('Awesome Title');
expect(message.elements?.body).toBe('Radical Body Text');
expect(message.elements?.mediaUrl).toBe('https://example.com/image.jpg');
expect(message.elements?.mediaUrlCaption).toBe(
'Check out this sick image!'
);

// Check payload
expect(message.payload).toEqual({
customKey: 'customValue',
anotherKey: 123,
});
});

it('should create an instance with only required metadata', () => {
Iterable.logger.log('iterableEmbeddedMessage_fromDict_required_only');

const dict = {
metadata: {
messageId: 'msg-123',
placementId: 1,
isProof: false,
},
};

const message = IterableEmbeddedMessage.fromDict(dict);

expect(message).toBeInstanceOf(IterableEmbeddedMessage);
expect(message.metadata).toBeInstanceOf(IterableEmbeddedMessageMetadata);
expect(message.metadata.messageId).toBe('msg-123');
expect(message.metadata.placementId).toBe(1);
expect(message.metadata.campaignId).toBeUndefined();
expect(message.metadata.isProof).toBe(false);
expect(message.elements).toBeUndefined();
expect(message.payload).toBeUndefined();
});

it('should throw an error if metadata is missing', () => {
Iterable.logger.log('iterableEmbeddedMessage_fromDict_missing_metadata');

const dict = {
elements: {
title: 'Some Title',
body: 'Some Body',
},
};

expect(() => IterableEmbeddedMessage.fromDict(dict)).toThrow(
'metadata is required'
);
});

it('should create an instance with elements but no payload', () => {
Iterable.logger.log('iterableEmbeddedMessage_fromDict_elements_only');

const dict = {
metadata: {
messageId: 'msg-123',
placementId: 1,
isProof: false,
},
elements: {
title: 'Elements Only',
body: 'No payload here',
},
};

const message = IterableEmbeddedMessage.fromDict(dict);

expect(message).toBeInstanceOf(IterableEmbeddedMessage);
expect(message.metadata).toBeInstanceOf(IterableEmbeddedMessageMetadata);
expect(message.elements).toBeInstanceOf(IterableEmbeddedMessageElements);
expect(message.elements?.title).toBe('Elements Only');
expect(message.elements?.body).toBe('No payload here');
expect(message.payload).toBeUndefined();
});

it('should create an instance with payload but no elements', () => {
Iterable.logger.log('iterableEmbeddedMessage_fromDict_payload_only');

const dict = {
metadata: {
messageId: 'msg-123',
placementId: 1,
isProof: false,
},
payload: {
someData: 'someValue',
},
};

const message = IterableEmbeddedMessage.fromDict(dict);

expect(message).toBeInstanceOf(IterableEmbeddedMessage);
expect(message.metadata).toBeInstanceOf(IterableEmbeddedMessageMetadata);
expect(message.elements).toBeUndefined();
expect(message.payload).toEqual({
someData: 'someValue',
});
});
});
61 changes: 61 additions & 0 deletions src/embedded/classes/IterableEmbeddedMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { IterableEmbeddedMessageMetadata } from './IterableEmbeddedMessageMetadata';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error loading TSDoc config file:
Error encountered for /home/runner/work/react-native-sdk/tsdoc.json:
Unable to resolve "extends" reference to "typedoc/tsdoc.json": Cannot find module 'typedoc/tsdoc.json' from '/home/runner/work/react-native-sdk'
[eslint:tsdoc/syntax]

import { IterableEmbeddedMessageElements } from './IterableEmbeddedMessageElements';

/**
* IterableEmbeddedMessage represents an embedded message.
*/
export class IterableEmbeddedMessage {
/** The metadata of the embedded message */
metadata: IterableEmbeddedMessageMetadata;
/** The elements of the embedded message */
elements?: IterableEmbeddedMessageElements;
/** The custom payload of the embedded message */
payload?: Record<string, unknown>;

/**
* Creates an instance of `IterableEmbeddedMessage`.
*
* @param metadata - The metadata of the embedded message.
* @param elements - The elements of the embedded message.
* @param payload - The custom payload of the embedded message.
*/
constructor(
metadata: IterableEmbeddedMessageMetadata,
elements?: IterableEmbeddedMessageElements,
payload?: Record<string, unknown>
) {
this.metadata = metadata;
this.elements = elements;
this.payload = payload;
}

/**
* Creates an instance of `IterableEmbeddedMessage` from a dictionary object.
*
* @param dict - The dictionary object containing the properties to initialize the `IterableEmbeddedMessage` instance.
* @returns A new instance of `IterableEmbeddedMessage` initialized with the provided dictionary properties.
*/
static fromDict(dict: Partial<EmbeddedMessageDict>): IterableEmbeddedMessage {
if (!dict.metadata) {
throw new Error('metadata is required');
}
const metadata = IterableEmbeddedMessageMetadata.fromDict(dict.metadata);
const elements = dict.elements
? IterableEmbeddedMessageElements.fromDict(dict.elements)
: undefined;
const payload = dict.payload;
return new IterableEmbeddedMessage(metadata, elements, payload);
}
}

/**
* An interface defining the dictionary object containing the properties for the embedded message.
*/
interface EmbeddedMessageDict {
/** The metadata of the embedded message */
metadata: IterableEmbeddedMessageMetadata;
/** The elements of the embedded message */
elements: IterableEmbeddedMessageElements;
/** The custom payload of the embedded message */
payload: Record<string, unknown>;
}