Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinfirman committed Apr 2, 2024
1 parent 60dd2fc commit b026930
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
17 changes: 17 additions & 0 deletions packages/hydrogen/src/cart/cart-test-helper.ts
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}
11 changes: 10 additions & 1 deletion packages/hydrogen/src/cart/createCartHandler.test.ts
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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'});

Expand Down
11 changes: 10 additions & 1 deletion 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', () => {
Expand Down Expand Up @@ -36,6 +36,8 @@ function createFetchResponse<T>(data: T, options: {ok: boolean}) {

let session: HydrogenSession;

const exchangeForStorefrontCustomerAccessToken = vi.fn();

describe('auth.helpers', () => {
describe('refreshToken', () => {
beforeEach(() => {
Expand All @@ -58,6 +60,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});
}

Expand All @@ -77,6 +80,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});
}

Expand Down Expand Up @@ -106,6 +110,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});
}

Expand Down Expand Up @@ -136,6 +141,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});

expect(session.set).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -194,6 +200,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});
}

Expand Down Expand Up @@ -224,6 +231,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});

expect(session.set).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -265,6 +273,7 @@ describe('auth.helpers', () => {
customerAccountId: 'customerAccountId',
customerAccountUrl: 'customerAccountUrl',
httpsOrigin: 'https://localhost',
exchangeForStorefrontCustomerAccessToken,
});

expect(session.set).not.toHaveBeenNthCalledWith(1, 'customerAccount', {
Expand Down
4 changes: 1 addition & 3 deletions 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) {
Expand Down Expand Up @@ -519,7 +519,6 @@ describe('customer', () => {
expiresAt: expect.any(String),
idToken: 'e30=.eyJub25jZSI6ICJub25jZSJ9.signature',
refreshToken: 'refresh_token',
redirectPath: undefined,
}),
);
});
Expand Down Expand Up @@ -570,7 +569,6 @@ describe('customer', () => {
expiresAt: expect.any(String),
idToken: 'e30=.eyJub25jZSI6ICJub25jZSJ9.signature',
refreshToken: 'refresh_token',
redirectPath: undefined,
}),
);
});
Expand Down

0 comments on commit b026930

Please sign in to comment.