-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.test.js
62 lines (51 loc) · 1.92 KB
/
blockchain.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
const Blockchain = require("./blockchain");
const Block = require("./block");
describe("Blockchain", () => {
let blockchain;
beforeEach(() => {
blockchain = new Blockchain();
});
it("contains a `chain` Array instance", () => {
expect(blockchain.chain instanceof Array).toBe(true);
});
it("starts with the genesis block", () => {
expect(blockchain.chain[0]).toEqual(Block.genesis());
});
it("adds a new block to the chain", () => {
const newData = "foo bar";
blockchain.addBlock({ data: newData });
expect(blockchain.chain[blockchain.chain.length - 1].data).toEqual(newData);
});
describe("isValidChain()", () => {
describe("when the chain does not start with the genesis block", () => {
it("returns false", () => {
blockchain.chain[0] = { data: "fake-genesis" };
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe("when the chain starts with the genesis block and has multiple blocks", () => {
beforeEach(() => {
blockchain.addBlock({ data: "Bears" });
blockchain.addBlock({ data: "Beets" });
blockchain.addBlock({ data: "Battlestar Galactica" });
});
describe("and a lastHash reference has changed", () => {
it("returns false", () => {
blockchain.chain[2].lastHash = "broken-lastHash";
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe("and the chain contains a block with an invalid field", () => {
it("returns false", () => {
blockchain.chain[2].data = "some-bad-and-evil-data";
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe("and the chain does not contain any invalid blocks", () => {
it("returns true", () => {
expect(Blockchain.isValidChain(blockchain.chain)).toBe(true);
});
});
});
});
});