|
| 1 | +import { vi, describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import api from './adapter' |
| 4 | +import { getPriorityRule } from './priority-rule.service' |
| 5 | + |
| 6 | +vi.mock('./adapter', async () => ({ default: { services: { |
| 7 | + getIssuesFacet: vi.fn(), |
| 8 | + getRuleDetails: vi.fn().mockImplementation(key => ({ |
| 9 | + 'blocker:key': { key }, |
| 10 | + 'critical:key': { key }, |
| 11 | + 'major:key': { key }, |
| 12 | + 'minor:key': { key }, |
| 13 | + 'info:key': { key }, |
| 14 | + })[key]) |
| 15 | +}}})) |
| 16 | + |
| 17 | +const mockConfig = { project: 'foo' } |
| 18 | + |
| 19 | +function mockGetIssuesFacet(data) { |
| 20 | + return (facet, config) => { |
| 21 | + expect(config).toHaveProperty('severity') |
| 22 | + expect(config).toHaveProperty('project', 'foo') |
| 23 | + expect(facet).toBe('rules') |
| 24 | + return data[config.severity] || {} |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const BLOCKER = { 'blocker:key': 4, 'blocker:key2': 3 } |
| 29 | +const CRITICAL = { 'critical:key': 7, 'critical:key2': 4 } |
| 30 | +const MAJOR = { 'major:key': 12, 'critical:key2': 4 } |
| 31 | +const MINOR_OR_INFO = { 'minor:key': 31, 'info:key': 12, 'minor:key2': 4 } |
| 32 | + |
| 33 | +describe('getPriorityRule()', () => { |
| 34 | + |
| 35 | + test('Should return Blocker issue related rule if one exist', async () => { |
| 36 | + const mock = mockGetIssuesFacet({ BLOCKER, CRITICAL, MAJOR }) |
| 37 | + api.services.getIssuesFacet.mockImplementation(mock); |
| 38 | + const rule = await getPriorityRule(mockConfig) |
| 39 | + expect(rule).toHaveProperty('key', 'blocker:key') |
| 40 | + }) |
| 41 | + |
| 42 | + test('Should return Critical issue related rule if one exist and no blocker exist', async () => { |
| 43 | + const mock = mockGetIssuesFacet({ CRITICAL, MAJOR }) |
| 44 | + api.services.getIssuesFacet.mockImplementation(mock); |
| 45 | + const rule = await getPriorityRule(mockConfig) |
| 46 | + expect(rule).toHaveProperty('key', 'critical:key') |
| 47 | + }) |
| 48 | + |
| 49 | + test('Should return major issue related rule if one exist and no blocker or critical exist', async () => { |
| 50 | + const mock = mockGetIssuesFacet({ MAJOR, 'MINOR,INFO': MINOR_OR_INFO }) |
| 51 | + api.services.getIssuesFacet.mockImplementation(mock); |
| 52 | + const rule = await getPriorityRule(mockConfig) |
| 53 | + expect(rule).toHaveProperty('key', 'major:key') |
| 54 | + }) |
| 55 | + |
| 56 | + test('Should return minor issue related rule if one exist and no blocker, critical or major exist', async () => { |
| 57 | + const mock = mockGetIssuesFacet({ 'MINOR,INFO': MINOR_OR_INFO }) |
| 58 | + api.services.getIssuesFacet.mockImplementation(mock); |
| 59 | + const rule = await getPriorityRule(mockConfig) |
| 60 | + expect(rule).toHaveProperty('key', 'minor:key') |
| 61 | + }) |
| 62 | + |
| 63 | + test('Should return null if no issue exist', async () => { |
| 64 | + const mock = mockGetIssuesFacet({ }) |
| 65 | + api.services.getIssuesFacet.mockImplementation(mock); |
| 66 | + const rule = await getPriorityRule(mockConfig) |
| 67 | + expect(rule).toBe(null) |
| 68 | + }) |
| 69 | + |
| 70 | +}) |
0 commit comments