iachat/app/javascript/dashboard/store/modules/specs/conversations/helpers.spec.js
Nithin David Thomas 5ea0436051
feat: Adds support for draft in conversation reply box (#4205)
* Add draft support

* Fixes issue with draft loading

* Adds draft for private notes

* Use localstorage helper

* .remove instead of .clear

* Remove timestamp

* clearLocalStorageOnLogout

* Fix draft save on refresh

* Remove usage of delete operator

* Adds autosave for draft messages

* Remove setinterval and add debounce

* Removes draft redundancy check

* Adds test cases for debouncer

* Update app/javascript/shared/helpers/specs/TimeHelpers.spec.js

Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>

* Update app/javascript/shared/helpers/specs/TimeHelpers.spec.js

Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>

* Review fixes

* Fixes issue with debouncer

* FIxes debouncer issue

* Fixes issue with draft empty message

* Removes empty keys from local storage drafts

* Fixes error with empty draft

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
Co-authored-by: Fayaz Ahmed <15716057+fayazara@users.noreply.github.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: iamsivin <iamsivin@gmail.com>
2022-04-07 22:16:45 +05:30

129 lines
3.4 KiB
JavaScript

import {
findPendingMessageIndex,
applyPageFilters,
trimMessage,
} from '../../conversations/helpers';
const conversationList = [
{
id: 1,
inbox_id: 2,
status: 'open',
meta: {},
labels: ['sales', 'dev'],
},
{
id: 2,
inbox_id: 2,
status: 'open',
meta: {},
labels: ['dev'],
},
{
id: 11,
inbox_id: 3,
status: 'resolved',
meta: { team: { id: 5 } },
labels: [],
},
{
id: 22,
inbox_id: 4,
status: 'pending',
meta: { team: { id: 5 } },
labels: ['sales'],
},
];
describe('#findPendingMessageIndex', () => {
it('returns the correct index of pending message with id', () => {
const chat = {
messages: [{ id: 1, status: 'progress' }],
};
const message = { echo_id: 1 };
expect(findPendingMessageIndex(chat, message)).toEqual(0);
});
it('returns -1 if pending message with id is not present', () => {
const chat = {
messages: [{ id: 1, status: 'progress' }],
};
const message = { echo_id: 2 };
expect(findPendingMessageIndex(chat, message)).toEqual(-1);
});
});
describe('#applyPageFilters', () => {
describe('#filter-team', () => {
it('returns true if conversation has team and team filter is active', () => {
const filters = {
status: 'resolved',
teamId: 5,
};
expect(applyPageFilters(conversationList[2], filters)).toEqual(true);
});
it('returns true if conversation has no team and team filter is active', () => {
const filters = {
status: 'open',
teamId: 5,
};
expect(applyPageFilters(conversationList[0], filters)).toEqual(false);
});
});
describe('#filter-inbox', () => {
it('returns true if conversation has inbox and inbox filter is active', () => {
const filters = {
status: 'pending',
inboxId: 4,
};
expect(applyPageFilters(conversationList[3], filters)).toEqual(true);
});
it('returns true if conversation has no inbox and inbox filter is active', () => {
const filters = {
status: 'open',
inboxId: 5,
};
expect(applyPageFilters(conversationList[0], filters)).toEqual(false);
});
});
describe('#filter-labels', () => {
it('returns true if conversation has labels and labels filter is active', () => {
const filters = {
status: 'open',
labels: ['dev'],
};
expect(applyPageFilters(conversationList[0], filters)).toEqual(true);
});
it('returns true if conversation has no inbox and inbox filter is active', () => {
const filters = {
status: 'open',
labels: ['dev'],
};
expect(applyPageFilters(conversationList[2], filters)).toEqual(false);
});
});
describe('#filter-status', () => {
it('returns true if conversation has status and status filter is active', () => {
const filters = {
status: 'open',
};
expect(applyPageFilters(conversationList[1], filters)).toEqual(true);
});
it('returns true if conversation has status and status filter is all', () => {
const filters = {
status: 'all',
};
expect(applyPageFilters(conversationList[1], filters)).toEqual(true);
});
});
});
describe('#trimMessage', () => {
expect(trimMessage('Hello world', 5)).toEqual('Hello');
expect(trimMessage('Hello', 5)).toEqual('Hello');
expect(trimMessage(undefined, 5)).toEqual('');
});