Skip to content
Open
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
40 changes: 40 additions & 0 deletions src/__tests__/IterableInApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,46 @@ describe('Iterable In App', () => {
);
});

test('fromDict_withCreatedAtAndExpiresAt_returnsDateObjects', () => {
// GIVEN a message dict with numeric timestamps from the native bridge
const createdAtMs = 1609459200000; // 2021-01-01T00:00:00.000Z
const expiresAtMs = 1609545600000; // 2021-01-02T00:00:00.000Z
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
createdAt: createdAtMs,
expiresAt: expiresAtMs,
priorityLevel: 300.5,
};

// WHEN fromDict is called
const message = IterableInAppMessage.fromDict(messageDict);

// THEN createdAt and expiresAt are Date objects with the correct values
expect(message.createdAt).toBeInstanceOf(Date);
expect(message.expiresAt).toBeInstanceOf(Date);
expect(message.createdAt?.getTime()).toBe(createdAtMs);
expect(message.expiresAt?.getTime()).toBe(expiresAtMs);
});

test('fromDict_withoutCreatedAtAndExpiresAt_returnsUndefined', () => {
// GIVEN a message dict without timestamps
const messageDict = {
messageId: 'message1',
campaignId: 1234,
trigger: { type: IterableInAppTriggerType.immediate },
priorityLevel: 300.5,
};

// WHEN fromDict is called
const message = IterableInAppMessage.fromDict(messageDict);

// THEN createdAt and expiresAt are undefined
expect(message.createdAt).toBeUndefined();
expect(message.expiresAt).toBeUndefined();
});

test('getMessages_noParams_returnsMessages', async () => {
// GIVEN a list of in-app messages representing the local queue
const messageDicts = [
Expand Down
22 changes: 7 additions & 15 deletions src/inApp/classes/IterableInAppMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,12 @@ export class IterableInAppMessage {
const campaignId = dict.campaignId;
const trigger = IterableInAppTrigger.fromDict(dict.trigger);

let createdAt = dict.createdAt;
if (createdAt) {
const dateObject = new Date(0);
createdAt = dateObject.setUTCMilliseconds(createdAt);
}
let expiresAt = dict.expiresAt;
if (expiresAt) {
const dateObject = new Date(0);
expiresAt = dateObject.setUTCMilliseconds(expiresAt);
}
const createdAt = dict.createdAt
? new Date(dict.createdAt)
: undefined;
const expiresAt = dict.expiresAt
? new Date(dict.expiresAt)
: undefined;
const saveToInbox = IterableUtil.readBoolean(dict, 'saveToInbox');
const inboxMetadataDict = dict.inboxMetadata;
let inboxMetadata: IterableInboxMetadata | undefined;
Expand All @@ -196,16 +192,12 @@ export class IterableInAppMessage {
const customPayload = dict.customPayload;
const read = IterableUtil.readBoolean(dict, 'read');

const priorityLevel = dict.priorityLevel;
const priorityLevel = dict.priorityLevel ?? 0;

return new IterableInAppMessage(
messageId,
campaignId,
trigger,
// MOB-10426: Speak to the team about `IterableInAppMessage` requiring a date
// object, but being passed a number in this case
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
createdAt,
expiresAt,
saveToInbox,
Expand Down
Loading