-
Notifications
You must be signed in to change notification settings - Fork 43
[MOB-7936] create IterableEmbeddedPlacement class #644
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { IterableEmbeddedMessage } from '../embedded/classes/IterableEmbeddedMessage'; | ||
| 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', | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { IterableEmbeddedPlacement } from '../embedded/classes/IterableEmbeddedPlacement'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| import { IterableEmbeddedMessage } from '../embedded/classes/IterableEmbeddedMessage'; | ||
| import { Iterable } from '../core/classes/Iterable'; | ||
|
|
||
| describe('IterableEmbeddedPlacement', () => { | ||
| it('should create an instance with placementId and messages', () => { | ||
| Iterable.logger.log('iterableEmbeddedPlacement_fromDict_with_messages'); | ||
|
|
||
| const dict = { | ||
| placementId: 123, | ||
| messages: [ | ||
| { | ||
| metadata: { | ||
| messageId: 'msg-1', | ||
| placementId: 123, | ||
| isProof: false, | ||
| }, | ||
| elements: { | ||
| title: 'First Message', | ||
| body: 'Body of first message', | ||
| }, | ||
| }, | ||
| { | ||
| metadata: { | ||
| messageId: 'msg-2', | ||
| placementId: 123, | ||
| isProof: false, | ||
| }, | ||
| elements: { | ||
| title: 'Second Message', | ||
| body: 'Body of second message', | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const placement = IterableEmbeddedPlacement.fromDict(dict); | ||
|
|
||
| expect(placement).toBeInstanceOf(IterableEmbeddedPlacement); | ||
| expect(placement.placementId).toBe(123); | ||
| expect(placement.messages).toBeDefined(); | ||
| expect(placement.messages!.length).toBe(2); | ||
| const messages = placement.messages as [ | ||
| IterableEmbeddedMessage, | ||
| IterableEmbeddedMessage, | ||
| ]; | ||
| expect(messages[0]).toBeInstanceOf(IterableEmbeddedMessage); | ||
| expect(messages[1]).toBeInstanceOf(IterableEmbeddedMessage); | ||
| expect(messages[0].metadata.messageId).toBe('msg-1'); | ||
| expect(messages[1].metadata.messageId).toBe('msg-2'); | ||
| }); | ||
|
|
||
| it('should create an instance with only placementId', () => { | ||
| Iterable.logger.log('iterableEmbeddedPlacement_fromDict_placementId_only'); | ||
|
|
||
| const dict = { | ||
| placementId: 456, | ||
| }; | ||
|
|
||
| const placement = IterableEmbeddedPlacement.fromDict(dict); | ||
|
|
||
| expect(placement).toBeInstanceOf(IterableEmbeddedPlacement); | ||
| expect(placement.placementId).toBe(456); | ||
| expect(placement.messages).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should throw an error if placementId is missing', () => { | ||
| Iterable.logger.log( | ||
| 'iterableEmbeddedPlacement_fromDict_missing_placementId' | ||
| ); | ||
|
|
||
| const dict = { | ||
| messages: [ | ||
| { | ||
| metadata: { | ||
| messageId: 'msg-1', | ||
| placementId: 123, | ||
| isProof: false, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| expect(() => IterableEmbeddedPlacement.fromDict(dict)).toThrow( | ||
| 'placementId is required' | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle empty messages array', () => { | ||
| Iterable.logger.log('iterableEmbeddedPlacement_fromDict_empty_messages'); | ||
|
|
||
| const dict = { | ||
| placementId: 789, | ||
| messages: [], | ||
| }; | ||
|
|
||
| const placement = IterableEmbeddedPlacement.fromDict(dict); | ||
|
|
||
| expect(placement).toBeInstanceOf(IterableEmbeddedPlacement); | ||
| expect(placement.placementId).toBe(789); | ||
| expect(placement.messages).toBeDefined(); | ||
| expect(placement.messages!.length).toBe(0); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { IterableEmbeddedMessageMetadata } from './IterableEmbeddedMessageMetadata'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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 { | ||
| metadata: IterableEmbeddedMessageMetadata; | ||
| elements: IterableEmbeddedMessageElements; | ||
| payload: Record<string, unknown>; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,52 @@ | ||
| import { IterableEmbeddedMessage } from './IterableEmbeddedMessage'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| /** | ||
| * Iterable embedded placement | ||
| * Contains placement id and the associated embedded messages | ||
| * IterableEmbeddedPlacement represents an embedded placement. | ||
| */ | ||
| export class IterableEmbeddedPlacement { | ||
| /** The placement id of the embedded placement */ | ||
| readonly placementId: number; | ||
| /** The messages associated with the embedded placement */ | ||
| readonly messages?: IterableEmbeddedMessage[]; | ||
|
|
||
| constructor(placementId: number) { | ||
| /** | ||
| * Creates an instance of `IterableEmbeddedPlacement`. | ||
| * | ||
| * @param placementId - The placement id of the embedded placement. | ||
| * @param messages - The messages associated with the embedded placement. | ||
| */ | ||
| constructor(placementId: number, messages?: IterableEmbeddedMessage[]) { | ||
| this.placementId = placementId; | ||
| this.messages = messages; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an instance of `IterableEmbeddedPlacement` from a dictionary object. | ||
| * | ||
| * @param dict - The dictionary object containing the properties to initialize the `IterableEmbeddedPlacement` instance. | ||
| * @returns A new instance of `IterableEmbeddedPlacement` initialized with the provided dictionary properties. | ||
| */ | ||
| static fromDict( | ||
| dict: Partial<EmbeddedPlacementDict> | ||
| ): IterableEmbeddedPlacement { | ||
| if (!dict.placementId) { | ||
| throw new Error('placementId is required'); | ||
| } | ||
|
|
||
| const placementId = dict.placementId; | ||
| const messages = dict.messages | ||
| ? dict.messages?.map((message) => | ||
| IterableEmbeddedMessage.fromDict(message) | ||
| ) | ||
| : undefined; | ||
| return new IterableEmbeddedPlacement(placementId, messages); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An interface defining the dictionary object containing the properties for the embedded placement. | ||
| */ | ||
| export interface EmbeddedPlacementDict { | ||
| placementId: number; | ||
| messages?: IterableEmbeddedMessage[]; | ||
| } | ||
There was a problem hiding this comment.
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]