|
| 1 | +import { Block } from '../../src/utils/provider'; |
| 2 | + |
| 3 | +describe('new Block()', () => { |
| 4 | + test('decimal number 0 BlockIdentifier', () => { |
| 5 | + const block = new Block(0); |
| 6 | + expect(block.identifier).toMatchObject({ block_number: 0 }); |
| 7 | + expect(block.queryIdentifier).toBe('blockNumber=0'); |
| 8 | + expect(block.hash).toBe(null); |
| 9 | + expect(block.number).toBe(0); |
| 10 | + expect(block.tag).toBe(null); |
| 11 | + }); |
| 12 | + |
| 13 | + test('decimal number 631581 BlockIdentifier', () => { |
| 14 | + const block1 = new Block(631581); |
| 15 | + expect(block1.identifier).toMatchObject({ block_number: 631581 }); |
| 16 | + expect(block1.queryIdentifier).toBe('blockNumber=631581'); |
| 17 | + expect(block1.hash).toBe(null); |
| 18 | + expect(block1.number).toBe(631581); |
| 19 | + expect(block1.tag).toBe(null); |
| 20 | + }); |
| 21 | + |
| 22 | + test('non-decimal number -1 BlockIdentifier', () => { |
| 23 | + expect(() => { |
| 24 | + const block = new Block(-1); |
| 25 | + return block; |
| 26 | + }).toThrow(TypeError); |
| 27 | + }); |
| 28 | + |
| 29 | + test('decimal string `0` BlockIdentifier', () => { |
| 30 | + const block = new Block('0'); |
| 31 | + expect(block.identifier).toMatchObject({ block_number: 0 }); |
| 32 | + expect(block.queryIdentifier).toBe('blockNumber=0'); |
| 33 | + expect(block.hash).toBe(null); |
| 34 | + expect(block.number).toBe(0); |
| 35 | + expect(block.tag).toBe(null); |
| 36 | + }); |
| 37 | + |
| 38 | + test('decimal string `631581` BlockIdentifier', () => { |
| 39 | + const block1 = new Block('631581'); |
| 40 | + expect(block1.identifier).toMatchObject({ block_number: 631581 }); |
| 41 | + expect(block1.queryIdentifier).toBe('blockNumber=631581'); |
| 42 | + expect(block1.hash).toBe(null); |
| 43 | + expect(block1.number).toBe(631581); |
| 44 | + expect(block1.tag).toBe(null); |
| 45 | + }); |
| 46 | + |
| 47 | + test('non-decimal string `-1` BlockIdentifier', () => { |
| 48 | + expect(() => { |
| 49 | + const block = new Block('-1'); |
| 50 | + return block; |
| 51 | + }).toThrow(TypeError); |
| 52 | + }); |
| 53 | + |
| 54 | + test('(Irregular support) hex string `0x0` BlockIdentifier.', () => { |
| 55 | + const block = new Block('0x0'); |
| 56 | + expect(block.identifier).toMatchObject({ block_hash: '0x0' }); |
| 57 | + expect(block.queryIdentifier).toBe('blockHash=0x0'); |
| 58 | + expect(block.hash).toBe('0x0'); |
| 59 | + expect(block.number).toBe(null); |
| 60 | + expect(block.tag).toBe(null); |
| 61 | + }); |
| 62 | + |
| 63 | + test('hex string `0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb` BlockIdentifier', () => { |
| 64 | + const block = new Block('0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb'); |
| 65 | + expect(block.identifier).toMatchObject({ |
| 66 | + block_hash: '0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb', |
| 67 | + }); |
| 68 | + expect(block.queryIdentifier).toBe( |
| 69 | + 'blockHash=0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb' |
| 70 | + ); |
| 71 | + expect(block.hash).toBe('0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb'); |
| 72 | + expect(block.number).toBe(null); |
| 73 | + expect(block.tag).toBe(null); |
| 74 | + }); |
| 75 | + |
| 76 | + test('BigInt 1100871802642964430494835386862140987097292376415056243504467124241116103854n BlockIdentifier', () => { |
| 77 | + const block = new Block( |
| 78 | + 1100871802642964430494835386862140987097292376415056243504467124241116103854n |
| 79 | + ); |
| 80 | + expect(block.identifier).toMatchObject({ |
| 81 | + block_hash: '0x26f12449d649a5339d4891b312a381f23ebc1106792d169b42e6646e87304ae', |
| 82 | + }); |
| 83 | + expect(block.queryIdentifier).toBe( |
| 84 | + 'blockHash=0x26f12449d649a5339d4891b312a381f23ebc1106792d169b42e6646e87304ae' |
| 85 | + ); |
| 86 | + expect(block.hash).toBe('0x26f12449d649a5339d4891b312a381f23ebc1106792d169b42e6646e87304ae'); |
| 87 | + expect(block.number).toBe(null); |
| 88 | + expect(block.tag).toBe(null); |
| 89 | + }); |
| 90 | + |
| 91 | + test('String BigInt `1100871802642964430494835386862140987097292376415056243504467124241116103854n` BlockIdentifier', () => { |
| 92 | + expect(() => { |
| 93 | + const block = new Block( |
| 94 | + '1100871802642964430494835386862140987097292376415056243504467124241116103854n' |
| 95 | + ); |
| 96 | + return block; |
| 97 | + }).toThrow(TypeError); |
| 98 | + }); |
| 99 | + |
| 100 | + test('string `pending` BlockIdentifier', () => { |
| 101 | + const block1 = new Block('pending'); |
| 102 | + expect(block1.identifier).toBe('pending'); |
| 103 | + expect(block1.queryIdentifier).toBe('blockNumber=pending'); |
| 104 | + expect(block1.hash).toBe(null); |
| 105 | + expect(block1.number).toBe(null); |
| 106 | + expect(block1.tag).toBe('pending'); |
| 107 | + }); |
| 108 | + |
| 109 | + test('string `latest` BlockIdentifier', () => { |
| 110 | + const block1 = new Block('latest'); |
| 111 | + expect(block1.identifier).toBe('latest'); |
| 112 | + expect(block1.queryIdentifier).toBe('blockNumber=latest'); |
| 113 | + expect(block1.hash).toBe(null); |
| 114 | + expect(block1.number).toBe(null); |
| 115 | + expect(block1.tag).toBe('latest'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('False string `supernova` BlockIdentifier', () => { |
| 119 | + expect(() => { |
| 120 | + const block = new Block('supernova'); |
| 121 | + return block; |
| 122 | + }).toThrow(TypeError); |
| 123 | + }); |
| 124 | + |
| 125 | + test('null BlockIdentifier', () => { |
| 126 | + const block1 = new Block(null); |
| 127 | + expect(block1.identifier).toBe('pending'); |
| 128 | + expect(block1.queryIdentifier).toBe('blockNumber=pending'); |
| 129 | + expect(block1.hash).toBe(null); |
| 130 | + expect(block1.number).toBe(null); |
| 131 | + expect(block1.tag).toBe('pending'); |
| 132 | + }); |
| 133 | +}); |
0 commit comments