-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add item, parser and woolworths tests
- Loading branch information
Showing
9 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,8 @@ cache: | |
directories: | ||
- "node_modules" | ||
|
||
script: | ||
- npm run test-with-coverage | ||
|
||
after_success: | ||
- npm run coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as fs from 'fs'; | ||
import * as cheerio from 'cheerio'; | ||
import { assert } from 'chai'; | ||
|
||
import { DefaultCatalogueParser } from '../../src/api/CatalogueParser'; | ||
import Category from '../../src/api/Category'; | ||
|
||
describe('DefaultCategoryParser', () => { | ||
describe('#parseCategoryList()', () => { | ||
it('should parse correctly', () => { | ||
const content = fs.readFileSync('test/api/testdata/category-list.html').toString(); | ||
const $ = cheerio.load(content); | ||
|
||
const parser = new DefaultCatalogueParser(); | ||
const categories = parser.parseCategoryList($); | ||
|
||
assert.deepEqual(categories, [ | ||
new Category('Bread & Bakery', [563], []), | ||
new Category('Pantry', [533, 534, 535, 537, 538, 548], []), | ||
]); | ||
}); | ||
}); | ||
|
||
describe('#parseItem()', () => { | ||
it('should parse correctly', () => { | ||
const content = fs.readFileSync('test/api/testdata/item.html').toString(); | ||
const $ = cheerio.load(content); | ||
|
||
const parser = new DefaultCatalogueParser(); | ||
const item = parser.parseItem($.root()); | ||
|
||
assert.equal(item.name, 'Doritos Corn Chips 150g-170g'); | ||
assert.equal(item.regularPrice, 3.30); | ||
assert.equal(item.salePrice, 1.65); | ||
assert.equal(item.priceText, 'Was $3.30, Save $1.65'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { assert } from 'chai'; | ||
import Item from '../../src/api/Item'; | ||
|
||
describe('Item', () => { | ||
describe('#getDiscount()', () => { | ||
it('calculates the discount correctly', () => { | ||
const item = new Item('Item 1', 10, 5, 1, ''); | ||
assert.equal(item.getDiscount(), 5); | ||
}); | ||
}); | ||
|
||
describe('#getDiscountPercent()', () => { | ||
it('calculates the discount perecentage correctly', () => { | ||
const item = new Item('Item 1', 10, 5, 1, ''); | ||
assert.equal(item.getDiscountPercent(), 0.5); | ||
}); | ||
}); | ||
|
||
describe('#getDiscountPercentFormatted()', () => { | ||
it('formats the discount percentage correctly', () => { | ||
const item = new Item('Item 1', 15, 10, 1, ''); | ||
assert.equal(item.getDiscountPercentFormatted(), '33%'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<div id="sf-salenavbar-container"> | ||
<ul id="sf-salenavbar"> | ||
<li><a id="sf-twopage" title="Double Page View" href="#view=catalogue2&saleId=15262&areaName=c-nsw-met" ></a></li> | ||
<li><a id="sf-singlepage" title="Single Page View" href="#view=catalogue&saleId=15262&areaName=c-nsw-met" ></a></li> | ||
<li><a id="sf-thumbpage" title="Thumbnail Page View" href="#view=pagethumb&saleId=15262&areaName=c-nsw-met" ></a></li> | ||
<li><a id="sf-navpages" title="Catalogue Page View" role="presentation" href="#view=catalogue2&saleId=15262&areaName=c-nsw-met" >Pages</a></li> | ||
<li><a id="sf-navlist" title="Product List View" href="#view=list&saleId=15262&areaName=c-nsw-met" >Product List</a></li> | ||
<li id="sf-navcategory-container"> | ||
<a id="sf-navcategory-button" title="Category List" tabindex="0">Category</a> | ||
<ul id="sf-navcategories"> | ||
<li class="sf-navcategory"><a class="sf-navcategory-link" title="Bread & Bakery" href="#view=category&saleId=15262&categoryId=563&areaName=c-nsw-met">Bread & Bakery</a></li>></li> | ||
<li class="sf-navcategory"><a class="sf-navcategory-link" title="Pantry" href="#view=category&saleId=15262&categoryId=533,534,535,537,538,548&areaName=c-nsw-met">Pantry</a></li> | ||
</ul> | ||
</li> | ||
<li id="sf-navsearch-box"> | ||
<input id="sf-keyword" type="text" tabindex="0" placeholder="search catalogues" onkeypress="if(event.keyCode == 13){jsf('#sf-navsearch').click();}"></input> | ||
<a id="sf-navsearch" role="presentation" aria-hidden="true"></a> | ||
</li> | ||
<li> | ||
<a id="sf-email-action" title="Email Catalogue" class="sf-catalogue-action" href="//embed.salefinder.com.au/catalogue/catalogueemail/15262/"> | ||
<div class="sf-email-icon"></div> | ||
<span>Email</span> | ||
</a> | ||
</li> | ||
<li id="sf-catalogue-print-container"> | ||
<a id="sf-print-action" title="Print Catalogue" class="sf-catalogue-action" href="https://embed.salefinder.com.au/catalogue/catalogueprint/15262/?all=1"> | ||
<div class="sf-print-icon"></div> | ||
<span>Print</span> | ||
</a> | ||
<ul id="sf-catalogue-print-options"> | ||
<li><a id="sf-catalogue-print-left" title="Print Left Catalogue Page" class="sf-catalogue-print-option" target="_blank" href="https://embed.salefinder.com.au/catalogue/catalogueprint/15262/?pagenum=">Left Page</a></li> | ||
<li><a id="sf-catalogue-print-right" title="Print Right Catalogue Page" class="sf-catalogue-print-option" target="_blank" href="https://embed.salefinder.com.au/catalogue/catalogueprint/15262/?pagenum=">Right Page</a></li> | ||
<li><a id="sf-catalogue-print-all" title="Print All Catalogue Pages" class="sf-catalogue-print-option" target="_blank" href="https://embed.salefinder.com.au/catalogue/catalogueprint/15262/?all=1">All</a></li> | ||
</ul> | ||
</li> | ||
</ul> | ||
<ul id="sf-navbarcart-container"> | ||
<li><div id="sf-navbar-checkout-items">no items</div></li> | ||
<li><a id="sf-navbar-checkout-icon" title="Shopping List Page" href="#view=cart&saleId=15262&areaName=c-nsw-met">Your Shopping List</a></li> | ||
</ul> | ||
<ul id="sf-salepage-sort-container"> | ||
<li><label for="sf-navbar-sort">Select option to sort results</label><select id="sf-navbar-sort" name="sort"> | ||
<option value="">Sort By</option> | ||
<option value="lowestPrice">Lowest Price</option> | ||
<option value="highestPrice">Highest Price</option> | ||
<option value="itemName">Name</option> | ||
</select></li> | ||
</ul> | ||
<a id="sf-printshortlist-action" href="#"><span id="sf-printshortlist-ico"></span>Print List</a> | ||
<a id="sf-emailshortlist-action" href="#"><span id="sf-emailshortlist-ico"></span>Email List</a> | ||
</div> | ||
<div id="sf-content" tabindex="-1"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<td class="sf-item mid"> | ||
<div class="sf-item-container"> | ||
<h4> | ||
<a | ||
title="Doritos Corn Chips 150g-170g" | ||
href="" | ||
class="sf-item-heading" | ||
data-itemid="225461004" | ||
>Doritos Corn Chips 150g-170g</a> | ||
</h4> | ||
<a | ||
href="" | ||
title="Doritos Corn Chips 150g-170g" | ||
class="sf-item-image" | ||
data-itemid="225461004" | ||
aria-hidden="true" | ||
role="presentation" | ||
tabindex="-1" | ||
><img src="https://d2g5na3xotdfpc.cloudfront.net/images/products/160x170/225461004.jpg?ts=1527662023"/></a> | ||
<div class="sf-item-details"> | ||
<div class="sf-price-options"> | ||
<span class="sf-regoption">Was $3.30, Save <span class="sf-regprice">$1.65</span><span class="sf-optionsuffix"></span></span> | ||
<br/> | ||
<span class="sf-nowprice"> | ||
<span class="sf-pricedisplay">$1.65</span> <span class="sf-optionsuffix">each</span> | ||
</span> | ||
<br/> | ||
</div> | ||
<div class="links-area"> | ||
<div class="sale-dates">Offer valid Wed 6 Jun - Tue 12 Jun 2018</div> | ||
</div> | ||
</div> | ||
</div> | ||
</td> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { assert } from 'chai'; | ||
|
||
import Item from '../../src/api/Item'; | ||
import SaleFinderAPI from '../../src/api'; | ||
import WoolworthsCatalogue from '../../src/catalogue/WoolworthsCatalogue'; | ||
|
||
describe('WoolworthsCatalogue', () => { | ||
describe('#itemIsHalfPrice()', () => { | ||
it('should return true for an item from the current catalogue with the text `1/2 Price`', () => { | ||
const item = new Item('Item 1', 10, 5, 1, '1/2 Price'); | ||
const catalogue = new WoolworthsCatalogue(new SaleFinderAPI(), 1); | ||
assert.isTrue(catalogue.itemIsHalfPrice(item)); | ||
}); | ||
|
||
it('should return false if the item does not have the text `1/2 Price`', () => { | ||
const item = new Item('Item 1', 10, 5, 1, ''); | ||
const catalogue = new WoolworthsCatalogue(new SaleFinderAPI(), 1); | ||
assert.isFalse(catalogue.itemIsHalfPrice(item)); | ||
}); | ||
|
||
it('should return false for an item from a different catalogue', () => { | ||
const item = new Item('Item 1', 10, 5, 1, '1/2 Price'); | ||
const catalogue = new WoolworthsCatalogue(new SaleFinderAPI(), 2); | ||
assert.isFalse(catalogue.itemIsHalfPrice(item)); | ||
}); | ||
}); | ||
}); |