1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/app/scripts/lib/message-manager.test.js

129 lines
3.7 KiB
JavaScript
Raw Normal View History

import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
import MessageManager from './message-manager';
2017-02-01 20:54:01 +01:00
describe('Message Manager', () => {
let messageManager;
2017-02-01 20:54:01 +01:00
beforeEach(() => {
messageManager = new MessageManager({
metricsEvent: jest.fn(),
});
});
2017-02-01 20:54:01 +01:00
describe('#getMsgList', () => {
it('when new should return empty array', () => {
const result = messageManager.messages;
expect(Array.isArray(result)).toStrictEqual(true);
expect(result).toHaveLength(0);
});
});
2017-02-01 20:54:01 +01:00
describe('#addMsg', () => {
it('adds a Msg returned in getMsgList', () => {
const Msg = {
id: 1,
status: TRANSACTION_STATUSES.APPROVED,
metamaskNetworkId: 'unit test',
};
messageManager.addMsg(Msg);
const result = messageManager.messages;
expect(Array.isArray(result)).toStrictEqual(true);
expect(result).toHaveLength(1);
expect(result[0].id).toStrictEqual(1);
});
});
2017-02-01 20:54:01 +01:00
describe('#setMsgStatusApproved', () => {
it('sets the Msg status to approved', () => {
2020-11-03 00:41:28 +01:00
const Msg = {
id: 1,
status: 'unapproved',
metamaskNetworkId: 'unit test',
};
messageManager.addMsg(Msg);
messageManager.setMsgStatusApproved(1);
const result = messageManager.messages;
expect(Array.isArray(result)).toStrictEqual(true);
expect(result).toHaveLength(1);
expect(result[0].status).toStrictEqual(TRANSACTION_STATUSES.APPROVED);
});
});
2017-02-01 20:54:01 +01:00
describe('#rejectMsg', () => {
it('sets the Msg status to rejected', () => {
2020-11-03 00:41:28 +01:00
const Msg = {
id: 1,
status: 'unapproved',
metamaskNetworkId: 'unit test',
};
messageManager.addMsg(Msg);
messageManager.rejectMsg(1);
const result = messageManager.messages;
expect(Array.isArray(result)).toStrictEqual(true);
expect(result).toHaveLength(1);
expect(result[0].status).toStrictEqual(TRANSACTION_STATUSES.REJECTED);
});
});
2017-02-01 20:54:01 +01:00
describe('#_updateMsg', () => {
it('replaces the Msg with the same id', () => {
2020-11-03 00:41:28 +01:00
messageManager.addMsg({
id: '1',
status: 'unapproved',
metamaskNetworkId: 'unit test',
});
2020-11-03 00:41:28 +01:00
messageManager.addMsg({
id: '2',
status: TRANSACTION_STATUSES.APPROVED,
2020-11-03 00:41:28 +01:00
metamaskNetworkId: 'unit test',
});
2020-11-03 00:41:28 +01:00
messageManager._updateMsg({
id: '1',
status: 'blah',
hash: 'foo',
metamaskNetworkId: 'unit test',
});
const result = messageManager.getMsg('1');
expect(result.hash).toStrictEqual('foo');
});
});
2017-02-01 20:54:01 +01:00
describe('#getUnapprovedMsgs', () => {
it('returns unapproved Msgs in a hash', () => {
2020-11-03 00:41:28 +01:00
messageManager.addMsg({
id: '1',
status: 'unapproved',
metamaskNetworkId: 'unit test',
});
2020-11-03 00:41:28 +01:00
messageManager.addMsg({
id: '2',
status: TRANSACTION_STATUSES.APPROVED,
2020-11-03 00:41:28 +01:00
metamaskNetworkId: 'unit test',
});
const result = messageManager.getUnapprovedMsgs();
expect(typeof result).toStrictEqual('object');
expect(result['1'].status).toStrictEqual('unapproved');
expect(result['2']).toBeUndefined();
});
});
2017-02-01 20:54:01 +01:00
describe('#getMsg', () => {
it('returns a Msg with the requested id', () => {
2020-11-03 00:41:28 +01:00
messageManager.addMsg({
id: '1',
status: 'unapproved',
metamaskNetworkId: 'unit test',
});
2020-11-03 00:41:28 +01:00
messageManager.addMsg({
id: '2',
status: TRANSACTION_STATUSES.APPROVED,
2020-11-03 00:41:28 +01:00
metamaskNetworkId: 'unit test',
});
expect(messageManager.getMsg('1').status).toStrictEqual('unapproved');
expect(messageManager.getMsg('2').status).toStrictEqual(
TRANSACTION_STATUSES.APPROVED,
);
});
});
});