Skip to content
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

[MOB-7175]: add new filter method that leaves in JSON only messages #238

Merged
merged 2 commits into from
Oct 19, 2023
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
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
Loading