|
| 1 | +import { |
| 2 | + assertPriceIsReasonable, |
| 3 | + computePrices, |
| 4 | + getVariantPrice, |
| 5 | + priceLiteralText, |
| 6 | + ShopifyProduct, |
| 7 | +} from './price-utils'; |
| 8 | + |
| 9 | +describe('getVariantPrice', () => { |
| 10 | + const products: ShopifyProduct[] = [ |
| 11 | + { |
| 12 | + handle: 'charachorder-x', |
| 13 | + variants: [{ title: 'Default Title', price: '99.99' }], |
| 14 | + }, |
| 15 | + { |
| 16 | + handle: 'master-forge-1', |
| 17 | + variants: [ |
| 18 | + { title: 'Standard', price: '599.99' }, |
| 19 | + { title: 'Premium', price: '749.99' }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + ]; |
| 23 | + |
| 24 | + it('returns the price of the only variant when no title is given', () => { |
| 25 | + expect(getVariantPrice(products, 'charachorder-x')).toBe(99.99); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns the price of the matching variant title', () => { |
| 29 | + expect(getVariantPrice(products, 'master-forge-1', 'Premium')).toBe(749.99); |
| 30 | + }); |
| 31 | + |
| 32 | + it('throws when the product handle is not found', () => { |
| 33 | + expect(() => getVariantPrice(products, 'does-not-exist')).toThrow( |
| 34 | + /was not found/, |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it('throws when the variant title is not found', () => { |
| 39 | + expect(() => |
| 40 | + getVariantPrice(products, 'master-forge-1', 'does-not-exist'), |
| 41 | + ).toThrow(/was not found/); |
| 42 | + }); |
| 43 | + |
| 44 | + it('throws when the price is not a valid number', () => { |
| 45 | + const badProducts: ShopifyProduct[] = [ |
| 46 | + { handle: 'broken', variants: [{ title: 'Default', price: 'n/a' }] }, |
| 47 | + ]; |
| 48 | + expect(() => getVariantPrice(badProducts, 'broken')).toThrow( |
| 49 | + /not a valid number/, |
| 50 | + ); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('computePrices', () => { |
| 55 | + it('computes single prices and ranges from Shopify product data', () => { |
| 56 | + const charachorderProducts: ShopifyProduct[] = [ |
| 57 | + { |
| 58 | + handle: 'master-forge-1', |
| 59 | + variants: [{ title: 'Default Title', price: '599.99' }], |
| 60 | + }, |
| 61 | + { |
| 62 | + handle: 'master-forge-premium', |
| 63 | + variants: [{ title: 'Default Title', price: '749.99' }], |
| 64 | + }, |
| 65 | + { handle: 'cc2', variants: [{ title: 'Default Title', price: '249.99' }] }, |
| 66 | + { |
| 67 | + handle: 'charachorder-lite', |
| 68 | + variants: [{ title: 'Default Title', price: '149.99' }], |
| 69 | + }, |
| 70 | + { |
| 71 | + handle: 'charachorder-x', |
| 72 | + variants: [{ title: 'Default Title', price: '99.99' }], |
| 73 | + }, |
| 74 | + ]; |
| 75 | + const svalboardProducts: ShopifyProduct[] = [ |
| 76 | + { |
| 77 | + handle: 'lightly', |
| 78 | + variants: [ |
| 79 | + { title: 'No Pointer', price: '800' }, |
| 80 | + { title: 'Dual Pointer', price: '1050' }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + ]; |
| 84 | + |
| 85 | + expect(computePrices(charachorderProducts, svalboardProducts)).toEqual({ |
| 86 | + m4g: { min: 599.99, max: 749.99 }, |
| 87 | + 'cc2-1': 249.99, |
| 88 | + cclite: 149.99, |
| 89 | + ccx: 99.99, |
| 90 | + sval: { min: 800, max: 1050 }, |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('priceLiteralText', () => { |
| 96 | + it('renders a plain number', () => { |
| 97 | + expect(priceLiteralText(99.99)).toBe('99.99'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('renders a number range as a device-spec object literal', () => { |
| 101 | + expect(priceLiteralText({ min: 599.99, max: 749.99 })).toBe( |
| 102 | + "{ type: 'number-range', value: { min: 599.99, max: 749.99 } }", |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('assertPriceIsReasonable', () => { |
| 108 | + it('does not throw when there is no committed price to compare against', () => { |
| 109 | + expect(() => |
| 110 | + assertPriceIsReasonable('sval', null, { min: 800, max: 1050 }), |
| 111 | + ).not.toThrow(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('does not throw when the fresh price is close to the committed price', () => { |
| 115 | + expect(() => |
| 116 | + assertPriceIsReasonable('ccx', 99.99, 104.99), |
| 117 | + ).not.toThrow(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('throws when the fresh price is a currency-localized outlier', () => { |
| 121 | + expect(() => assertPriceIsReasonable('m4g', 599.99, 19263)).toThrow( |
| 122 | + /sanity check failed/, |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('throws when the fresh price is implausibly small', () => { |
| 127 | + expect(() => assertPriceIsReasonable('ccx', 99.99, 1)).toThrow( |
| 128 | + /sanity check failed/, |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('checks both ends of a price range', () => { |
| 133 | + expect(() => |
| 134 | + assertPriceIsReasonable( |
| 135 | + 'm4g', |
| 136 | + { min: 599.99, max: 749.99 }, |
| 137 | + { min: 599.99, max: 24079 }, |
| 138 | + ), |
| 139 | + ).toThrow(/sanity check failed/); |
| 140 | + }); |
| 141 | +}); |
0 commit comments