Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncalteen committed Sep 23, 2023
1 parent 2b79730 commit 31ca1e7
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
4 changes: 4 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import * as main from '../src/main'
const runMock = jest.spyOn(main, 'run').mockImplementation()

describe('index', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('calls run when imported', async () => {
require('../src/index')

Expand Down
4 changes: 4 additions & 0 deletions __tests__/utils/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import { formatKey } from '../../src/utils/format'

describe('formatKey', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('removes non-alphanumeric characters', async () => {
expect(formatKey('!@#$%^&*()_+')).toBe('')
})
Expand Down
4 changes: 4 additions & 0 deletions __tests__/utils/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import * as parse from '../../src/utils/parse'
import { IssueFormTemplate } from '../../src/interfaces'

describe('parseTemplate', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('skips markdown fields', async () => {
const template: IssueFormTemplate = {
name: 'Example Request',
Expand Down
114 changes: 95 additions & 19 deletions __tests__/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import * as textarea from '../src/validate/textarea'
import * as dropdown from '../src/validate/dropdown'
import * as checkboxes from '../src/validate/checkboxes'

// Mock the action's entrypoint
jest.spyOn(input, 'validateInput').mockImplementation()
jest.spyOn(textarea, 'validateTextarea').mockImplementation()
jest.spyOn(dropdown, 'validateDropdown').mockImplementation()
jest.spyOn(checkboxes, 'validateCheckboxes').mockImplementation()

const config: string = `
validators:
- field: test
script: team
method: exists
`
// Mock Octokit
jest.mock('@octokit/rest', () => ({
Octokit: jest.fn()
}))

describe('validate', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('skips custom validation if no config is present', async () => {
jest.spyOn(input, 'validateInput').mockImplementation()
jest.spyOn(textarea, 'validateTextarea').mockImplementation()
jest.spyOn(dropdown, 'validateDropdown').mockImplementation()
jest.spyOn(checkboxes, 'validateCheckboxes').mockImplementation()
jest.spyOn(fs, 'existsSync').mockReturnValue(false)

const errors: string[] = await validate(
Expand All @@ -42,30 +42,106 @@ describe('validate', () => {
expect(errors).toEqual([])
})

it('runs custom validation if config is present', async () => {
it('passes custom validation if config is present and inputs are valid', async () => {
jest.spyOn(input, 'validateInput').mockImplementation()
jest.spyOn(textarea, 'validateTextarea').mockImplementation()
jest.spyOn(dropdown, 'validateDropdown').mockImplementation()
jest.spyOn(checkboxes, 'validateCheckboxes').mockImplementation()
jest.spyOn(fs, 'existsSync').mockReturnValue(true)
jest.spyOn(fs, 'readFileSync').mockReturnValue(config)
jest.spyOn(require(`${process.cwd()}/.github/validator/team`), 'exists')

const mocktokit = {
rest: {
teams: {
getByName: jest.fn().mockResolvedValue({
data: {
id: 1234
}
})
}
}
}

jest
.spyOn(require(`${process.cwd()}/.github/validator/team`), 'exists')
.mockReturnValue('success')
.spyOn(require('@octokit/rest'), 'Octokit')
.mockImplementation(() => mocktokit)

const errors: string[] = await validate(
{
test: {
read_team: {
type: 'input',
required: true
},
write_team: {
type: 'input',
required: true
}
},
{
test: 'test'
read_team: 'IssueOps-Demo-Readers',
write_team: 'IssueOps-Demo-Writers'
},
process.cwd()
)

expect(errors).toEqual([])
expect(
require(`${process.cwd()}/.github/validator/team`).exists
).toHaveBeenCalledWith('IssueOps-Demo-Readers')
expect(
require(`${process.cwd()}/.github/validator/team`).exists
).toHaveBeenCalledWith('IssueOps-Demo-Writers')
})

it('fails custom validation if config is present and inputs are invalid', async () => {
jest.spyOn(input, 'validateInput').mockImplementation()
jest.spyOn(textarea, 'validateTextarea').mockImplementation()
jest.spyOn(dropdown, 'validateDropdown').mockImplementation()
jest.spyOn(checkboxes, 'validateCheckboxes').mockImplementation()
jest.spyOn(fs, 'existsSync').mockReturnValue(true)
jest.spyOn(require(`${process.cwd()}/.github/validator/team`), 'exists')

const mocktokit = {
rest: {
teams: {
getByName: jest.fn().mockRejectedValue({
status: 404
})
}
}
}

jest
.spyOn(require('@octokit/rest'), 'Octokit')
.mockImplementation(() => mocktokit)

const errors: string[] = await validate(
{
read_team: {
type: 'input',
required: true
},
write_team: {
type: 'input',
required: true
}
},
{
read_team: 'IssueOps-Demo-Readers',
write_team: 'IssueOps-Demo-Writers'
},
process.cwd()
)

expect(errors).toEqual([
'Invalid read_team: Team IssueOps-Demo-Readers does not exist',
'Invalid write_team: Team IssueOps-Demo-Writers does not exist'
])
expect(
require(`${process.cwd()}/.github/validator/team`).exists
).toHaveBeenCalledWith('IssueOps-Demo-Readers')
expect(
require(`${process.cwd()}/.github/validator/team`).exists
).toHaveBeenCalledWith('test')
).toHaveBeenCalledWith('IssueOps-Demo-Writers')
})
})
1 change: 1 addition & 0 deletions __tests__/validate/checkboxes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('checkboxes', () => {
let errors: string[] = []

beforeEach(() => {
jest.clearAllMocks()
errors = []
})

Expand Down
1 change: 1 addition & 0 deletions __tests__/validate/dropdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('dropdown', () => {
let errors: string[] = []

beforeEach(() => {
jest.clearAllMocks()
errors = []
})

Expand Down
1 change: 1 addition & 0 deletions __tests__/validate/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('input', () => {
let errors: string[] = []

beforeEach(() => {
jest.clearAllMocks()
errors = []
})

Expand Down
1 change: 1 addition & 0 deletions __tests__/validate/textarea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('textarea', () => {
let errors: string[] = []

beforeEach(() => {
jest.clearAllMocks()
errors = []
})

Expand Down

0 comments on commit 31ca1e7

Please sign in to comment.