Skip to content

Commit

Permalink
test: add item, parser and woolworths tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szdc committed Jun 9, 2018
1 parent dd78a0d commit 1ec5629
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ insert_final_newline = true
indent_style = tab
indent_size = 4

[test/testdata/**.md]
[test/**/testdata/*]
insert_final_newline = false
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ cache:
directories:
- "node_modules"

script:
- npm run test-with-coverage

after_success:
- npm run coverage
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"build": "tsc",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "tslint --project tsconfig.json --config tslint.json",
"test": "nyc --reporter=text mocha",
"test": "mocha",
"test-with-coverage": "nyc --reporter=text mocha",
"type-check": "tsc --project tsconfig.json --noEmit",
"watch": "tsc -w"
},
Expand Down
12 changes: 5 additions & 7 deletions src/api/API.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as cheerio from 'cheerio';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { CatalogueParser, DefaultCatalogueParser } from './CatalogueParser';
import Category from './Category';
import Item from './Item';

export interface SaleFinderAPIConfig {
catalogueParser: CatalogueParser;
requestConfig: AxiosRequestConfig;
requestInstance: AxiosInstance;
rowsPerPage: number;
}

Expand All @@ -32,11 +32,9 @@ export default class SaleFinderAPI implements ISaleFinderAPI {
...config,
};

this.request = axios.create({
baseURL: 'https://embed.salefinder.com.au/',
timeout: 5000,
...config.requestConfig,
} as AxiosRequestConfig);
this.request = this.config.requestInstance || axios.create();
this.request.defaults.baseURL = this.request.defaults.baseURL || 'https://embed.salefinder.com.au/';
this.request.defaults.timeout = this.request.defaults.timeout || 5000;
}

getCategories = (catalogueId: number, retailerId: number) =>
Expand Down
38 changes: 38 additions & 0 deletions test/api/CategoryParser.spec.ts
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');
});
});
});
25 changes: 25 additions & 0 deletions test/api/Item.spec.ts
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%');
});
});
});
52 changes: 52 additions & 0 deletions test/api/testdata/category-list.html
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>
34 changes: 34 additions & 0 deletions test/api/testdata/item.html
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>
27 changes: 27 additions & 0 deletions test/catalogue/WoolworthsCatalogue.spec.ts
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));
});
});
});

0 comments on commit 1ec5629

Please sign in to comment.