-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.test.js
More file actions
104 lines (103 loc) · 4.34 KB
/
Copy pathblockchain.test.js
File metadata and controls
104 lines (103 loc) · 4.34 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Block = require("./block");
const Blockchain = require("./blockchain");
describe("Blockchain", () => {
let blockchain, newBlockchain, originalChain;
beforeEach(() => {
blockchain = new Blockchain();
newBlockchain = new Blockchain();
});
it("Contains a `chain` Array instance", () => {
expect(blockchain.chain instanceof Array).toEqual(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 doesn't start with genesis block", () => {
it("Returns false", () => {
blockchain.chain[0] = {data: "fake-genesis"};
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe("When the chain start with genesis block and has multiple blocks", () => {
beforeEach(() => {
blockchain.addBlock({data: "one"});
blockchain.addBlock({data: "two"});
blockchain.addBlock({data: "three"});
});
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 = "changed data";
expect(Blockchain.isValidChain(blockchain.chain)).toBe(false);
});
});
describe("And the chain doesn't contain any invalid blocks", () => {
it("Returns true", () => {
expect(Blockchain.isValidChain(blockchain.chain)).toBe(true);
});
});
});
});
describe("replaceChain()", () => {
let errorMock, logMock;
beforeEach(() => {
errorMock = jest.spyOn(console, "error").mockImplementation();
logMock = jest.spyOn(console, "log").mockImplementation();
});
describe("When the new chain is not longer", () => {
beforeEach(() => {
blockchain.addBlock({data: "one"});
originalChain = JSON.parse(JSON.stringify(blockchain.chain));
blockchain.replaceChain(newBlockchain.chain);
});
it("Does not replace the chain", () => {
expect(blockchain.chain).toEqual(originalChain);
});
it("Logs an error", () => {
expect(errorMock).toHaveBeenCalled();
});
});
describe("When the new chain is longer", () => {
beforeEach(() => {
newBlockchain.addBlock({data: "one"});
newBlockchain.addBlock({data: "two"});
newBlockchain.addBlock({data: "three"});
originalChain = JSON.parse(JSON.stringify(blockchain.chain));
});
describe("And the new chain is invalid", () => {
beforeEach(() => {
newBlockchain.chain[2].hash = "fake-hash";
blockchain.replaceChain(newBlockchain.chain);
});
it("Does not replace the chain", () => {
expect(blockchain.chain).toEqual(originalChain);
});
it("Logs an error", () => {
expect(errorMock).toHaveBeenCalledWith("Received chain is not valid.");
});
});
describe("And the new chain is valid", () => {
beforeEach(() => {
blockchain.replaceChain(newBlockchain.chain);
})
it("Replaces the chain", () => {
expect(blockchain.chain).toEqual(newBlockchain.chain);
});
it("Logs a message", () => {
expect(logMock).toHaveBeenCalled();
});
});
});
});
});