|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals' |
| 4 | + |
| 5 | +// Mock all required modules |
| 6 | +jest.mock('@actions/core') |
| 7 | +jest.mock('@actions/github', () => ({ |
| 8 | + context: { |
| 9 | + payload: { repository: { name: 'test-repo' } }, |
| 10 | + repo: { owner: 'test-owner' }, |
| 11 | + }, |
| 12 | + getOctokit: jest.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +// Mock the main modules we'll need |
| 16 | +const mockGetFirstBlocks = jest.fn() |
| 17 | +jest.mock('../src/utils/slack/blocks.js', () => ({ |
| 18 | + getFirstBlocks: mockGetFirstBlocks, |
| 19 | + getLastBlocks: jest.fn(() => []), |
| 20 | + getPullBlocks: jest.fn(() => Promise.resolve([])), |
| 21 | +})) |
| 22 | + |
| 23 | +const mockSlackPostMessage = jest.fn() |
| 24 | +jest.mock('../src/utils/slack/slack-client.js', () => ({ |
| 25 | + SlackClient: jest.fn(() => ({ |
| 26 | + enforceAppNamePattern: jest.fn(), |
| 27 | + postMessage: mockSlackPostMessage, |
| 28 | + })), |
| 29 | +})) |
| 30 | + |
| 31 | +jest.mock('../src/utils/github/github-client.js', () => { |
| 32 | + const mockGetOrg = jest.fn() |
| 33 | + mockGetOrg.mockImplementation(() => Promise.resolve({ name: 'test-org' })) |
| 34 | + |
| 35 | + return { |
| 36 | + GitHubClient: jest.fn(() => ({ |
| 37 | + getOrg: mockGetOrg, |
| 38 | + getPulls: jest.fn(() => Promise.resolve([])), |
| 39 | + getRepositories: jest.fn(() => Promise.resolve([])), |
| 40 | + })), |
| 41 | + } |
| 42 | +}) |
| 43 | + |
| 44 | +jest.mock('../src/utils/test-data.js') |
| 45 | +jest.mock('../src/input-parser.js', () => ({ |
| 46 | + parseInputs: jest.fn(() => ({ |
| 47 | + githubConfig: { |
| 48 | + options: {}, |
| 49 | + tokens: ['fake-token1', 'fake-token2'], |
| 50 | + }, |
| 51 | + repositoryFilter: [], |
| 52 | + slackConfig: { channels: [], token: 'fake-token' }, |
| 53 | + withArchived: false, |
| 54 | + withDrafts: false, |
| 55 | + withPublic: false, |
| 56 | + withTestData: false, |
| 57 | + withUserMentions: false, |
| 58 | + })), |
| 59 | +})) |
| 60 | + |
| 61 | +describe('Organization links in Slack notifications', () => { |
| 62 | + beforeEach(() => { |
| 63 | + jest.resetAllMocks() |
| 64 | + jest.spyOn(console, 'log').mockImplementation(jest.fn()) |
| 65 | + jest.spyOn(console, 'error').mockImplementation(jest.fn()) |
| 66 | + }) |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + jest.restoreAllMocks() |
| 70 | + }) |
| 71 | + |
| 72 | + it('passes org names to getFirstBlocks from results array, not dedupedPulls', () => { |
| 73 | + // We test this logic: |
| 74 | + // const orgs = [...new Set(results.map((result) => result.org))] |
| 75 | + // blocks = [...getFirstBlocks(orgs, header, text), ...blocks] |
| 76 | + |
| 77 | + jest.isolateModules(() => { |
| 78 | + // Create spies to verify the behavior |
| 79 | + const orgSpy = jest.fn() |
| 80 | + mockGetFirstBlocks.mockImplementation((orgs) => { |
| 81 | + orgSpy(orgs) |
| 82 | + |
| 83 | + return [] |
| 84 | + }) |
| 85 | + |
| 86 | + // This test doesn't actually run the code in app.ts, but verifies |
| 87 | + // that with the fix in place, orgs would be obtained from results |
| 88 | + // rather than dedupedPulls when there are no PRs |
| 89 | + const results = [{ org: 'org1' }, { org: 'org2' }] |
| 90 | + |
| 91 | + const dedupedPulls: any[] = [] |
| 92 | + |
| 93 | + // Simulate the fixed code behavior |
| 94 | + const orgs = [...new Set(results.map((result) => result.org))] |
| 95 | + expect(orgs).toEqual(['org1', 'org2']) |
| 96 | + |
| 97 | + // Verify that the old behavior would result in empty orgs |
| 98 | + const oldOrgs = [...new Set(dedupedPulls.map((pull) => (pull as { org: string }).org))] |
| 99 | + expect(oldOrgs).toEqual([]) |
| 100 | + }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments