Skip to content

Commit

Permalink
sendSampleWebhook better null field handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cdarne committed Nov 26, 2024
1 parent c49eb8a commit ebca8ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {ExtensionInstance} from '../../models/extensions/extension-instance.js'
import {ListApps} from '../../api/graphql/app-management/generated/apps.js'
import {PublicApiVersionsQuery} from '../../api/graphql/webhooks/generated/public-api-versions.js'
import {AvailableTopicsQuery} from '../../api/graphql/webhooks/generated/available-topics.js'
import {CliTestingMutation} from '../../api/graphql/webhooks/generated/cli-testing.js'
import {CliTesting, CliTestingMutation} from '../../api/graphql/webhooks/generated/cli-testing.js'
import {SendSampleWebhookVariables} from '../../services/webhook/request-sample.js'
import {describe, expect, test, vi} from 'vitest'
import {CLI_KIT_VERSION} from '@shopify/cli-kit/common/version'
Expand Down Expand Up @@ -369,14 +369,24 @@ describe('sendSampleWebhook', () => {
errors: [],
},
}
const expectedVariables = {
address: input.address,
apiKey: input.api_key,
apiVersion: input.api_version,
deliveryMethod: input.delivery_method,
sharedSecret: input.shared_secret,
topic: input.topic,
}
const token = 'token'
vi.mocked(webhooksRequest).mockResolvedValueOnce(mockedResponse)

// When
const client = new AppManagementClient()
client.token = () => Promise.resolve('token')
client.token = () => Promise.resolve(token)
const result = await client.sendSampleWebhook(input)

// Then
expect(webhooksRequest).toHaveBeenCalledWith(CliTesting, token, expectedVariables)
expect(result.sendSampleWebhook.samplePayload).toEqual(mockedResponse.cliTesting?.samplePayload)
expect(result.sendSampleWebhook.headers).toEqual(mockedResponse.cliTesting?.headers)
expect(result.sendSampleWebhook.success).toEqual(true)
Expand All @@ -401,14 +411,24 @@ describe('sendSampleWebhook', () => {
errors: [],
},
}
const expectedVariables = {
address: input.address,
apiKey: input.api_key,
apiVersion: input.api_version,
deliveryMethod: input.delivery_method,
sharedSecret: input.shared_secret,
topic: input.topic,
}
const token = 'token'
vi.mocked(webhooksRequest).mockResolvedValueOnce(mockedResponse)

// When
const client = new AppManagementClient()
client.token = () => Promise.resolve('token')
client.token = () => Promise.resolve(token)
const result = await client.sendSampleWebhook(input)

// Then
expect(webhooksRequest).toHaveBeenCalledWith(CliTesting, token, expectedVariables)
expect(result.sendSampleWebhook.samplePayload).toEqual('{}')
expect(result.sendSampleWebhook.headers).toEqual('{}')
expect(result.sendSampleWebhook.success).toEqual(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ import {
} from '../../api/graphql/development_preview.js'
import {AppReleaseSchema} from '../../api/graphql/app_release.js'
import {AppVersionsDiffSchema} from '../../api/graphql/app_versions_diff.js'
import {SendSampleWebhookSchema, SendSampleWebhookVariables} from '../../services/webhook/request-sample.js'
import {
SampleWebhook,
SendSampleWebhookSchema,
SendSampleWebhookVariables,
} from '../../services/webhook/request-sample.js'
import {PublicApiVersionsSchema} from '../../services/webhook/request-api-versions.js'
import {WebhookTopicsSchema, WebhookTopicsVariables} from '../../services/webhook/request-topics.js'
import {
Expand Down Expand Up @@ -724,15 +728,17 @@ export class AppManagementClient implements DeveloperPlatformClient {
topic: input.topic,
}
const result = await webhooksRequest(query, await this.token(), variables)

return {
sendSampleWebhook: {
samplePayload: result.cliTesting?.samplePayload ?? '{}',
headers: result.cliTesting?.headers ?? '{}',
success: result.cliTesting?.success ?? false,
userErrors: result.cliTesting?.errors?.map((error) => ({message: error, fields: []})) ?? [],
},
let sendSampleWebhook: SampleWebhook = {samplePayload: '{}', headers: '{}', success: false, userErrors: []}
const cliTesting = result.cliTesting
if (cliTesting) {
sendSampleWebhook = {
samplePayload: cliTesting.samplePayload ?? '{}',
headers: cliTesting.headers ?? '{}',
success: cliTesting.success,
userErrors: cliTesting.errors.map((error) => ({message: error, fields: []})),
}
}
return {sendSampleWebhook}
}

async apiVersions(): Promise<PublicApiVersionsSchema> {
Expand Down

0 comments on commit ebca8ac

Please sign in to comment.