diff --git a/test/unit/config.fixture.ts b/test/unit/config.fixture.ts new file mode 100644 index 0000000..0fa6c3a --- /dev/null +++ b/test/unit/config.fixture.ts @@ -0,0 +1,54 @@ +export const emptyConfig = {}; + +export const basicConfig = { + policy: [ + { + section: [ + { + id: ['type'], + label: [ + { name: 'bug 🐛', keys: ['Bug Report'] }, + { name: 'RFE 🎁', keys: ['Feature Request'] }, + ], + }, + ], + }, + ], +}; + +export const templateConfig = { + policy: [ + { + template: ['bug.yml', 'feature.yml'], + section: [ + { + id: ['type'], + label: [ + { name: 'bug 🐛', keys: ['Bug Report'] }, + { name: 'RFE 🎁', keys: ['Feature Request'] }, + ], + }, + { + id: ['severity'], + label: [ + { name: 'high', keys: ['High'] }, + { name: 'medium', keys: ['Medium'] }, + { name: 'low', keys: ['Low'] }, + ], + }, + ], + }, + { + template: ['custom.yml'], + section: [ + { + id: ['type'], + label: [ + { name: 'custom1', keys: ['Custom 1'] }, + { name: 'custom2', keys: ['Custom 2'] }, + ], + }, + ], + }, + ], +}; diff --git a/test/unit/config.test.ts b/test/unit/config.test.ts index 6d36c92..e56bfe2 100644 --- a/test/unit/config.test.ts +++ b/test/unit/config.test.ts @@ -2,55 +2,10 @@ import { describe, expect, test } from 'vitest'; import { Config } from '../../src/config'; -const emptyConfig = {}; - -const basicConfig = { - policy: [ - { - section: [ - { - id: ['type'], - label: [ - { name: 'bug 🐛', keys: ['Bug Report'] }, - { name: 'RFE 🎁', keys: ['Feature Request'] }, - ], - }, - ], - }, - ], -}; - -const templateConfig = { - policy: [ - { - template: ['bug.yml', 'feature.yml'], - section: [ - { - id: ['type'], - label: [ - { name: 'bug 🐛', keys: ['Bug Report'] }, - { name: 'RFE 🎁', keys: ['Feature Request'] }, - ], - }, - ], - }, - { - template: ['custom.yml'], - section: [ - { - id: ['type'], - label: [ - { name: 'custom1', keys: ['Custom 1'] }, - { name: 'custom2', keys: ['Custom 2'] }, - ], - }, - ], - }, - ], -}; +import { basicConfig, emptyConfig, templateConfig } from './config.fixture'; describe('Test Config class', () => { - test('Config class is defined', () => { + test('smoke test', () => { const config = new Config(basicConfig, '.github/issue-labeler.yml'); expect(config.policy).toMatchInlineSnapshot(` @@ -201,6 +156,32 @@ describe('Test Config class', () => { }, ], }, + { + "block-list": [], + "id": [ + "severity", + ], + "label": [ + { + "keys": [ + "High", + ], + "name": "high", + }, + { + "keys": [ + "Medium", + ], + "name": "medium", + }, + { + "keys": [ + "Low", + ], + "name": "low", + }, + ], + }, ], "template": [ "bug.yml", diff --git a/test/unit/issue-form.fixture.ts b/test/unit/issue-form.fixture.ts new file mode 100644 index 0000000..38b9ad7 --- /dev/null +++ b/test/unit/issue-form.fixture.ts @@ -0,0 +1,14 @@ +export const emptyForm = {}; + +export const basicForm = { + title: 'Test issue', + body: 'Test body', +}; + +export const dropdownForm = { + title: 'Test issue', + body: 'Test body', + severity: 'High', + type: 'Bug Report, Feature Request, other, none', + checkList: ['one', 'two', 'three'], +}; diff --git a/test/unit/issue-form.test.ts b/test/unit/issue-form.test.ts index 2a4352b..0239d69 100644 --- a/test/unit/issue-form.test.ts +++ b/test/unit/issue-form.test.ts @@ -1,24 +1,10 @@ import { describe, expect, test } from 'vitest'; import { IssueForm } from '../../src/issue-form'; - -const emptyForm = {}; - -const basicForm = { - title: 'Test issue', - body: 'Test body', -}; - -const dropdownForm = { - title: 'Test issue', - body: 'Test body', - severity: 'high', - multiDropdown: 'one, two, other, none', - checkList: ['one', 'two', 'three'], -}; +import { basicForm, dropdownForm } from './issue-form.fixture'; describe('Test IssueForm class', () => { - test('IssueForm class is defined', () => { + test('smoke test', () => { const issueForm = new IssueForm(basicForm); expect(issueForm.parsed).toMatchInlineSnapshot(` @@ -43,7 +29,7 @@ describe('Test IssueForm class', () => { `"Test issue"` ); expect(issueForm.getSafeProperty('severity')).toMatchInlineSnapshot( - `"high"` + `"High"` ); expect(issueForm.getSafeProperty('nonexistent')).toBeUndefined(); }); @@ -53,17 +39,17 @@ describe('Test IssueForm class', () => { expect(issueForm.listKeywords('severity', ['none'])).toMatchInlineSnapshot(` [ - "high", + "High", ] `); - expect(issueForm.listKeywords('multiDropdown', ['other', 'none'])) + expect(issueForm.listKeywords('type', ['other', 'none'])) .toMatchInlineSnapshot(` - [ - "one", - "two", - ] - `); + [ + "Bug Report", + "Feature Request", + ] + `); // Checklists are unsupported at the moment expect(issueForm.listKeywords('checkList', [])).toBeUndefined(); diff --git a/test/unit/labeler.test.ts b/test/unit/labeler.test.ts new file mode 100644 index 0000000..e5e31c2 --- /dev/null +++ b/test/unit/labeler.test.ts @@ -0,0 +1,196 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; + +import { Config } from '../../src/config'; +import { IssueForm } from '../../src/issue-form'; +import { Labeler } from '../../src/labeler'; + +import { basicConfig, emptyConfig, templateConfig } from './config.fixture'; +import { basicForm, dropdownForm } from './issue-form.fixture'; + +describe('Test Labeler class', () => { + afterEach(() => { + vi.unstubAllEnvs(); + }); + + test('smoke test', () => { + const labeler = new Labeler( + new IssueForm(basicForm), + new Config(basicConfig, '.github/issue-labeler.yml') + ); + + expect(labeler).toBeInstanceOf(Labeler); + }); + + describe('input based labeling', () => { + let labeler: Labeler; + + beforeEach(() => { + vi.stubEnv('INPUT_SECTION', 'type'); + vi.stubEnv('INPUT_BLOCK-LIST', 'other\nnone'); + + labeler = new Labeler( + new IssueForm(dropdownForm), + new Config(emptyConfig, '.github/issue-labeler.yml') + ); + }); + + test('initialization', () => { + expect(labeler.isConfig).toBe(false); + expect(labeler.isInputs).toBe(true); + + expect(labeler.section).toBe('type'); + expect(labeler.blockList).toMatchInlineSnapshot(` + [ + "other", + "none", + ] + `); + + expect(labeler.outputPolicy).toMatchInlineSnapshot(` + { + "section": {}, + "template": "", + } + `); + }); + + test('gatherLabels()', () => { + const keywords = labeler.gatherLabels(); + + expect(keywords).toMatchInlineSnapshot(` + [ + "Bug Report", + "Feature Request", + ] + `); + }); + + describe('inputBasedLabels()', () => { + test('get labels', () => { + const keywords = labeler.inputBasedLabels(); + + expect(labeler.outputPolicy).toMatchInlineSnapshot(` + { + "section": { + "type": [ + "Bug Report", + "Feature Request", + ], + }, + "template": "", + } + `); + expect(keywords).toMatchInlineSnapshot(` + [ + "Bug Report", + "Feature Request", + ] + `); + }); + + test('empty section or blocklist', () => { + labeler.section = ''; + + expect(() => labeler.inputBasedLabels()).toThrowError( + 'Section or block list is undefined!' + ); + + labeler.section = 'type'; + labeler.blockList = undefined; + + expect(() => labeler.inputBasedLabels()).toThrowError( + 'Section or block list is undefined!' + ); + }); + + test('empty section field', () => { + labeler.section = 'nonexistent'; + + const keywords = labeler.inputBasedLabels(); + + expect(labeler.outputPolicy).toMatchInlineSnapshot(` + { + "section": {}, + "template": "", + } + `); + expect(keywords).toBeUndefined(); + }); + }); + }); + + describe('policy based labeling', () => { + let labeler: Labeler; + + beforeEach(() => { + vi.stubEnv('INPUT_TEMPLATE', 'bug.yml'); + + labeler = new Labeler( + new IssueForm(dropdownForm), + new Config(templateConfig, '.github/issue-labeler.yml') + ); + }); + + test('initialization', () => { + expect(labeler.isConfig).toBe(true); + expect(labeler.isInputs).toBe(false); + + expect(labeler.outputPolicy).toMatchInlineSnapshot(` + { + "section": {}, + "template": "bug.yml", + } + `); + }); + + test('gatherLabels()', () => { + const labels = labeler.gatherLabels(); + + expect(labels).toMatchInlineSnapshot(` + [ + "bug 🐛", + "RFE 🎁", + "high", + ] + `); + }); + + describe('configBasedLabels()', () => { + test('get labels', () => { + const labels = labeler.configBasedLabels(); + + expect(labeler.outputPolicy).toMatchInlineSnapshot(` + { + "section": { + "severity": [ + "high", + ], + "type": [ + "bug 🐛", + "RFE 🎁", + ], + }, + "template": "bug.yml", + } + `); + expect(labels).toMatchInlineSnapshot(` + [ + "bug 🐛", + "RFE 🎁", + "high", + ] + `); + }); + + test('no policy provided', () => { + labeler.config.policy = undefined; + + expect(() => + labeler.configBasedLabels() + ).toThrowErrorMatchingInlineSnapshot( + `[Error: Missing configuration. Please setup 'Advanced Issue Labeler' Action using '.github/issue-labeler.yml' file.]` + ); + }); + }); + }); +});