-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.test.js
64 lines (52 loc) · 1.66 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
54
55
56
57
58
59
60
61
62
63
64
const Block = require("./block");
const { GENESIS_DATA } = require("./config");
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, hash 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();
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.mineBlock({ 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).toEqual(data);
});
it("sets a `timestamp`", () => {
expect(minedBlock.timestamp).not.toEqual(undefined);
});
it("creates a SHA-256 `hash` on the proper inputs", () => {
expect(minedBlock.hash).toEqual(
cryptoHash(minedBlock.timestamp, lastBlock.hash, data)
);
});
});
});