1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00

Using expect in jest unit tests under /shared (#11413)

This commit is contained in:
ryanml 2021-06-29 10:50:18 -07:00 committed by GitHub
parent 844a021fcd
commit e2882792b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 40 deletions

View File

@ -1,4 +1,3 @@
import { strict as assert } from 'assert';
import BN from 'bn.js';
import { toBuffer } from './buffer-utils';
@ -6,64 +5,64 @@ describe('buffer utils', function () {
describe('toBuffer', function () {
it('should work with prefixed hex strings', function () {
const result = toBuffer('0xe');
assert.equal(result.length, 1);
expect(result).toHaveLength(1);
});
it('should work with non prefixed hex strings', function () {
const result = toBuffer('e');
assert.equal(result.length, 1);
expect(result).toHaveLength(1);
});
it('should work with weirdly 0x prefixed non-hex strings', function () {
const result = toBuffer('0xtest');
assert.equal(result.length, 6);
expect(result).toHaveLength(6);
});
it('should work with regular strings', function () {
const result = toBuffer('test');
assert.equal(result.length, 4);
expect(result).toHaveLength(4);
});
it('should work with BN', function () {
const result = toBuffer(new BN(100));
assert.equal(result.length, 1);
expect(result).toHaveLength(1);
});
it('should work with Buffer', function () {
const result = toBuffer(Buffer.from('test'));
assert.equal(result.length, 4);
expect(result).toHaveLength(4);
});
it('should work with a number', function () {
const result = toBuffer(100);
assert.equal(result.length, 1);
expect(result).toHaveLength(1);
});
it('should work with null or undefined', function () {
const result = toBuffer(null);
const result2 = toBuffer(undefined);
assert.equal(result.length, 0);
assert.equal(result2.length, 0);
expect(result).toHaveLength(0);
expect(result2).toHaveLength(0);
});
it('should work with UInt8Array', function () {
const uint8 = new Uint8Array(2);
const result = toBuffer(uint8);
assert.equal(result.length, 2);
expect(result).toHaveLength(2);
});
it('should work with objects that have a toBuffer property', function () {
const result = toBuffer({
toBuffer: () => Buffer.from('hi'),
});
assert.equal(result.length, 2);
expect(result).toHaveLength(2);
});
it('should work with objects that have a toArray property', function () {
const result = toBuffer({
toArray: () => ['hi'],
});
assert.equal(result.length, 1);
expect(result).toHaveLength(1);
});
});
});

View File

@ -1,6 +1,4 @@
import { strict as assert } from 'assert';
import nock from 'nock';
import { MILLISECOND, SECOND } from '../constants/time';
import getFetchWithTimeout from './fetch-with-timeout';
@ -12,7 +10,7 @@ describe('getFetchWithTimeout', function () {
const response = await (
await fetchWithTimeout('https://api.infura.io/money')
).json();
assert.deepEqual(response, {
expect(response).toStrictEqual({
hodl: false,
});
});
@ -25,14 +23,14 @@ describe('getFetchWithTimeout', function () {
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
try {
const fetchWithTimeoutThrowsError = async () => {
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
r.json(),
);
assert.fail('Request should throw');
} catch (e) {
assert.ok(e);
}
throw new Error('Request should throw');
};
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow('Aborted');
});
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);
try {
const fetchWithTimeoutThrowsError = async () => {
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
r.json(),
);
assert.fail('Request should be aborted');
} catch (e) {
assert.deepEqual(e.message, 'Aborted');
}
throw new Error('Request should be aborted');
};
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow('Aborted');
});
it('throws on invalid timeout', async function () {
assert.throws(() => getFetchWithTimeout(), 'should throw');
assert.throws(() => getFetchWithTimeout(-1), 'should throw');
assert.throws(() => getFetchWithTimeout({}), 'should throw');
assert.throws(() => getFetchWithTimeout(true), 'should throw');
expect(() => getFetchWithTimeout()).toThrow(
'Must specify positive integer timeout.',
);
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.',
);
});
});

View File

@ -1,4 +1,3 @@
import { strict as assert } from 'assert';
import { toChecksumAddress } from 'ethereumjs-util';
import { isValidHexAddress } from './hexstring-utils';
@ -7,51 +6,51 @@ describe('hexstring utils', function () {
it('should allow 40-char non-prefixed hex', function () {
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825';
const result = isValidHexAddress(address);
assert.equal(result, true);
expect(result).toBe(true);
});
it('should allow 42-char prefixed hex', function () {
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825';
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 () {
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825';
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 () {
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85';
const result = isValidHexAddress(address);
assert.equal(result, false);
expect(result).toBe(false);
});
it('should NOT allow less than 42 character hex-prefixed string', function () {
const address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85';
const result = isValidHexAddress(address);
assert.equal(result, false);
expect(result).toBe(false);
});
it('should recognize correct capitalized checksum', function () {
const address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825';
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
assert.equal(result, true);
expect(result).toBe(true);
});
it('should recognize incorrect capitalized checksum', function () {
const address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825';
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
assert.equal(result, false);
expect(result).toBe(false);
});
it('should recognize this sample hashed address', function () {
const address = '0x5Fda30Bb72B8Dfe20e48A00dFc108d0915BE9Bb0';
const result = isValidHexAddress(address, { mixedCaseUseChecksum: true });
const hashed = toChecksumAddress(address.toLowerCase());
assert.equal(hashed, address);
assert.equal(result, true);
expect(hashed).toBe(address);
expect(result).toBe(true);
});
});
});