Skip to content

Commit

Permalink
Chain Validation | Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Sep 27, 2022
1 parent b291dc2 commit f99b2fb
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion blockchain.test.js
Expand Up @@ -2,7 +2,11 @@ const Blockchain = require('./blockchain');
const Block = require('./block');

describe('Blockchain', () => {
const blockchain = new Blockchain();
let blockchain;

beforeEach(() => {
blockchain = new Blockchain();
});

it('contains a `chain` Array instance', () => {
expect(blockchain.chain instanceof Array).toBe(true);
Expand All @@ -18,4 +22,44 @@ describe('Blockchain', () => {

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);
});
});
});
});
});

0 comments on commit f99b2fb

Please sign in to comment.