1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/test/unit/app/message-manager-test.js
Brad Decker a49a4a066c
expand transaction constants coverage (#9790)
* expand transaction constants coverage

* touchups

* dont import inside of e2e

* Update app/scripts/controllers/transactions/tx-state-manager.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Update test/unit/app/controllers/transactions/tx-controller-test.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2020-11-07 01:38:12 -06:00

129 lines
3.6 KiB
JavaScript

import assert from 'assert'
import MessageManager from '../../../app/scripts/lib/message-manager'
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction'
describe('Message Manager', function () {
let messageManager
beforeEach(function () {
messageManager = new MessageManager()
})
describe('#getMsgList', function () {
it('when new should return empty array', function () {
const result = messageManager.messages
assert.ok(Array.isArray(result))
assert.equal(result.length, 0)
})
})
describe('#addMsg', function () {
it('adds a Msg returned in getMsgList', function () {
const Msg = {
id: 1,
status: TRANSACTION_STATUSES.APPROVED,
metamaskNetworkId: 'unit test',
}
messageManager.addMsg(Msg)
const result = messageManager.messages
assert.ok(Array.isArray(result))
assert.equal(result.length, 1)
assert.equal(result[0].id, 1)
})
})
describe('#setMsgStatusApproved', function () {
it('sets the Msg status to approved', function () {
const Msg = {
id: 1,
status: 'unapproved',
metamaskNetworkId: 'unit test',
}
messageManager.addMsg(Msg)
messageManager.setMsgStatusApproved(1)
const result = messageManager.messages
assert.ok(Array.isArray(result))
assert.equal(result.length, 1)
assert.equal(result[0].status, TRANSACTION_STATUSES.APPROVED)
})
})
describe('#rejectMsg', function () {
it('sets the Msg status to rejected', function () {
const Msg = {
id: 1,
status: 'unapproved',
metamaskNetworkId: 'unit test',
}
messageManager.addMsg(Msg)
messageManager.rejectMsg(1)
const result = messageManager.messages
assert.ok(Array.isArray(result))
assert.equal(result.length, 1)
assert.equal(result[0].status, TRANSACTION_STATUSES.REJECTED)
})
})
describe('#_updateMsg', function () {
it('replaces the Msg with the same id', function () {
messageManager.addMsg({
id: '1',
status: 'unapproved',
metamaskNetworkId: 'unit test',
})
messageManager.addMsg({
id: '2',
status: TRANSACTION_STATUSES.APPROVED,
metamaskNetworkId: 'unit test',
})
messageManager._updateMsg({
id: '1',
status: 'blah',
hash: 'foo',
metamaskNetworkId: 'unit test',
})
const result = messageManager.getMsg('1')
assert.equal(result.hash, 'foo')
})
})
describe('#getUnapprovedMsgs', function () {
it('returns unapproved Msgs in a hash', function () {
messageManager.addMsg({
id: '1',
status: 'unapproved',
metamaskNetworkId: 'unit test',
})
messageManager.addMsg({
id: '2',
status: TRANSACTION_STATUSES.APPROVED,
metamaskNetworkId: 'unit test',
})
const result = messageManager.getUnapprovedMsgs()
assert.equal(typeof result, 'object')
assert.equal(result['1'].status, 'unapproved')
assert.equal(result['2'], undefined)
})
})
describe('#getMsg', function () {
it('returns a Msg with the requested id', function () {
messageManager.addMsg({
id: '1',
status: 'unapproved',
metamaskNetworkId: 'unit test',
})
messageManager.addMsg({
id: '2',
status: TRANSACTION_STATUSES.APPROVED,
metamaskNetworkId: 'unit test',
})
assert.equal(messageManager.getMsg('1').status, 'unapproved')
assert.equal(
messageManager.getMsg('2').status,
TRANSACTION_STATUSES.APPROVED,
)
})
})
})