Skip to content

Commit

Permalink
[MOB-7175]: add new filter method that leaves in JSON only messages (#…
Browse files Browse the repository at this point in the history
…238)

* add new filter method that leaves in JSON only messages

* Mentioning filterOnlyReadAndNeverTriggerMessages

---------

Co-authored-by: mitch prewitt <[email protected]>
Co-authored-by: Brad Umbaugh <[email protected]>
  • Loading branch information
3 people committed Dec 6, 2023
1 parent f16f378 commit 61ca0db
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ request()

:rotating_light: *_PLEASE NOTE_*: If you choose the `deferred` option, the SDK will _not_ do any filtering or sorting on the messages internally. You will get the messages exactly as they come down from the API, untouched. This means you may (for example) show in-app messages marked `read` or show the messages in the wrong order based on `priority`.

If you want to keep the default sorting and filtering, please take advantage of the `sortInAppMessages` and `filterHiddenInAppMessages` methods the SDK provides.
If you want to keep the default sorting and filtering, please take advantage of the `sortInAppMessages` and `filterHiddenInAppMessages` methods the SDK provides. Also see `filterOnlyReadAndNeverTriggerMessages`, which is similar to `filterHiddenInAppMessages` but does not filter out JSON-only messages.

## initialize

Expand Down
97 changes: 96 additions & 1 deletion src/inapp/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CLOSE_BUTTON_POSITION, CachedMessage, InAppMessage } from '../types';
import {
addButtonAttrsToAnchorTag,
filterHiddenInAppMessages,
filterOnlyReadAndNeverTriggerMessages,
generateCloseButton,
generateWidth,
getCachedMessagesToDelete,
Expand All @@ -33,7 +34,7 @@ const mockMarkup = `
`;

describe('Utils', () => {
describe('Filtering', () => {
describe('filterHiddenInAppMessages', () => {
it('should filter out read messages', () => {
expect(filterHiddenInAppMessages()).toEqual([]);
expect(filterHiddenInAppMessages(messages).every((e) => !e?.read)).toBe(
Expand Down Expand Up @@ -128,6 +129,100 @@ describe('Utils', () => {
});
});

describe('filterOnlyReadAndNeverTriggerMessages', () => {
it('should filter out read messages', () => {
expect(filterOnlyReadAndNeverTriggerMessages()).toEqual([]);
expect(
filterOnlyReadAndNeverTriggerMessages(messages).every((e) => !e?.read)
).toBe(true);
expect(
filterOnlyReadAndNeverTriggerMessages([
{
...messages[0],
read: true
},
{
...messages[1],
read: true
}
]).length
).toBe(0);
expect(
filterOnlyReadAndNeverTriggerMessages([
{
...messages[0],
trigger: { type: 'good' },
read: undefined
},
{
...messages[1],
trigger: { type: 'good' },
read: undefined
}
]).length
).toBe(2);
});

it('should filter out trigger type "never" messages', () => {
expect(
filterOnlyReadAndNeverTriggerMessages(messages).every(
(e) => !e?.trigger?.type
)
).not.toBe('never');
expect(
filterOnlyReadAndNeverTriggerMessages([
{
...messages[0],
trigger: { type: 'never' }
},
{
...messages[1],
trigger: { type: 'never' }
}
]).length
).toBe(0);
expect(
filterOnlyReadAndNeverTriggerMessages([
{
...messages[0],
trigger: undefined
},
{
...messages[1],
trigger: undefined
}
]).length
).toBe(2);
});

it('should not filter out messages with no HTML body', () => {
expect(
filterOnlyReadAndNeverTriggerMessages([
{
...messages[0],
content: { ...messages[0].content, html: '' }
},
{
...messages[1],
content: { ...messages[0].content, html: '<p>' }
}
]).length
).toBe(2);
expect(
filterOnlyReadAndNeverTriggerMessages([
{
...messages[0],
content: undefined
},
{
...messages[1],
content: undefined
}
]).length
).toBe(2);
});
});

describe('Sorting', () => {
it('should sort messages by priority level, lesser ones first', () => {
expect(sortInAppMessages()).toEqual([]);
Expand Down
8 changes: 8 additions & 0 deletions src/inapp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export const filterHiddenInAppMessages = (
});
};

export const filterOnlyReadAndNeverTriggerMessages = (
messages: Partial<InAppMessage>[] = []
) => {
return messages.filter(
(eachMessage) => !eachMessage.read && eachMessage.trigger?.type !== 'never'
);
};

export const sortInAppMessages = (messages: Partial<InAppMessage>[] = []) => {
return messages.sort(by(['priorityLevel', 'asc'], ['createdAt', 'asc']));
};
Expand Down

0 comments on commit 61ca0db

Please sign in to comment.