From 6ccf2811e75924d7e92793df7e4ec915770cf6b4 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Tue, 21 Aug 2018 19:17:57 -0300 Subject: unit tests for watchAsset --- .../app/controllers/preferences-controller-test.js | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) (limited to 'test/unit/app/controllers/preferences-controller-test.js') diff --git a/test/unit/app/controllers/preferences-controller-test.js b/test/unit/app/controllers/preferences-controller-test.js index 9b2c846bd..58fc3d9c5 100644 --- a/test/unit/app/controllers/preferences-controller-test.js +++ b/test/unit/app/controllers/preferences-controller-test.js @@ -1,6 +1,7 @@ const assert = require('assert') const ObservableStore = require('obs-store') const PreferencesController = require('../../../../app/scripts/controllers/preferences') +const sinon = require('sinon') describe('preferences controller', function () { let preferencesController @@ -339,5 +340,114 @@ describe('preferences controller', function () { assert.deepEqual(tokensSecond, initialTokensSecond, 'tokens equal for same network') }) }) + + describe('on watchAsset', function () { + var stubNext, stubEnd, stubHandleWatchAssetERC20, asy, req, res + const sandbox = sinon.createSandbox() + + beforeEach(() => { + req = {params: {}} + res = {} + asy = {next: () => {}, end: () => {}} + stubNext = sandbox.stub(asy, 'next') + stubEnd = sandbox.stub(asy, 'end').returns(0) + stubHandleWatchAssetERC20 = sandbox.stub(preferencesController, '_handleWatchAssetERC20') + }) + after(() => { + sandbox.restore() + }) + + it('should do anything if method not corresponds', async function () { + const asy = {next: () => {}, end: () => {}} + var stubNext = sandbox.stub(asy, 'next') + var stubEnd = sandbox.stub(asy, 'end').returns(0) + req.method = 'metamask' + await preferencesController.requestWatchAsset(req, res, asy.next, asy.end) + sandbox.assert.notCalled(stubEnd) + sandbox.assert.called(stubNext) + }) + it('should do something if method is supported', async function () { + const asy = {next: () => {}, end: () => {}} + var stubNext = sandbox.stub(asy, 'next') + var stubEnd = sandbox.stub(asy, 'end').returns(0) + req.method = 'metamask_watchAsset' + req.params.type = 'someasset' + await preferencesController.requestWatchAsset(req, res, asy.next, asy.end) + sandbox.assert.called(stubEnd) + sandbox.assert.notCalled(stubNext) + }) + it('should through error if method is supported but asset type is not', async function () { + req.method = 'metamask_watchAsset' + req.params.type = 'someasset' + await preferencesController.requestWatchAsset(req, res, asy.next, asy.end) + sandbox.assert.called(stubEnd) + sandbox.assert.notCalled(stubHandleWatchAssetERC20) + sandbox.assert.notCalled(stubNext) + assert.deepEqual(res, {}) + }) + it('should trigger handle add asset if type supported', async function () { + const asy = {next: () => {}, end: () => {}} + req.method = 'metamask_watchAsset' + req.params.type = 'ERC20' + await preferencesController.requestWatchAsset(req, res, asy.next, asy.end) + sandbox.assert.called(stubHandleWatchAssetERC20) + }) + }) + + describe('on watchAsset of type ERC20', function () { + var req + + const sandbox = sinon.createSandbox() + beforeEach(() => { + req = {params: {type: 'ERC20'}} + }) + after(() => { + sandbox.restore() + }) + + it('should add suggested token', async function () { + const address = '0xabcdef1234567' + const symbol = 'ABBR' + const decimals = 5 + const imageUrl = 'someimageurl' + req.params.options = { address, symbol, decimals, imageUrl } + + sandbox.stub(preferencesController, '_validateERC20AssetParams').returns(true) + preferencesController.showWatchAssetUi = async () => {} + + await preferencesController._handleWatchAssetERC20(req.params.options) + const suggested = preferencesController.getSuggestedTokens() + assert.equal(Object.keys(suggested).length, 1, `one token added ${Object.keys(suggested)}`) + + assert.equal(suggested[address].address, address, 'set address correctly') + assert.equal(suggested[address].symbol, symbol, 'set symbol correctly') + assert.equal(suggested[address].decimals, decimals, 'set decimals correctly') + assert.equal(suggested[address].imageUrl, imageUrl, 'set imageUrl correctly') + }) + + it('should add token correctly if user confirms', async function () { + const address = '0xabcdef1234567' + const symbol = 'ABBR' + const decimals = 5 + const imageUrl = 'someimageurl' + req.params.options = { address, symbol, decimals, imageUrl } + + sandbox.stub(preferencesController, '_validateERC20AssetParams').returns(true) + preferencesController.showWatchAssetUi = async () => { + await preferencesController.addToken(address, symbol, decimals, imageUrl) + } + + await preferencesController._handleWatchAssetERC20(req.params.options) + const tokens = preferencesController.getTokens() + assert.equal(tokens.length, 1, `one token added`) + const added = tokens[0] + assert.equal(added.address, address, 'set address correctly') + assert.equal(added.symbol, symbol, 'set symbol correctly') + assert.equal(added.decimals, decimals, 'set decimals correctly') + + const assetImages = preferencesController.getAssetImages() + assert.ok(assetImages[address], `set imageurl correctly`) + }) + }) }) -- cgit From b59a1e91b8f4b595500a0785f325e833fa35407d Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Thu, 23 Aug 2018 15:54:40 -0300 Subject: typo watchAsset imageUrl to image --- test/unit/app/controllers/preferences-controller-test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'test/unit/app/controllers/preferences-controller-test.js') diff --git a/test/unit/app/controllers/preferences-controller-test.js b/test/unit/app/controllers/preferences-controller-test.js index 58fc3d9c5..d63356215 100644 --- a/test/unit/app/controllers/preferences-controller-test.js +++ b/test/unit/app/controllers/preferences-controller-test.js @@ -409,8 +409,8 @@ describe('preferences controller', function () { const address = '0xabcdef1234567' const symbol = 'ABBR' const decimals = 5 - const imageUrl = 'someimageurl' - req.params.options = { address, symbol, decimals, imageUrl } + const image = 'someimage' + req.params.options = { address, symbol, decimals, image } sandbox.stub(preferencesController, '_validateERC20AssetParams').returns(true) preferencesController.showWatchAssetUi = async () => {} @@ -422,19 +422,19 @@ describe('preferences controller', function () { assert.equal(suggested[address].address, address, 'set address correctly') assert.equal(suggested[address].symbol, symbol, 'set symbol correctly') assert.equal(suggested[address].decimals, decimals, 'set decimals correctly') - assert.equal(suggested[address].imageUrl, imageUrl, 'set imageUrl correctly') + assert.equal(suggested[address].image, image, 'set image correctly') }) it('should add token correctly if user confirms', async function () { const address = '0xabcdef1234567' const symbol = 'ABBR' const decimals = 5 - const imageUrl = 'someimageurl' - req.params.options = { address, symbol, decimals, imageUrl } + const image = 'someimage' + req.params.options = { address, symbol, decimals, image } sandbox.stub(preferencesController, '_validateERC20AssetParams').returns(true) preferencesController.showWatchAssetUi = async () => { - await preferencesController.addToken(address, symbol, decimals, imageUrl) + await preferencesController.addToken(address, symbol, decimals, image) } await preferencesController._handleWatchAssetERC20(req.params.options) @@ -446,7 +446,7 @@ describe('preferences controller', function () { assert.equal(added.decimals, decimals, 'set decimals correctly') const assetImages = preferencesController.getAssetImages() - assert.ok(assetImages[address], `set imageurl correctly`) + assert.ok(assetImages[address], `set image correctly`) }) }) }) -- cgit From 3106374cc31b66e5a0faadd657b4430e21aa48b2 Mon Sep 17 00:00:00 2001 From: Esteban MIno Date: Mon, 27 Aug 2018 22:10:14 -0300 Subject: watchAsset small changes --- test/unit/app/controllers/preferences-controller-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit/app/controllers/preferences-controller-test.js') diff --git a/test/unit/app/controllers/preferences-controller-test.js b/test/unit/app/controllers/preferences-controller-test.js index d63356215..2c261be90 100644 --- a/test/unit/app/controllers/preferences-controller-test.js +++ b/test/unit/app/controllers/preferences-controller-test.js @@ -357,7 +357,7 @@ describe('preferences controller', function () { sandbox.restore() }) - it('should do anything if method not corresponds', async function () { + it('shouldn not do anything if method not corresponds', async function () { const asy = {next: () => {}, end: () => {}} var stubNext = sandbox.stub(asy, 'next') var stubEnd = sandbox.stub(asy, 'end').returns(0) -- cgit