From f9462a01b77329d2ff972cbeab82ef3c0ae07d8a Mon Sep 17 00:00:00 2001 From: Matt Holtzman Date: Sat, 9 Oct 2021 10:53:23 -0400 Subject: [PATCH 1/3] add unit tests for addNetwork action, update network ID parsing --- main/store/actions/index.js | 2 +- test/main/store/actions/index.test.js | 195 ++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 test/main/store/actions/index.test.js diff --git a/main/store/actions/index.js b/main/store/actions/index.js index bfb679293..0d9bcd488 100644 --- a/main/store/actions/index.js +++ b/main/store/actions/index.js @@ -4,7 +4,7 @@ function validateNetworkSettings (network) { const networkId = parseInt(network.id) if ( - typeof (parseInt(networkId)) !== 'number' || + (!Number.isInteger(networkId)) || typeof (network.type) !== 'string' || typeof (network.name) !== 'string' || typeof (network.explorer) !== 'string' || diff --git a/test/main/store/actions/index.test.js b/test/main/store/actions/index.test.js new file mode 100644 index 000000000..973d8d2ea --- /dev/null +++ b/test/main/store/actions/index.test.js @@ -0,0 +1,195 @@ +import { addNetwork as addNetworkAction } from '../../../../main/store/actions' + +describe('#addNetwork', () => { + const updaterFn = (node, update) => { + if (node !== 'main') throw new Error(`attempted to update wrong node: ${node}`) + update({ networks, networksMeta }) + } + + const addNetwork = network => addNetworkAction(updaterFn, network) + + const network = { + id: 137, + name: 'Polygon', + type: 'ethereum', + layer: 'sidechain', + explorer: 'https://polygonscan.com', + symbol: 'MATIC' + } + + let networks, networksMeta + + beforeEach(() => { + networks = { ethereum: {} } + networksMeta = { ethereum: {} } + }) + + it('adds a network with the correct id', () => { + addNetwork(network) + + expect(networks.ethereum['137'].id).toBe(137) + }) + + it('adds a network with the correct id if the id is a number represented as a string', () => { + addNetwork({ ...network, id: '137' }) + + expect(networks.ethereum['137'].id).toBe(137) + }) + + it('adds a network with the correct name', () => { + addNetwork(network) + + expect(networks.ethereum['137'].name).toBe('Polygon') + }) + + it('adds a network with the correct symbol', () => { + addNetwork(network) + + expect(networks.ethereum['137'].symbol).toBe('MATIC') + }) + + it('adds a network with the correct explorer', () => { + addNetwork(network) + + expect(networks.ethereum['137'].explorer).toBe('https://polygonscan.com') + }) + + it('adds a network that is on by default', () => { + addNetwork(network) + + expect(networks.ethereum['137'].on).toBe(true) + }) + + it('adds a network with the correct primary RPC', () => { + network.primaryRpc = 'https://polygon-rpc.com' + + addNetwork(network) + + expect(networks.ethereum['137'].primaryRpc).toBeUndefined() + expect(networks.ethereum['137'].connection.primary.custom).toBe('https://polygon-rpc.com') + }) + + it('adds a network with the correct secondary RPC', () => { + network.secondaryRpc = 'https://rpc-mainnet.matic.network' + + addNetwork(network) + + expect(networks.ethereum['137'].secondaryRpc).toBeUndefined() + expect(networks.ethereum['137'].connection.secondary.custom).toBe( 'https://rpc-mainnet.matic.network') + }) + + it('adds a network with the correct default connection presets', () => { + addNetwork(network) + + expect(networks.ethereum['137'].connection.presets).toEqual({ local: 'direct' }) + }) + + it('adds a network with the correct default primary connection settings', () => { + addNetwork(network) + + expect(networks.ethereum['137'].connection.primary).toEqual({ + on: true, + current: 'custom', + status: 'loading', + connected: false, + type: '', + network: '', + custom: '' + }) + }) + + it('adds a network with the correct default secondary connection settings', () => { + addNetwork(network) + + expect(networks.ethereum['137'].connection.secondary).toEqual({ + on: false, + current: 'custom', + status: 'loading', + connected: false, + type: '', + network: '', + custom: '' + }) + }) + + it('adds a network with the correct default gas settings', () => { + addNetwork(network) + + expect(networks.ethereum['137'].gas).toEqual({ + price: { + selected: 'standard', + levels: { slow: '', standard: '', fast: '', asap: '', custom: '' } + } + }) + }) + + it('adds a network with the correct default metadata', () => { + addNetwork(network) + + expect(networksMeta.ethereum['137']).toEqual({ + gas: { + price: { + selected: 'standard', + levels: { slow: '', standard: '', fast: '', asap: '', custom: '' } + } + } + }) + }) + + it('does not add the network if id is not a parseable number', () => { + addNetwork({ ...network, id: 'test' }) + + expect(Object.keys(networks.ethereum)).toHaveLength(0) + expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) + }) + + it('does not add the network if name is not defined', () => { + addNetwork({ ...network, name: undefined }) + + expect(Object.keys(networks.ethereum)).toHaveLength(0) + expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) + }) + + it('does not add the network if explorer is not defined', () => { + addNetwork({ ...network, explorer: undefined }) + + expect(Object.keys(networks.ethereum)).toHaveLength(0) + expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) + }) + + it('does not add the network if symbol is not defined', () => { + addNetwork({ ...network, symbol: undefined }) + + expect(Object.keys(networks.ethereum)).toHaveLength(0) + expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) + }) + + it('does not add the network if type is not a string', () => { + addNetwork({ ...network, type: 2 }) + + expect(Object.keys(networks.ethereum)).toHaveLength(0) + expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) + }) + + it('does not add the network if type is not "ethereum"', () => { + addNetwork({ ...network, type: 'solana' }) + + expect(Object.keys(networks.ethereum)).toHaveLength(0) + expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) + }) + + it('does not add the network if the networks already exists', () => { + networks.ethereum['137'] = { ...network } + + addNetwork({ + id: 137, + type: 'ethereum', + name: 'Matic v1', + explorer: 'https://rpc-mainnet.maticvigil.com', + symbol: 'MATIC' + }) + + expect(networks.ethereum['137'].name).toBe('Polygon') + expect(networks.ethereum['137'].explorer).toBe('https://polygonscan.com') + }) +}) \ No newline at end of file From e831a7b6ac5449d4c7e2c82331a9bc6668ccd48f Mon Sep 17 00:00:00 2001 From: Matt Holtzman Date: Sat, 9 Oct 2021 11:00:54 -0400 Subject: [PATCH 2/3] update formatting --- test/main/store/actions/index.test.js | 60 +++++++++++++-------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/test/main/store/actions/index.test.js b/test/main/store/actions/index.test.js index 973d8d2ea..2b5604b5d 100644 --- a/test/main/store/actions/index.test.js +++ b/test/main/store/actions/index.test.js @@ -1,14 +1,7 @@ import { addNetwork as addNetworkAction } from '../../../../main/store/actions' describe('#addNetwork', () => { - const updaterFn = (node, update) => { - if (node !== 'main') throw new Error(`attempted to update wrong node: ${node}`) - update({ networks, networksMeta }) - } - - const addNetwork = network => addNetworkAction(updaterFn, network) - - const network = { + const polygonNetwork = { id: 137, name: 'Polygon', type: 'ethereum', @@ -19,73 +12,80 @@ describe('#addNetwork', () => { let networks, networksMeta + const updaterFn = (node, update) => { + if (node !== 'main') throw new Error(`attempted to update wrong node: ${node}`) + update({ networks, networksMeta }) + } + + const addNetwork = network => addNetworkAction(updaterFn, network) + beforeEach(() => { networks = { ethereum: {} } networksMeta = { ethereum: {} } }) it('adds a network with the correct id', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].id).toBe(137) }) it('adds a network with the correct id if the id is a number represented as a string', () => { - addNetwork({ ...network, id: '137' }) + addNetwork({ ...polygonNetwork, id: '137' }) expect(networks.ethereum['137'].id).toBe(137) }) it('adds a network with the correct name', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].name).toBe('Polygon') }) it('adds a network with the correct symbol', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].symbol).toBe('MATIC') }) it('adds a network with the correct explorer', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].explorer).toBe('https://polygonscan.com') }) it('adds a network that is on by default', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].on).toBe(true) }) it('adds a network with the correct primary RPC', () => { - network.primaryRpc = 'https://polygon-rpc.com' + polygonNetwork.primaryRpc = 'https://polygon-rpc.com' - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].primaryRpc).toBeUndefined() expect(networks.ethereum['137'].connection.primary.custom).toBe('https://polygon-rpc.com') }) it('adds a network with the correct secondary RPC', () => { - network.secondaryRpc = 'https://rpc-mainnet.matic.network' + polygonNetwork.secondaryRpc = 'https://rpc-mainnet.matic.network' - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].secondaryRpc).toBeUndefined() expect(networks.ethereum['137'].connection.secondary.custom).toBe( 'https://rpc-mainnet.matic.network') }) it('adds a network with the correct default connection presets', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].connection.presets).toEqual({ local: 'direct' }) }) it('adds a network with the correct default primary connection settings', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].connection.primary).toEqual({ on: true, @@ -99,7 +99,7 @@ describe('#addNetwork', () => { }) it('adds a network with the correct default secondary connection settings', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].connection.secondary).toEqual({ on: false, @@ -113,7 +113,7 @@ describe('#addNetwork', () => { }) it('adds a network with the correct default gas settings', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networks.ethereum['137'].gas).toEqual({ price: { @@ -124,7 +124,7 @@ describe('#addNetwork', () => { }) it('adds a network with the correct default metadata', () => { - addNetwork(network) + addNetwork(polygonNetwork) expect(networksMeta.ethereum['137']).toEqual({ gas: { @@ -137,49 +137,49 @@ describe('#addNetwork', () => { }) it('does not add the network if id is not a parseable number', () => { - addNetwork({ ...network, id: 'test' }) + addNetwork({ ...polygonNetwork, id: 'test' }) expect(Object.keys(networks.ethereum)).toHaveLength(0) expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) }) it('does not add the network if name is not defined', () => { - addNetwork({ ...network, name: undefined }) + addNetwork({ ...polygonNetwork, name: undefined }) expect(Object.keys(networks.ethereum)).toHaveLength(0) expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) }) it('does not add the network if explorer is not defined', () => { - addNetwork({ ...network, explorer: undefined }) + addNetwork({ ...polygonNetwork, explorer: undefined }) expect(Object.keys(networks.ethereum)).toHaveLength(0) expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) }) it('does not add the network if symbol is not defined', () => { - addNetwork({ ...network, symbol: undefined }) + addNetwork({ ...polygonNetwork, symbol: undefined }) expect(Object.keys(networks.ethereum)).toHaveLength(0) expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) }) it('does not add the network if type is not a string', () => { - addNetwork({ ...network, type: 2 }) + addNetwork({ ...polygonNetwork, type: 2 }) expect(Object.keys(networks.ethereum)).toHaveLength(0) expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) }) it('does not add the network if type is not "ethereum"', () => { - addNetwork({ ...network, type: 'solana' }) + addNetwork({ ...polygonNetwork, type: 'solana' }) expect(Object.keys(networks.ethereum)).toHaveLength(0) expect(Object.keys(networksMeta.ethereum)).toHaveLength(0) }) it('does not add the network if the networks already exists', () => { - networks.ethereum['137'] = { ...network } + networks.ethereum['137'] = { ...polygonNetwork } addNetwork({ id: 137, From 03ca224acb20bcd32dcac5e1facdf212dc846662 Mon Sep 17 00:00:00 2001 From: Matt Holtzman Date: Sat, 9 Oct 2021 11:01:56 -0400 Subject: [PATCH 3/3] add layer test --- test/main/store/actions/index.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/main/store/actions/index.test.js b/test/main/store/actions/index.test.js index 2b5604b5d..bb9c7aeba 100644 --- a/test/main/store/actions/index.test.js +++ b/test/main/store/actions/index.test.js @@ -48,6 +48,12 @@ describe('#addNetwork', () => { expect(networks.ethereum['137'].symbol).toBe('MATIC') }) + it('adds a network with the correct layer', () => { + addNetwork(polygonNetwork) + + expect(networks.ethereum['137'].layer).toBe('sidechain') + }) + it('adds a network with the correct explorer', () => { addNetwork(polygonNetwork)