|
| 1 | +import {describe, it, expect, vi, beforeEach} from 'vitest' |
| 2 | + |
| 3 | +// Mock generateIssueBody so we can inspect what screenshotRepo is passed |
| 4 | +vi.mock('../src/generateIssueBody.js', () => ({ |
| 5 | + generateIssueBody: vi.fn((_finding, screenshotRepo: string) => `body with screenshotRepo=${screenshotRepo}`), |
| 6 | +})) |
| 7 | + |
| 8 | +import {reopenIssue} from '../src/reopenIssue.ts' |
| 9 | +import {generateIssueBody} from '../src/generateIssueBody.ts' |
| 10 | +import {Issue} from '../src/Issue.ts' |
| 11 | + |
| 12 | +const baseFinding = { |
| 13 | + scannerType: 'axe', |
| 14 | + ruleId: 'color-contrast', |
| 15 | + url: 'https://example.com/page', |
| 16 | + html: '<span>Low contrast</span>', |
| 17 | + problemShort: 'elements must meet minimum color contrast ratio thresholds', |
| 18 | + problemUrl: 'https://dequeuniversity.com/rules/axe/4.10/color-contrast?application=playwright', |
| 19 | + solutionShort: 'ensure the contrast between foreground and background colors meets WCAG thresholds', |
| 20 | +} |
| 21 | + |
| 22 | +const testIssue = new Issue({ |
| 23 | + id: 42, |
| 24 | + nodeId: 'MDU6SXNzdWU0Mg==', |
| 25 | + url: 'https://github.com/org/filing-repo/issues/7', |
| 26 | + title: 'Accessibility issue: test', |
| 27 | + state: 'closed', |
| 28 | +}) |
| 29 | + |
| 30 | +function mockOctokit() { |
| 31 | + return { |
| 32 | + request: vi.fn().mockResolvedValue({data: {id: 42, html_url: 'https://github.com/org/filing-repo/issues/7'}}), |
| 33 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 34 | + } as any |
| 35 | +} |
| 36 | + |
| 37 | +describe('reopenIssue', () => { |
| 38 | + beforeEach(() => { |
| 39 | + vi.clearAllMocks() |
| 40 | + }) |
| 41 | + |
| 42 | + it('passes screenshotRepo to generateIssueBody when provided', async () => { |
| 43 | + const octokit = mockOctokit() |
| 44 | + await reopenIssue(octokit, testIssue, baseFinding, 'org/filing-repo', 'org/workflow-repo') |
| 45 | + |
| 46 | + expect(generateIssueBody).toHaveBeenCalledWith(baseFinding, 'org/workflow-repo') |
| 47 | + }) |
| 48 | + |
| 49 | + it('falls back to repoWithOwner when screenshotRepo is not provided', async () => { |
| 50 | + const octokit = mockOctokit() |
| 51 | + await reopenIssue(octokit, testIssue, baseFinding, 'org/filing-repo') |
| 52 | + |
| 53 | + expect(generateIssueBody).toHaveBeenCalledWith(baseFinding, 'org/filing-repo') |
| 54 | + }) |
| 55 | + |
| 56 | + it('does not generate a body when finding is not provided', async () => { |
| 57 | + const octokit = mockOctokit() |
| 58 | + await reopenIssue(octokit, testIssue) |
| 59 | + |
| 60 | + expect(generateIssueBody).not.toHaveBeenCalled() |
| 61 | + expect(octokit.request).toHaveBeenCalledWith( |
| 62 | + expect.any(String), |
| 63 | + expect.not.objectContaining({body: expect.anything()}), |
| 64 | + ) |
| 65 | + }) |
| 66 | + |
| 67 | + it('does not generate a body when repoWithOwner is not provided', async () => { |
| 68 | + const octokit = mockOctokit() |
| 69 | + await reopenIssue(octokit, testIssue, baseFinding) |
| 70 | + |
| 71 | + expect(generateIssueBody).not.toHaveBeenCalled() |
| 72 | + }) |
| 73 | + |
| 74 | + it('sends PATCH to the correct issue URL with state open', async () => { |
| 75 | + const octokit = mockOctokit() |
| 76 | + await reopenIssue(octokit, testIssue, baseFinding, 'org/filing-repo', 'org/workflow-repo') |
| 77 | + |
| 78 | + expect(octokit.request).toHaveBeenCalledWith( |
| 79 | + 'PATCH /repos/org/filing-repo/issues/7', |
| 80 | + expect.objectContaining({ |
| 81 | + state: 'open', |
| 82 | + issue_number: 7, |
| 83 | + }), |
| 84 | + ) |
| 85 | + }) |
| 86 | + |
| 87 | + it('includes generated body when finding and repoWithOwner are provided', async () => { |
| 88 | + const octokit = mockOctokit() |
| 89 | + await reopenIssue(octokit, testIssue, baseFinding, 'org/filing-repo', 'org/workflow-repo') |
| 90 | + |
| 91 | + expect(octokit.request).toHaveBeenCalledWith( |
| 92 | + expect.any(String), |
| 93 | + expect.objectContaining({ |
| 94 | + body: 'body with screenshotRepo=org/workflow-repo', |
| 95 | + }), |
| 96 | + ) |
| 97 | + }) |
| 98 | +}) |
0 commit comments