diff --git a/packages/hydrogen/src/cart/cart-test-helper.ts b/packages/hydrogen/src/cart/cart-test-helper.ts index 76c7b5d41e..e93d732965 100644 --- a/packages/hydrogen/src/cart/cart-test-helper.ts +++ b/packages/hydrogen/src/cart/cart-test-helper.ts @@ -2,7 +2,10 @@ import {CachingStrategy} from '../cache/strategies'; import type {ExecutionArgs} from 'graphql'; import {Storefront} from '../storefront'; import {CacheNone} from '../cache/strategies'; +import { CustomerAccount } from '../customer/types'; +export const BUYER_ACCESS_TOKEN = 'sha123'; +export const BUYER_LOCATION_ID = 'gid://shopify/CompanyLocation/1'; export const CART_ID = 'gid://shopify/Cart/c1-123'; export const NEW_CART_ID = 'c1-new-cart-id'; export const CHECKOUT_URL = @@ -72,3 +75,17 @@ export function mockCreateStorefrontClient() { CacheNone: CacheNone, } as Storefront; } + +export function mockGetBuyer() { + return Promise.resolve({ + customerAccessToken: BUYER_ACCESS_TOKEN, + companyLocationId: BUYER_LOCATION_ID, + }); +} + +export function mockCreateCustomerAccountClient() { + return { + UNSTABLE_getBuyer: mockGetBuyer, + isLoggedIn: () => Promise.resolve(true), + } as CustomerAccount; +} diff --git a/packages/hydrogen/src/cart/createCartHandler.test.ts b/packages/hydrogen/src/cart/createCartHandler.test.ts index 56755d018e..dfc2b3e364 100644 --- a/packages/hydrogen/src/cart/createCartHandler.test.ts +++ b/packages/hydrogen/src/cart/createCartHandler.test.ts @@ -4,7 +4,7 @@ import { HydrogenCartCustom, createCartHandler, } from './createCartHandler'; -import {mockCreateStorefrontClient, mockHeaders} from './cart-test-helper'; +import {mockCreateCustomerAccountClient, mockCreateStorefrontClient, mockHeaders} from './cart-test-helper'; type MockCarthandler = { cartId?: string; @@ -17,6 +17,7 @@ function getCartHandler(options: MockCarthandler = {}) { const {cartId, ...rest} = options; return createCartHandler({ storefront: mockCreateStorefrontClient(), + customerAccount: mockCreateCustomerAccountClient(), getCartId: () => options.cartId ? `gid://shopify/Cart/${options.cartId}` : undefined, setCartId: () => new Headers(), @@ -160,6 +161,14 @@ describe('createCartHandler', () => { expect(result.cart).toHaveProperty('id', 'c1-new-cart-id'); }); + it('function create includes buyerIdentity in args', async () => { + const cart = getCartHandler({cartId: 'c1-123'}); + + const result = await cart.create({}); + + expect(result.cart).toHaveProperty('id', 'c1-new-cart-id'); + }); + it('function addLines has a working default implementation', async () => { const cart = getCartHandler({cartId: 'c1-123'}); diff --git a/packages/hydrogen/src/customer/auth.helpers.test.ts b/packages/hydrogen/src/customer/auth.helpers.test.ts index 460db56e4b..bfce68697a 100644 --- a/packages/hydrogen/src/customer/auth.helpers.test.ts +++ b/packages/hydrogen/src/customer/auth.helpers.test.ts @@ -1,6 +1,6 @@ import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest'; import type {HydrogenSession} from '../hydrogen'; -import {CUSTOMER_ACCOUNT_SESSION_KEY} from './constants'; +import {CUSTOMER_ACCOUNT_SESSION_KEY} from '../constants'; import {checkExpires, clearSession, refreshToken} from './auth.helpers'; vi.mock('./BadRequest', () => { @@ -36,6 +36,8 @@ function createFetchResponse(data: T, options: {ok: boolean}) { let session: HydrogenSession; +const exchangeForStorefrontCustomerAccessToken = vi.fn(); + describe('auth.helpers', () => { describe('refreshToken', () => { beforeEach(() => { @@ -58,6 +60,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); } @@ -77,6 +80,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); } @@ -106,6 +110,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); } @@ -136,6 +141,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); expect(session.set).toHaveBeenNthCalledWith( @@ -194,6 +200,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); } @@ -224,6 +231,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); expect(session.set).toHaveBeenNthCalledWith( @@ -265,6 +273,7 @@ describe('auth.helpers', () => { customerAccountId: 'customerAccountId', customerAccountUrl: 'customerAccountUrl', httpsOrigin: 'https://localhost', + exchangeForStorefrontCustomerAccessToken, }); expect(session.set).not.toHaveBeenNthCalledWith(1, 'customerAccount', { diff --git a/packages/hydrogen/src/customer/customer.test.ts b/packages/hydrogen/src/customer/customer.test.ts index fffe2919df..c8fc6a07bf 100644 --- a/packages/hydrogen/src/customer/customer.test.ts +++ b/packages/hydrogen/src/customer/customer.test.ts @@ -1,7 +1,7 @@ import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest'; import type {HydrogenSession, HydrogenSessionData} from '../hydrogen'; import {createCustomerAccountClient} from './customer'; -import {CUSTOMER_ACCOUNT_SESSION_KEY} from './constants'; +import {CUSTOMER_ACCOUNT_SESSION_KEY} from '../constants'; import crypto from 'node:crypto'; if (!globalThis.crypto) { @@ -519,7 +519,6 @@ describe('customer', () => { expiresAt: expect.any(String), idToken: 'e30=.eyJub25jZSI6ICJub25jZSJ9.signature', refreshToken: 'refresh_token', - redirectPath: undefined, }), ); }); @@ -570,7 +569,6 @@ describe('customer', () => { expiresAt: expect.any(String), idToken: 'e30=.eyJub25jZSI6ICJub25jZSJ9.signature', refreshToken: 'refresh_token', - redirectPath: undefined, }), ); });