Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
add fake timers to enable timestamp advancing (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Lincoln <nkl199@yahoo.co.uk>
  • Loading branch information
nklincoln committed Nov 7, 2017
1 parent 9970cc0 commit ca5f1be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/perishable-network/package.json
Expand Up @@ -44,7 +44,8 @@
"license-check": "^1.1.5",
"mkdirp": "^0.5.1",
"mocha": "^3.2.0",
"moment": "^2.17.1"
"moment": "^2.17.1",
"sinon": "2.3.8"
},
"license-check-config": {
"src": [
Expand Down
15 changes: 12 additions & 3 deletions packages/perishable-network/test/perishable.js
Expand Up @@ -21,12 +21,14 @@ const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefi
const path = require('path');

require('chai').should();
let sinon = require('sinon');

const bfs_fs = BrowserFS.BFSRequire('fs');
const NS = 'org.acme.shipping.perishable';
let grower_id = 'farmer@email.com';
let importer_id = 'supermarket@email.com';
let factory;
let clock;

describe('Perishable Shipping Network', () => {

Expand Down Expand Up @@ -60,6 +62,14 @@ describe('Perishable Shipping Network', () => {
});
});

beforeEach(function () {
clock = sinon.useFakeTimers();
});

afterEach(function () {
clock.restore();
});

describe('#shipment', () => {

it('should receive base price for a shipment within temperature range', () => {
Expand Down Expand Up @@ -112,14 +122,13 @@ describe('Perishable Shipping Network', () => {
const tempReading = factory.newTransaction(NS, 'TemperatureReading');
tempReading.shipment = factory.newRelationship(NS, 'Shipment', 'SHIP_001');
tempReading.centigrade = 4.5;
// advance the javascript clock to create a time-advanced test timestamp
clock.tick(1000000000000000);
return businessNetworkConnection.submitTransaction(tempReading)
.then(() => {
// submit the shipment received
const received = factory.newTransaction(NS, 'ShipmentReceived');
received.shipment = factory.newRelationship(NS, 'Shipment', 'SHIP_001');
const late = new Date();
late.setDate(late.getDate() + 2);
received.timestamp = late;
return businessNetworkConnection.submitTransaction(received);
})
.then(() => {
Expand Down

0 comments on commit ca5f1be

Please sign in to comment.