Skip to content

Commit

Permalink
added productList init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schaechinger committed Mar 26, 2018
1 parent e128072 commit 047ed9c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/ProductList/ProductList.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class ProductList {
});
}

/**
* Initializes the product list with the cached data or latest ones if missing
* or outdated.
*
* @returns {Promise.<boolean>}
*/
init() {
return this._temp.get()
.then(data => {
Expand All @@ -89,7 +95,7 @@ class ProductList {
return this.loadProducts();
}
else {
return true;
return Promise.resolve(true);
}
});
}
Expand All @@ -99,6 +105,7 @@ class ProductList {
*
* @param {Object} products - The raw data of the product list with meta
* information.
* @returns {boolean}
*/
_parseData(products) {
if (!products) {
Expand Down
67 changes: 67 additions & 0 deletions test/ProductList/ProductList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const ProductList = require('../../lib/ProductList');

describe('ProductList', () => {
describe('init', () => {
const productList = new ProductList({ client: {} });

productList.loadProducts = sinon.stub().returns(Promise.resolve('LOADING'));

const data = {
date: '2018-03-01',
salesProductList: {
SalesProduct: []
}
};

beforeEach(() => {
productList.loadProducts.resetHistory();
});

it('should trigger load products if data are not available', done => {
productList._temp = {
get: sinon.stub().returns(Promise.resolve(''))
};

productList.init()
.then(result => {
productList._temp.get.calledOnce.should.be.true();
productList.loadProducts.calledOnce.should.be.true();

result.should.equal('LOADING');

done();
});
});

it('should load products if data are too old', done => {
productList._temp = {
get: sinon.stub().returns(Promise.resolve(JSON.stringify(data)))
};

productList.init()
.then(result => {
productList._temp.get.calledOnce.should.be.true();
productList.loadProducts.calledOnce.should.be.true();

result.should.equal('LOADING');

done();
});
});

xit('should take data if available', done => {
// TODO: manipulate date
productList._temp = {
get: sinon.stub().returns(Promise.resolve(JSON.stringify(data)))
};

productList.init()
.then(result => {
productList._temp.get.calledOnce.should.be.true();
productList.loadProducts.calledOnce.should.be.false();

done();
});
});
});
});

0 comments on commit 047ed9c

Please sign in to comment.