2021-02-04 19:15:23 +01:00
|
|
|
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
|
2021-03-16 22:00:08 +01:00
|
|
|
import PersonalMessageManager from './personal-message-manager';
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('Personal Message Manager', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let messageManager;
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
messageManager = new PersonalMessageManager({
|
|
|
|
metricsEvent: jest.fn(),
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#getMsgList', () => {
|
|
|
|
it('when new should return empty array', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = messageManager.messages;
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(Array.isArray(result)).toStrictEqual(true);
|
|
|
|
expect(result).toHaveLength(0);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#addMsg', () => {
|
|
|
|
it('adds a Msg returned in getMsgList', () => {
|
2020-11-07 08:38:12 +01:00
|
|
|
const Msg = {
|
|
|
|
id: 1,
|
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
messageManager.addMsg(Msg);
|
|
|
|
const result = messageManager.messages;
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(Array.isArray(result)).toStrictEqual(true);
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].id).toStrictEqual(1);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#setMsgStatusApproved', () => {
|
|
|
|
it('sets the Msg status to approved', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const Msg = {
|
|
|
|
id: 1,
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
messageManager.addMsg(Msg);
|
|
|
|
messageManager.setMsgStatusApproved(1);
|
|
|
|
const result = messageManager.messages;
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(Array.isArray(result)).toStrictEqual(true);
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].status).toStrictEqual(TRANSACTION_STATUSES.APPROVED);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#rejectMsg', () => {
|
|
|
|
it('sets the Msg status to rejected', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const Msg = {
|
|
|
|
id: 1,
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
messageManager.addMsg(Msg);
|
|
|
|
messageManager.rejectMsg(1);
|
|
|
|
const result = messageManager.messages;
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(Array.isArray(result)).toStrictEqual(true);
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].status).toStrictEqual(TRANSACTION_STATUSES.REJECTED);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#_updateMsg', () => {
|
|
|
|
it('replaces the Msg with the same id', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '1',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '2',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager._updateMsg({
|
|
|
|
id: '1',
|
|
|
|
status: 'blah',
|
|
|
|
hash: 'foo',
|
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const result = messageManager.getMsg('1');
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(result.hash).toStrictEqual('foo');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#getUnapprovedMsgs', () => {
|
|
|
|
it('returns unapproved Msgs in a hash', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '1',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '2',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const result = messageManager.getUnapprovedMsgs();
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(typeof result).toStrictEqual('object');
|
|
|
|
expect(result['1'].status).toStrictEqual(TRANSACTION_STATUSES.UNAPPROVED);
|
|
|
|
expect(result['2']).toBeUndefined();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#getMsg', () => {
|
|
|
|
it('returns a Msg with the requested id', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '1',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '2',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(messageManager.getMsg('1').status).toStrictEqual(
|
2020-11-07 08:38:12 +01:00
|
|
|
TRANSACTION_STATUSES.UNAPPROVED,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(messageManager.getMsg('2').status).toStrictEqual(
|
2020-11-07 08:38:12 +01:00
|
|
|
TRANSACTION_STATUSES.APPROVED,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2017-03-07 00:03:49 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('#normalizeMsgData', () => {
|
|
|
|
it('converts text to a utf8 hex string', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const input = 'hello';
|
|
|
|
const output = messageManager.normalizeMsgData(input);
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(output).toStrictEqual('0x68656c6c6f');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-03-07 00:03:49 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
it('tolerates a hex prefix', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const input = '0x12';
|
|
|
|
const output = messageManager.normalizeMsgData(input);
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(output).toStrictEqual('0x12');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-03-07 00:03:49 +01:00
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
it('tolerates normal hex', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const input = '12';
|
|
|
|
const output = messageManager.normalizeMsgData(input);
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(output).toStrictEqual('0x12');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|