-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.test.js
53 lines (46 loc) · 1.75 KB
/
block.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { GENESIS_DATA } = require('./config');
const Block = require('./block');
const cryptoHash = require('./crypto-hash');
describe('Block', () => {
const timestamp = 'a-date';
const lastHash = 'foo-hash';
const hash = 'bar-hash';
const data = ['blockchain','data'];
const block = new Block({ timestamp, lastHash, hash, data });
it('has a timestamp, lastHash, has and data property', () => {
expect(block.timestamp).toEqual(timestamp);
expect(block.lastHash).toEqual(lastHash);
expect(block.hash).toEqual(hash);
expect(block.data).toEqual(data);
});
describe ('genesis()', () => {
const genesisBlock = Block.genesis();
console.log('genesisBlock',genesisBlock);
it ('returns a Block instance', () => {
expect(genesisBlock instanceof Block).toBe(true);
});
it('returns the genesis data', () => {
expect(genesisBlock).toEqual(GENESIS_DATA);
});
});
describe ('mineBlock()',() => {
const lastBlock = Block.genesis();
const data = 'mined data';
const minedBlock = Block.minedBlock({ lastBlock,data});
it('returns a Block instance', () => {
expect (minedBlock instanceof Block).toBe(true);
});
it('sets the "lastHash" to be the "hash" of the lastBlock', () => {
expect(minedBlock.lastHash).toEqual(lastBlock.hash);
});
it('sets the "data" ', () => {
expect(minedBlock.data).not.toEqual(undefined);
});
it('sets the "timestamp" ', () => {
expect(minedBlock.timestamp).not.toEqual(undefined);
});
it('create a SHA-256 "hash" based on the proper inputs', () => {
expect(minedBlock.hash).toEqual(cryptoHash(minedBlock.timestamp,lastBlock.hash,data));
});
});
});