From 5c4c79d6ea5f912cd8346f6701458fa1560da178 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 29 Aug 2026 00:28:27 +0000 Subject: [PATCH] [Tests] Add unit tests for business platform API helpers --- .../public/node/api/business-platform.test.ts | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 packages/cli-kit/src/public/node/api/business-platform.test.ts diff --git a/packages/cli-kit/src/public/node/api/business-platform.test.ts b/packages/cli-kit/src/public/node/api/business-platform.test.ts new file mode 100644 index 00000000000..bae76699e64 --- /dev/null +++ b/packages/cli-kit/src/public/node/api/business-platform.test.ts @@ -0,0 +1,123 @@ +import { + businessPlatformRequest, + businessPlatformRequestDoc, + businessPlatformOrganizationsRequest, + businessPlatformOrganizationsRequestDoc, +} from './business-platform.js' +import {graphqlRequest, graphqlRequestDoc} from './graphql.js' +import {handleDeprecations} from './partners.js' +import {businessPlatformFqdn} from '../context/fqdn.js' +import {TypedDocumentNode} from '@graphql-typed-document-node/core' +import {test, vi, expect, describe, beforeEach} from 'vitest' + +vi.mock('./graphql.js') +vi.mock('../context/fqdn.js') + +const mockedResult = {data: {ok: true}} +const businessPlatformFqdnValue = 'business-platform.shopify.com' +const mockedToken = 'test-token' +const orgId = '12345' + +beforeEach(() => { + vi.mocked(businessPlatformFqdn).mockResolvedValue(businessPlatformFqdnValue) +}) + +describe('businessPlatformRequest', () => { + test('calls graphqlRequest with expected parameters', async () => { + vi.mocked(graphqlRequest).mockResolvedValue(mockedResult) + + const result = await businessPlatformRequest('query { user }', mockedToken, {var: 'val'}, {cacheTTL: {hours: 1}}) + + expect(graphqlRequest).toHaveBeenCalledWith({ + query: 'query { user }', + api: 'BusinessPlatform', + url: `https://${businessPlatformFqdnValue}/destinations/api/2020-07/graphql`, + token: mockedToken, + variables: {var: 'val'}, + cacheOptions: {cacheTTL: {hours: 1}}, + responseOptions: {onResponse: handleDeprecations}, + }) + expect(result).toEqual(mockedResult) + }) +}) + +describe('businessPlatformRequestDoc', () => { + test('calls graphqlRequestDoc with expected parameters', async () => { + vi.mocked(graphqlRequestDoc).mockResolvedValue(mockedResult) + const query = 'query' as unknown as TypedDocumentNode + const unauthorizedHandler = {type: 'token_refresh' as const, handler: vi.fn()} + + const result = await businessPlatformRequestDoc({ + query, + token: mockedToken, + variables: {var: 'val'}, + cacheOptions: {cacheTTL: {hours: 1}}, + unauthorizedHandler, + }) + + expect(graphqlRequestDoc).toHaveBeenCalledWith({ + query, + api: 'BusinessPlatform', + url: `https://${businessPlatformFqdnValue}/destinations/api/2020-07/graphql`, + token: mockedToken, + variables: {var: 'val'}, + cacheOptions: {cacheTTL: {hours: 1}}, + responseOptions: {onResponse: handleDeprecations}, + unauthorizedHandler, + }) + expect(result).toEqual(mockedResult) + }) +}) + +describe('businessPlatformOrganizationsRequest', () => { + test('calls graphqlRequest with organization-scoped URL', async () => { + vi.mocked(graphqlRequest).mockResolvedValue(mockedResult) + const unauthorizedHandler = {type: 'token_refresh' as const, handler: vi.fn()} + + const result = await businessPlatformOrganizationsRequest({ + query: 'query { org }', + token: mockedToken, + organizationId: orgId, + unauthorizedHandler, + variables: {var: 'val'}, + }) + + expect(graphqlRequest).toHaveBeenCalledWith({ + query: 'query { org }', + api: 'BusinessPlatform', + url: `https://${businessPlatformFqdnValue}/organizations/api/unstable/organization/${orgId}/graphql`, + token: mockedToken, + variables: {var: 'val'}, + responseOptions: {onResponse: handleDeprecations}, + unauthorizedHandler, + }) + expect(result).toEqual(mockedResult) + }) +}) + +describe('businessPlatformOrganizationsRequestDoc', () => { + test('calls graphqlRequestDoc with organization-scoped URL', async () => { + vi.mocked(graphqlRequestDoc).mockResolvedValue(mockedResult) + const query = 'query' as unknown as TypedDocumentNode + const unauthorizedHandler = {type: 'token_refresh' as const, handler: vi.fn()} + + const result = await businessPlatformOrganizationsRequestDoc({ + query, + token: mockedToken, + organizationId: orgId, + unauthorizedHandler, + variables: {var: 'val'}, + }) + + expect(graphqlRequestDoc).toHaveBeenCalledWith({ + query, + api: 'BusinessPlatform', + url: `https://${businessPlatformFqdnValue}/organizations/api/unstable/organization/${orgId}/graphql`, + token: mockedToken, + variables: {var: 'val'}, + responseOptions: {onResponse: handleDeprecations}, + unauthorizedHandler, + }) + expect(result).toEqual(mockedResult) + }) +})