FileMaster
Search
Toggle Dark Mode
Home
/
.
/
wp-content
/
plugins
/
surecart
/
dist
/
components
/
collection
/
store
/
checkout
/
test
Edit File: checkout-store.spec.js
import{state as checkoutState,dispose as disposeCheckout}from"..";import{getCheckout}from"../../checkouts/mutations";import{dispose}from"../../checkouts";import{getCompleteAddress,getResolvedBillingAddress,toStripeAddress}from"../getters";describe("checkout store",(()=>{beforeEach((()=>{dispose(),disposeCheckout()})),describe("watchers",(()=>{it("syncs checkout object with checkout store",(()=>{checkoutState.formId=1,checkoutState.mode="test",checkoutState.checkout={id:"test"},expect(getCheckout(1,"test")).toEqual(checkoutState.checkout),expect(checkoutState.checkout).toEqual(getCheckout(1,"test"))}))})),describe("events",(()=>{it("emits scCheckoutInitiated event",(()=>{const e=jest.fn();window.addEventListener("scCheckoutInitiated",e),checkoutState.checkout={id:"test"},expect(e).toBeCalledTimes(0)})),it("emits scAddedToCart, scRemovedFromCart and scCartUpdated events",(()=>{const e=jest.fn();window.addEventListener("scAddedToCart",e);const t=jest.fn();window.addEventListener("scRemovedFromCart",t);const i=jest.fn();window.addEventListener("scCartUpdated",i),checkoutState.checkout={id:"test",line_items:{data:[{id:"test"}]}},expect(e).toBeCalledTimes(1),expect(t).toBeCalledTimes(0),expect(i).toBeCalledTimes(1),checkoutState.checkout={id:"test",line_items:{data:[]}},expect(e).toBeCalledTimes(1),expect(t).toBeCalledTimes(1),expect(i).toBeCalledTimes(2)})),it("emits scCheckoutCompleted, scOrderPaid and scTrialStarted events",(()=>{const e=jest.fn();window.addEventListener("scCheckoutCompleted",e);const t=jest.fn();window.addEventListener("scOrderPaid",t);const i=jest.fn();window.addEventListener("scTrialStarted",i),checkoutState.checkout={id:"test",status:"finalized"},expect(e).toBeCalledTimes(0),expect(t).toBeCalledTimes(0),checkoutState.checkout={id:"test",status:"finalized"},expect(e).toBeCalledTimes(0),expect(t).toBeCalledTimes(0),checkoutState.checkout={id:"test",status:"processing"},expect(e).toBeCalledTimes(1),expect(t).toBeCalledTimes(1),checkoutState.checkout={id:"test",status:"paid",line_items:{data:[{id:"test",price:{trial_duration_days:10}}]}},expect(i).toBeCalledTimes(1),expect(e).toBeCalledTimes(2),expect(t).toBeCalledTimes(2)}))})),describe("getCompleteAddress",(()=>{it("returns shipping address by default",(()=>{checkoutState.checkout={shipping_address:{line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"}};const e=getCompleteAddress();expect(e).toEqual({line1:"456 Shipping Ave",line2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"})})),it("returns billing address when type is billing",(()=>{checkoutState.checkout={billing_address:{line_1:"123 Billing St",line_2:"Suite 1",city:"Billing City",state:"BC",postal_code:"12345",country:"US"},shipping_address:{line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"}};const e=getCompleteAddress("billing");expect(e).toEqual({line1:"123 Billing St",line2:"Suite 1",city:"Billing City",state:"BC",postal_code:"12345",country:"US"})})),it("returns undefined when address is incomplete",(()=>{checkoutState.checkout={shipping_address:{country:"US"}},expect(getCompleteAddress("shipping")).toBeUndefined()}))})),describe("getResolvedBillingAddress",(()=>{it("returns billing address when billing_matches_shipping is false and billing has line_1",(()=>{const e=getResolvedBillingAddress({billing_matches_shipping:!1,billing_address:{line_1:"123 Billing St",line_2:"Suite 1",city:"Billing City",state:"BC",postal_code:"12345",country:"US"},shipping_address:{line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"}});expect(e).toEqual({line_1:"123 Billing St",line_2:"Suite 1",city:"Billing City",state:"BC",postal_code:"12345",country:"US"})})),it("falls back to shipping when billing_matches_shipping is false but billing has no line_1",(()=>{const e=getResolvedBillingAddress({billing_matches_shipping:!1,billing_address:{country:"US"},shipping_address:{line_1:"456 Shipping Ave",line_2:"Apt 2",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"}});expect(e).toEqual({line_1:"456 Shipping Ave",line_2:"Apt 2",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"})})),it("returns shipping address when billing_matches_shipping is not false",(()=>{const e=getResolvedBillingAddress({billing_matches_shipping:!0,shipping_address:{line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"}});expect(e).toEqual({line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"})})),it("returns undefined when no addresses are present",(()=>{const e=getResolvedBillingAddress({});expect(e).toBeUndefined()})),it("returns undefined when billing has no line_1 and shipping is empty",(()=>{const e=getResolvedBillingAddress({billing_matches_shipping:!1,billing_address:{country:"US"},shipping_address:{}});expect(e).toBeUndefined()})),it("falls back to shipping when billing_matches_shipping is undefined",(()=>{const e=getResolvedBillingAddress({shipping_address:{line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"}});expect(e).toEqual({line_1:"456 Shipping Ave",line_2:"",city:"Shipping City",state:"SC",postal_code:"67890",country:"US"})}))})),describe("toStripeAddress",(()=>{it("converts canonical Address to Stripe format",(()=>{const e=toStripeAddress({line_1:"123 Main St",line_2:"Suite 1",city:"Springfield",state:"IL",postal_code:"62704",country:"US"});expect(e).toEqual({line1:"123 Main St",line2:"Suite 1",city:"Springfield",state:"IL",postal_code:"62704",country:"US"})})),it("returns undefined when address is undefined",(()=>{expect(toStripeAddress(void 0)).toBeUndefined()})),it("returns undefined when address has no line_1",(()=>{expect(toStripeAddress({country:"US"})).toBeUndefined()}))}))}));
Save
Back