From 39bd79adc1f257d8aff20f91f10701797d7769c8 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Tue, 1 Sep 2026 00:34:29 +0000 Subject: [PATCH] [Tests] Add unit tests for webhooksRequestDoc --- .../src/public/node/api/webhooks.test.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 packages/cli-kit/src/public/node/api/webhooks.test.ts diff --git a/packages/cli-kit/src/public/node/api/webhooks.test.ts b/packages/cli-kit/src/public/node/api/webhooks.test.ts new file mode 100644 index 00000000000..bf7dae18a27 --- /dev/null +++ b/packages/cli-kit/src/public/node/api/webhooks.test.ts @@ -0,0 +1,50 @@ +import {webhooksRequestDoc} from './webhooks.js' +import {graphqlRequestDoc} from './graphql.js' +import {appManagementFqdn} from '../context/fqdn.js' +import {describe, expect, test, vi, beforeEach} from 'vitest' +import {TypedDocumentNode} from '@graphql-typed-document-node/core' + +vi.mock('./graphql.js') +vi.mock('../context/fqdn.js') + +const mockedResult = {data: {webhooks: []}} +const appManagementFqdnValue = 'app.shopify.com' +const mockedToken = 'test-token' +const organizationId = '12345' + +beforeEach(() => { + vi.mocked(appManagementFqdn).mockResolvedValue(appManagementFqdnValue) +}) + +describe('webhooksRequestDoc', () => { + test('executes graphqlRequestDoc with correct URL and options', async () => { + // Given + vi.mocked(graphqlRequestDoc).mockResolvedValue(mockedResult) + const query = 'query { webhooks }' as unknown as TypedDocumentNode + const variables = {variables: 'test-variable'} + const unauthorizedHandler = { + type: 'token_refresh' as const, + handler: vi.fn().mockResolvedValue({token: mockedToken}), + } + + // When + const result = await webhooksRequestDoc({ + organizationId, + query, + token: mockedToken, + variables, + unauthorizedHandler, + }) + + // Then + expect(result).toBe(mockedResult) + expect(graphqlRequestDoc).toHaveBeenCalledWith({ + query, + api: 'Webhooks', + url: `https://${appManagementFqdnValue}/webhooks/unstable/organizations/${organizationId}/graphql.json`, + token: mockedToken, + variables, + unauthorizedHandler, + }) + }) +})