openvibers/openvibe.coupons Commits

A deal 'for from $46' or 'for only $19.99' is a product deal too (it was headlined Free shipping under Codes); the reading of codes has its test (#115)

OP OpenVibers committed 16 days ago · 3927ff16ab · 2 files, +29 −2

Changes

ddiff --git a/server/render/pages.js b/server/render/pages.js
iindex 144bf19..95d2863 100644
---- a/server/render/pages.js
++++ b/server/render/pages.js
@@@ -96,7 +96,7 @@ function reportForms(v, { viewer, csrf, back }) {
// apply it, a report with its reason) folds under Details, open on the code's own page.
/** DealNews' "Air Monitor for $35 + free shipping": a code for one product, not for the shop. */
--const PRODUCT_DEAL_RE = /\bfor \$\s?\d[\d,]*(?:\.\d\d)?\b/i;
++const PRODUCT_DEAL_RE = /\bfor (?:(?:from|only|just|as low as) )?\$\s?\d[\d,]*(?:\.\d\d)?\b/i;
const isProductDeal = (v) => PRODUCT_DEAL_RE.test(v.title || '');
/** The headline a person scans for, from the code's own title (then its description): 40% off, $16 off, Free shipping, $24. */
@@@ -109,7 +109,7 @@ function headline(v) {
if (m) return `$${m[1]} off`;
if (/free shipping/i.test(title) && !PRODUCT_DEAL_RE.test(title)) return 'Free shipping';
m = PRODUCT_DEAL_RE.exec(title);
-- if (m) return m[0].replace(/^for\s*/i, '').replace(/\s+/g, '');
++ if (m) return m[0].replace(/^for\s*(?:(?:from|only|just|as low as)\s+)?/i, '').replace(/\s+/g, '');
m = /\$\s?(\d[\d,]*(?:\.\d\d)?)\s*off/i.exec(both);
if (m) return `$${m[1]} off`;
return 'Code';
ddiff --git a/test/shop-page.test.js b/test/shop-page.test.js
nnew file mode 100644
iindex 0000000..530b240
---- /dev/null
++++ b/test/shop-page.test.js
@@@ -0,0 +1,27 @@
++'use strict';
++// The shop page's reading of a code (server/render/pages.js): which codes are for one product (Product deals) and the
++// headline a person scans for, from the code's own title and description; and which tab opens first.
++const assert = require('assert');
++const pages = require('../server/render/pages');
++
++const v = (title, description = '') => ({ title, description });
++const cases = [
++ ['Mosiech 7-Piece Ceramic Nonstick Cookware Set for From $46 + free shipping', true, '$46'],
++ ['Andusic Aroma Diffuser for $24 + free shipping w/ Prime', true, '$24'],
++ ['Shoes for only $19.99', true, '$19.99'],
++ ['20% off sitewide', false, '20% off'],
++ ['$15 off orders of $75+', false, '$15 off'],
++ ['Free shipping on orders over $60', false, 'Free shipping'],
++ ['Welcome offer', false, 'Code'],
++];
++cases.forEach(([title, deal, head]) => {
++ assert.strictEqual(pages.isProductDeal(v(title)), deal, `deal: ${title}`);
++ assert.strictEqual(pages.headline(v(title)), head, `headline: ${title}`);
++});
++assert.strictEqual(pages.headline(v('Air monitor for $35', "it's $35 (50%) off the list price")), '50% off', 'a percent in the description wins over the price');
++const active = [v('Desk for $99'), v('Lamp for $20')];
++assert.strictEqual(pages.shopTab({}, active), 'deals', 'only product deals: they open first');
++assert.strictEqual(pages.shopTab({}, [...active, v('10% off everything')]), 'codes', 'a code for the shop: Codes opens');
++assert.strictEqual(pages.shopTab({ tab: 'about' }, active), 'about');
++assert.strictEqual(pages.shopTab({ tab: 'nope' }, []), 'codes', 'an unknown tab, nothing active: Codes');
++console.log('shop page: product deals, headlines and the first tab ok');