Skip to content

Commit

Permalink
Adding tests for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
thsaravana committed Sep 18, 2024
1 parent c02cf71 commit 84bdc2a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 63 deletions.
100 changes: 67 additions & 33 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import * as action from '../src/action'
import * as core from '@actions/core'
import * as github from '@actions/github'
Expand All @@ -6,6 +8,19 @@ jest.mock('@actions/core')
jest.mock('@actions/github')

describe('Input validation', function () {
const eventName = 'pull_request'
const payload = {
pull_request: {
number: '45',
base: {
sha: 'guasft7asdtf78asfd87as6df7y2u3',
},
head: {
sha: 'aahsdflais76dfa78wrglghjkaghkj',
},
},
}

function getInput(key: string): string | undefined {
switch (key) {
case 'paths':
Expand All @@ -19,47 +34,44 @@ describe('Input validation', function () {
const listComments = jest.fn()
const updateComment = jest.fn()

/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
core.getInput = jest.fn(getInput)
// @ts-ignore
github.getOctokit = jest.fn(() => {
return {
repos: {
compareCommits: jest.fn(() => {
return {
data: {
files: [
{
filename: 'src/main/kotlin/com/madrapps/jacoco/Math.kt',
blob_url:
'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/kotlin/com/madrapps/jacoco/Math.kt',
},
{
filename:
'src/main/java/com/madrapps/jacoco/operation/StringOp.java',
blob_url:
'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/java/com/madrapps/jacoco/operation/StringOp.java',
},
],
},
}
}),
},
issues: {
createComment,
listComments,
updateComment,
rest: {
repos: {
compareCommits: jest.fn(() => {
return {
data: {
files: [
{
filename: 'src/main/kotlin/com/madrapps/jacoco/Math.kt',
blob_url:
'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/kotlin/com/madrapps/jacoco/Math.kt',
},
{
filename:
'src/main/java/com/madrapps/jacoco/operation/StringOp.java',
blob_url:
'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/java/com/madrapps/jacoco/operation/StringOp.java',
},
],
},
}
}),
},
issues: {
createComment,
listComments,
updateComment,
},
},
}
})
// @ts-ignore
core.setFailed = jest.fn(c => {
fail(c)
})

it('Fail if paths is not present', async () => {
// @ts-ignore
core.getInput = jest.fn(c => {
switch (c) {
case 'paths':
Expand All @@ -70,15 +82,13 @@ describe('Input validation', function () {
})
github.context.eventName = 'pull_request'

// @ts-ignore
core.setFailed = jest.fn(c => {
expect(c).toEqual("'paths' is missing")
})
await action.action()
})

it('Fail if token is not present', async () => {
// @ts-ignore
core.getInput = jest.fn(c => {
switch (c) {
case 'token':
Expand All @@ -88,10 +98,34 @@ describe('Input validation', function () {
}
})
github.context.eventName = 'pull_request'
// @ts-ignore
core.setFailed = jest.fn(c => {
expect(c).toEqual("'token' is missing")
})
await action.action()
})

it('Fail if comment-type is invalid', async () => {
core.getInput = jest.fn(c => {
switch (c) {
case 'comment-type':
return 'invalid'
default:
return getInput(c)
}
})
core.setFailed = jest.fn(c => {
expect(c).toEqual("'comment-type' invalid is invalid")
})
initContext(eventName, payload)

await action.action()
})
})

function initContext(eventName, payload): void {
const context = github.context
context.eventName = eventName
context.payload = payload
context.repo = 'jacoco-playground'
context.owner = 'madrapps'
}
90 changes: 60 additions & 30 deletions lib/__tests__/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
const action = __importStar(require("../src/action"));
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
jest.mock('@actions/core');
jest.mock('@actions/github');
describe('Input validation', function () {
const eventName = 'pull_request';
const payload = {
pull_request: {
number: '45',
base: {
sha: 'guasft7asdtf78asfd87as6df7y2u3',
},
head: {
sha: 'aahsdflais76dfa78wrglghjkaghkj',
},
},
};
function getInput(key) {
switch (key) {
case 'paths':
Expand All @@ -40,43 +54,40 @@ describe('Input validation', function () {
const createComment = jest.fn();
const listComments = jest.fn();
const updateComment = jest.fn();
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
core.getInput = jest.fn(getInput);
// @ts-ignore
github.getOctokit = jest.fn(() => {
return {
repos: {
compareCommits: jest.fn(() => {
return {
data: {
files: [
{
filename: 'src/main/kotlin/com/madrapps/jacoco/Math.kt',
blob_url: 'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/kotlin/com/madrapps/jacoco/Math.kt',
},
{
filename: 'src/main/java/com/madrapps/jacoco/operation/StringOp.java',
blob_url: 'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/java/com/madrapps/jacoco/operation/StringOp.java',
},
],
},
};
}),
},
issues: {
createComment,
listComments,
updateComment,
rest: {
repos: {
compareCommits: jest.fn(() => {
return {
data: {
files: [
{
filename: 'src/main/kotlin/com/madrapps/jacoco/Math.kt',
blob_url: 'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/kotlin/com/madrapps/jacoco/Math.kt',
},
{
filename: 'src/main/java/com/madrapps/jacoco/operation/StringOp.java',
blob_url: 'https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/java/com/madrapps/jacoco/operation/StringOp.java',
},
],
},
};
}),
},
issues: {
createComment,
listComments,
updateComment,
},
},
};
});
// @ts-ignore
core.setFailed = jest.fn(c => {
fail(c);
});
it('Fail if paths is not present', async () => {
// @ts-ignore
core.getInput = jest.fn(c => {
switch (c) {
case 'paths':
Expand All @@ -86,14 +97,12 @@ describe('Input validation', function () {
}
});
github.context.eventName = 'pull_request';
// @ts-ignore
core.setFailed = jest.fn(c => {
expect(c).toEqual("'paths' is missing");
});
await action.action();
});
it('Fail if token is not present', async () => {
// @ts-ignore
core.getInput = jest.fn(c => {
switch (c) {
case 'token':
Expand All @@ -103,10 +112,31 @@ describe('Input validation', function () {
}
});
github.context.eventName = 'pull_request';
// @ts-ignore
core.setFailed = jest.fn(c => {
expect(c).toEqual("'token' is missing");
});
await action.action();
});
it('Fail if comment-type is invalid', async () => {
core.getInput = jest.fn(c => {
switch (c) {
case 'comment-type':
return 'invalid';
default:
return getInput(c);
}
});
core.setFailed = jest.fn(c => {
expect(c).toEqual("'comment-type' invalid is invalid");
});
initContext(eventName, payload);
await action.action();
});
});
function initContext(eventName, payload) {
const context = github.context;
context.eventName = eventName;
context.payload = payload;
context.repo = 'jacoco-playground';
context.owner = 'madrapps';
}

0 comments on commit 84bdc2a

Please sign in to comment.