|
| 1 | +import { beforeAll, describe, expect, test, vi } from 'vitest' |
| 2 | + |
| 3 | +import { WebhooksTransformer } from '@/article-api/transformers/webhooks-transformer' |
| 4 | +import { getWebhooks } from '@/webhooks/lib/index' |
| 5 | +import { allVersions } from '@/versions/lib/all-versions' |
| 6 | +import type { Context, Page } from '@/types' |
| 7 | + |
| 8 | +// Wrap the real loader so the real-data tests keep using synced data while one test injects a fixture. |
| 9 | +vi.mock('@/webhooks/lib/index', async (importOriginal) => { |
| 10 | + const actual = await importOriginal<typeof import('@/webhooks/lib/index')>() |
| 11 | + return { ...actual, getWebhooks: vi.fn(actual.getWebhooks) } |
| 12 | +}) |
| 13 | + |
| 14 | +const stubPage = { |
| 15 | + autogenerated: 'webhooks', |
| 16 | + title: 'Webhook events and payloads', |
| 17 | + intro: '', |
| 18 | + renderProp: async () => '', |
| 19 | +} as unknown as Page |
| 20 | + |
| 21 | +function getCommonRow(body: string, name: string): string { |
| 22 | + const commonSection = body.split('## Common payload parameters')[1]?.split('\n## ')[0] || '' |
| 23 | + return commonSection.split('\n').find((line) => line.startsWith(`| \`${name}\` |`)) || '' |
| 24 | +} |
| 25 | + |
| 26 | +async function renderBody(currentVersion = 'free-pro-team@latest'): Promise<string> { |
| 27 | + const context = { |
| 28 | + currentVersion, |
| 29 | + currentVersionObj: allVersions[currentVersion], |
| 30 | + allVersions, |
| 31 | + } as unknown as Context |
| 32 | + return await new WebhooksTransformer().transform( |
| 33 | + stubPage, |
| 34 | + '/en/webhooks/webhook-events-and-payloads', |
| 35 | + context, |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +function makeAction(params: Array<[string, boolean]>) { |
| 40 | + return { |
| 41 | + bodyParameters: params.map(([paramName, isRequired]) => ({ |
| 42 | + name: paramName, |
| 43 | + type: 'object', |
| 44 | + description: `The ${paramName}.`, |
| 45 | + isRequired, |
| 46 | + })), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +describe('WebhooksTransformer common parameters', () => { |
| 51 | + let body = '' |
| 52 | + |
| 53 | + beforeAll(async () => { |
| 54 | + body = await renderBody() |
| 55 | + }) |
| 56 | + |
| 57 | + // Some events, such as push, have no action field, so the shared rows cannot be required for all events. |
| 58 | + test('common parameters are not marked required when some events omit them', () => { |
| 59 | + for (const name of ['action', 'repository', 'sender']) { |
| 60 | + const row = getCommonRow(body, name) |
| 61 | + expect(row).toBeTruthy() |
| 62 | + expect(row).not.toContain('**Required.**') |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + test('common section says required status varies by event', () => { |
| 67 | + expect(body).toContain('whether a parameter is required varies by event') |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +describe('WebhooksTransformer common parameters with fixture data', () => { |
| 72 | + test('marks a common row required only when every event requires it', async () => { |
| 73 | + vi.mocked(getWebhooks).mockResolvedValueOnce({ |
| 74 | + first: { |
| 75 | + default: makeAction([ |
| 76 | + ['action', true], |
| 77 | + ['repository', true], |
| 78 | + ['sender', true], |
| 79 | + ]), |
| 80 | + }, |
| 81 | + second: { |
| 82 | + opened: makeAction([ |
| 83 | + ['action', true], |
| 84 | + ['repository', true], |
| 85 | + ['sender', true], |
| 86 | + ]), |
| 87 | + closed: makeAction([ |
| 88 | + ['action', true], |
| 89 | + ['repository', false], |
| 90 | + ['sender', true], |
| 91 | + ]), |
| 92 | + }, |
| 93 | + third: { |
| 94 | + default: makeAction([ |
| 95 | + ['repository', true], |
| 96 | + ['sender', true], |
| 97 | + ]), |
| 98 | + }, |
| 99 | + } as unknown as Awaited<ReturnType<typeof getWebhooks>>) |
| 100 | + // Prepared webhooks are cached per version, so use a version the real-data tests do not load. |
| 101 | + const fixtureBody = await renderBody('enterprise-cloud@latest') |
| 102 | + |
| 103 | + expect(getCommonRow(fixtureBody, 'sender')).toContain('**Required.**') |
| 104 | + expect(getCommonRow(fixtureBody, 'repository')).toBeTruthy() |
| 105 | + expect(getCommonRow(fixtureBody, 'repository')).not.toContain('**Required.**') |
| 106 | + expect(getCommonRow(fixtureBody, 'action')).toBeTruthy() |
| 107 | + expect(getCommonRow(fixtureBody, 'action')).not.toContain('**Required.**') |
| 108 | + }) |
| 109 | +}) |
0 commit comments