From 84bdc2aab3dc8a7ef45a551ca7e7a1a6f3307331 Mon Sep 17 00:00:00 2001
From: thsaravana
Date: Wed, 18 Sep 2024 18:52:57 +0200
Subject: [PATCH] Adding tests for validation
---
__tests__/action.test.ts | 100 +++++++++++++++++++++++------------
lib/__tests__/action.test.js | 90 ++++++++++++++++++++-----------
2 files changed, 127 insertions(+), 63 deletions(-)
diff --git a/__tests__/action.test.ts b/__tests__/action.test.ts
index 674629e..a7664a9 100644
--- a/__tests__/action.test.ts
+++ b/__tests__/action.test.ts
@@ -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'
@@ -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':
@@ -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':
@@ -70,7 +82,6 @@ describe('Input validation', function () {
})
github.context.eventName = 'pull_request'
- // @ts-ignore
core.setFailed = jest.fn(c => {
expect(c).toEqual("'paths' is missing")
})
@@ -78,7 +89,6 @@ describe('Input validation', function () {
})
it('Fail if token is not present', async () => {
- // @ts-ignore
core.getInput = jest.fn(c => {
switch (c) {
case 'token':
@@ -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'
+}
diff --git a/lib/__tests__/action.test.js b/lib/__tests__/action.test.js
index e800546..c0a965a 100644
--- a/lib/__tests__/action.test.js
+++ b/lib/__tests__/action.test.js
@@ -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':
@@ -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':
@@ -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':
@@ -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';
+}