|
1
|
import{calculateInitialLineItems,getSessionId,getPerBundleQuantity,getBundleComponentRowsFromLineItems,groupBundleLineItems}from"../index";const prices=[{id:"price1",product_id:"product1",quantity:1,enabled:!0},{id:"price2",product_id:"product1",quantity:2,enabled:!0},{id:"price3",product_id:"product2",quantity:3,enabled:!0},{id:"price4",product_id:"product2",quantity:4,enabled:!0}];describe("Line items functions",(()=>{it("calculateInitialLineItems",(()=>{expect(calculateInitialLineItems(prices,"all")).toEqual([{price_id:"price1",quantity:1},{price_id:"price2",quantity:2},{price_id:"price3",quantity:3},{price_id:"price4",quantity:4}]),expect(calculateInitialLineItems(prices,"multiple")).toEqual([{price_id:"price1",quantity:1}]),expect(calculateInitialLineItems(prices,"single")).toEqual([{price_id:"price1",quantity:1}])})),describe("getPerBundleQuantity",(()=>{it("divides the component total by the parent quantity",(()=>{expect(getPerBundleQuantity({quantity:2},2)).toBe(1),expect(getPerBundleQuantity({quantity:4},2)).toBe(2)})),it("defaults the parent quantity to 1 and never returns below 1",(()=>{expect(getPerBundleQuantity({quantity:3})).toBe(3),expect(getPerBundleQuantity({quantity:0},5)).toBe(1)}))})),describe("getBundleComponentRowsFromLineItems",(()=>{const e=[{id:"c1",quantity:4,variant_options:["Red","XL"],price:{product:{name:"Air Beats"}}},{id:"c2",quantity:2,variant_display_options:"10°C",price:{product:{name:"Sleeping Bag"}}},{id:"c3",quantity:2,variant_options:[],price:{product:{name:"No Variant"}}}];it('builds per-bundle rows: options joined by " · ", display string fallback, all shown by default',(()=>{const t=getBundleComponentRowsFromLineItems(e,2);expect(t).toEqual([{id:"c1",name:"Air Beats",variants:"Red · XL",qty:2},{id:"c2",name:"Sleeping Bag",variants:"10°C",qty:1},{id:"c3",name:"No Variant",variants:"",qty:1}])})),it("drops components without a variant selection in variants-only mode",(()=>{const t=getBundleComponentRowsFromLineItems(e,2,!1);expect(t.map((e=>e.id))).toEqual(["c1","c2"])}))})),describe("groupBundleLineItems",(()=>{it("orders bundle components by position, regardless of payload order",(()=>{const{componentsByParent:e}=groupBundleLineItems([{id:"bundle",price:{product:{bundle:!0}},component_line_items:{data:[{id:"c-third",position:2},{id:"c-first",position:0},{id:"c-second",position:1}]}}]);expect(e.bundle.map((e=>e.id))).toEqual(["c-first","c-second","c-third"])})),it("preserves payload order when position is missing",(()=>{const{componentsByParent:e}=groupBundleLineItems([{id:"bundle",price:{product:{bundle:!0}},component_line_items:{data:[{id:"a"},{id:"b"},{id:"c"}]}}]);expect(e.bundle.map((e=>e.id))).toEqual(["a","b","c"])}))})),describe("getSessionId",(()=>{it("Should be able to refresh",(async()=>{expect(getSessionId("asdf",{id:"existing"},!0)).toBe(!1)})),it("Should return the checkout session id if it already exists",(async()=>{expect(getSessionId("asdf",{id:"existing"})).toBe("existing")})),it("Should get the checkout session from the url, first",(async()=>{delete window.location,window.location=new URL("https://www.example.com?order=urltest"),expect(getSessionId("asdf",{})).toBe("urltest")})),it("Should get the checkout session from localstorage, second",(async()=>{jest.spyOn(window.localStorage.__proto__,"getItem"),window.localStorage.__proto__.getItem=jest.fn(),getSessionId("asdf",{}),expect(localStorage.getItem).toHaveBeenCalled()}))}))}));
|