mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-25 03:20:23 +01:00
Using expect in jest unit tests under /shared (#11413)
This commit is contained in:
parent
844a021fcd
commit
e2882792b8
@ -1,4 +1,3 @@
|
|||||||
import { strict as assert } from 'assert';
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import { toBuffer } from './buffer-utils';
|
import { toBuffer } from './buffer-utils';
|
||||||
|
|
||||||
@ -6,64 +5,64 @@ describe('buffer utils', function () {
|
|||||||
describe('toBuffer', function () {
|
describe('toBuffer', function () {
|
||||||
it('should work with prefixed hex strings', function () {
|
it('should work with prefixed hex strings', function () {
|
||||||
const result = toBuffer('0xe');
|
const result = toBuffer('0xe');
|
||||||
assert.equal(result.length, 1);
|
expect(result).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with non prefixed hex strings', function () {
|
it('should work with non prefixed hex strings', function () {
|
||||||
const result = toBuffer('e');
|
const result = toBuffer('e');
|
||||||
assert.equal(result.length, 1);
|
expect(result).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with weirdly 0x prefixed non-hex strings', function () {
|
it('should work with weirdly 0x prefixed non-hex strings', function () {
|
||||||
const result = toBuffer('0xtest');
|
const result = toBuffer('0xtest');
|
||||||
assert.equal(result.length, 6);
|
expect(result).toHaveLength(6);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with regular strings', function () {
|
it('should work with regular strings', function () {
|
||||||
const result = toBuffer('test');
|
const result = toBuffer('test');
|
||||||
assert.equal(result.length, 4);
|
expect(result).toHaveLength(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with BN', function () {
|
it('should work with BN', function () {
|
||||||
const result = toBuffer(new BN(100));
|
const result = toBuffer(new BN(100));
|
||||||
assert.equal(result.length, 1);
|
expect(result).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with Buffer', function () {
|
it('should work with Buffer', function () {
|
||||||
const result = toBuffer(Buffer.from('test'));
|
const result = toBuffer(Buffer.from('test'));
|
||||||
assert.equal(result.length, 4);
|
expect(result).toHaveLength(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with a number', function () {
|
it('should work with a number', function () {
|
||||||
const result = toBuffer(100);
|
const result = toBuffer(100);
|
||||||
assert.equal(result.length, 1);
|
expect(result).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with null or undefined', function () {
|
it('should work with null or undefined', function () {
|
||||||
const result = toBuffer(null);
|
const result = toBuffer(null);
|
||||||
const result2 = toBuffer(undefined);
|
const result2 = toBuffer(undefined);
|
||||||
assert.equal(result.length, 0);
|
expect(result).toHaveLength(0);
|
||||||
assert.equal(result2.length, 0);
|
expect(result2).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with UInt8Array', function () {
|
it('should work with UInt8Array', function () {
|
||||||
const uint8 = new Uint8Array(2);
|
const uint8 = new Uint8Array(2);
|
||||||
const result = toBuffer(uint8);
|
const result = toBuffer(uint8);
|
||||||
assert.equal(result.length, 2);
|
expect(result).toHaveLength(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with objects that have a toBuffer property', function () {
|
it('should work with objects that have a toBuffer property', function () {
|
||||||
const result = toBuffer({
|
const result = toBuffer({
|
||||||
toBuffer: () => Buffer.from('hi'),
|
toBuffer: () => Buffer.from('hi'),
|
||||||
});
|
});
|
||||||
assert.equal(result.length, 2);
|
expect(result).toHaveLength(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with objects that have a toArray property', function () {
|
it('should work with objects that have a toArray property', function () {
|
||||||
const result = toBuffer({
|
const result = toBuffer({
|
||||||
toArray: () => ['hi'],
|
toArray: () => ['hi'],
|
||||||
});
|
});
|
||||||
assert.equal(result.length, 1);
|
expect(result).toHaveLength(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
import { strict as assert } from 'assert';
|
|
||||||
import nock from 'nock';
|
import nock from 'nock';
|
||||||
|
|
||||||
import { MILLISECOND, SECOND } from '../constants/time';
|
import { MILLISECOND, SECOND } from '../constants/time';
|
||||||
import getFetchWithTimeout from './fetch-with-timeout';
|
import getFetchWithTimeout from './fetch-with-timeout';
|
||||||
|
|
||||||
@ -12,7 +10,7 @@ describe('getFetchWithTimeout', function () {
|
|||||||
const response = await (
|
const response = await (
|
||||||
await fetchWithTimeout('https://api.infura.io/money')
|
await fetchWithTimeout('https://api.infura.io/money')
|
||||||
).json();
|
).json();
|
||||||
assert.deepEqual(response, {
|
expect(response).toStrictEqual({
|
||||||
hodl: false,
|
hodl: false,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -25,14 +23,14 @@ describe('getFetchWithTimeout', function () {
|
|||||||
|
|
||||||
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
|
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
|
||||||
|
|
||||||
try {
|
const fetchWithTimeoutThrowsError = async () => {
|
||||||
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
||||||
r.json(),
|
r.json(),
|
||||||
);
|
);
|
||||||
assert.fail('Request should throw');
|
throw new Error('Request should throw');
|
||||||
} catch (e) {
|
};
|
||||||
assert.ok(e);
|
|
||||||
}
|
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow('Aborted');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should abort the request when the custom timeout is hit', async function () {
|
it('should abort the request when the custom timeout is hit', async function () {
|
||||||
@ -43,20 +41,28 @@ describe('getFetchWithTimeout', function () {
|
|||||||
|
|
||||||
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
|
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
|
||||||
|
|
||||||
try {
|
const fetchWithTimeoutThrowsError = async () => {
|
||||||
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
||||||
r.json(),
|
r.json(),
|
||||||
);
|
);
|
||||||
assert.fail('Request should be aborted');
|
throw new Error('Request should be aborted');
|
||||||
} catch (e) {
|
};
|
||||||
assert.deepEqual(e.message, 'Aborted');
|
|
||||||
}
|
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow('Aborted');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws on invalid timeout', async function () {
|
it('throws on invalid timeout', async function () {
|
||||||
assert.throws(() => getFetchWithTimeout(), 'should throw');
|
expect(() => getFetchWithTimeout()).toThrow(
|
||||||
assert.throws(() => getFetchWithTimeout(-1), 'should throw');
|
'Must specify positive integer timeout.',
|
||||||
assert.throws(() => getFetchWithTimeout({}), 'should throw');
|
);
|
||||||
assert.throws(() => getFetchWithTimeout(true), 'should throw');
|
expect(() => getFetchWithTimeout(-1)).toThrow(
|
||||||
|
'Must specify positive integer timeout.',
|
||||||
|
);
|
||||||
|
expect(() => getFetchWithTimeout({})).toThrow(
|
||||||
|
'Must specify positive integer timeout.',
|
||||||
|
);
|
||||||
|
expect(() => getFetchWithTimeout(true)).toThrow(
|
||||||
|
'Must specify positive integer timeout.',
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { strict as assert } from 'assert';
|
|
||||||
import { toChecksumAddress } from 'ethereumjs-util';
|
import { toChecksumAddress } from 'ethereumjs-util';
|
||||||
import { isValidHexAddress } from './hexstring-utils';
|
import { isValidHexAddress } from './hexstring-utils';
|
||||||
|
|
||||||
@ -7,51 +6,51 @@ describe('hexstring utils', function () {
|
|||||||
it('should allow 40-char non-prefixed hex', function () {
|
it('should allow 40-char non-prefixed hex', function () {
|
||||||
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825';
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825';
|
||||||
const result = isValidHexAddress(address);
|
const result = isValidHexAddress(address);
|
||||||
assert.equal(result, true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow 42-char prefixed hex', function () {
|
it('should allow 42-char prefixed hex', function () {
|
||||||
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825';
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825';
|
||||||
const result = isValidHexAddress(address);
|
const result = isValidHexAddress(address);
|
||||||
assert.equal(result, true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT allow 40-char non-prefixed hex when allowNonPrefixed is false', function () {
|
it('should NOT allow 40-char non-prefixed hex when allowNonPrefixed is false', function () {
|
||||||
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825';
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825';
|
||||||
const result = isValidHexAddress(address, { allowNonPrefixed: false });
|
const result = isValidHexAddress(address, { allowNonPrefixed: false });
|
||||||
assert.equal(result, false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT allow any length of non hex-prefixed string', function () {
|
it('should NOT allow any length of non hex-prefixed string', function () {
|
||||||
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85';
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85';
|
||||||
const result = isValidHexAddress(address);
|
const result = isValidHexAddress(address);
|
||||||
assert.equal(result, false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT allow less than 42 character hex-prefixed string', function () {
|
it('should NOT allow less than 42 character hex-prefixed string', function () {
|
||||||
const address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85';
|
const address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85';
|
||||||
const result = isValidHexAddress(address);
|
const result = isValidHexAddress(address);
|
||||||
assert.equal(result, false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should recognize correct capitalized checksum', function () {
|
it('should recognize correct capitalized checksum', function () {
|
||||||
const address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825';
|
const address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825';
|
||||||
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
||||||
assert.equal(result, true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should recognize incorrect capitalized checksum', function () {
|
it('should recognize incorrect capitalized checksum', function () {
|
||||||
const address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825';
|
const address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825';
|
||||||
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
||||||
assert.equal(result, false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should recognize this sample hashed address', function () {
|
it('should recognize this sample hashed address', function () {
|
||||||
const address = '0x5Fda30Bb72B8Dfe20e48A00dFc108d0915BE9Bb0';
|
const address = '0x5Fda30Bb72B8Dfe20e48A00dFc108d0915BE9Bb0';
|
||||||
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
||||||
const hashed = toChecksumAddress(address.toLowerCase());
|
const hashed = toChecksumAddress(address.toLowerCase());
|
||||||
assert.equal(hashed, address);
|
expect(hashed).toBe(address);
|
||||||
assert.equal(result, true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user