Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Added integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Luuk van Houdt committed May 15, 2021
1 parent c78bc51 commit 88d77ba
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 75 deletions.
42 changes: 26 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ const core = require("@actions/core");
const github = require("@actions/github");

if (require.main === module) {
run();
const getListReleases = (token) =>
github.getOctokit(token).repos.listReleases;
run({
getInput: core.getInput,
setOutput: core.setOutput,
listReleases: getListReleases,
context: github.context.repo,
});
}

async function run(inject = {}) {
async function run({
getInput,
setOutput,
listReleases,
context,
}) {
try {
const getInput = inject.getInput || core.getInput;
const setOutput = inject.setOutput || core.setOutput;
const getClient = inject.getClient || github.getOctokit;
const context = inject.context || github.context.repo;

const token = getInput("token");
const client = getClient(token);

const { data } = await client.repos.listReleases(context);
console.info("Started retrieving releases");
const request = listReleases(token);
const { data } = await request(context);

const { changelog, latest } = getChangelogAndLatest(data, inject);
const { changelog, latest } = getChangelogAndLatest(data, {getInput});

setOutput("changelog", changelog);
setOutput("latest", latest);
Expand All @@ -26,17 +34,19 @@ async function run(inject = {}) {
}
}

function getChangelogAndLatest(releases, inject) {
function getChangelogAndLatest(releases, {getInput}) {
if (!Array.isArray(releases)) {
throw new Error(`Expected array but got ${typeof releases}`);
throw new Error(
`Expected an array back as response, but got "${typeof releases}"`
);
}

const getInput = inject.getInput || core.getInput;

const spacing = "\n\n";
const latest = { tag: null, date: null };
const changelog = releases
.map(({ tag_name, draft, published_at, name, body }) => {
if (draft) {
console.info(`Skipping draft with the name "${name}"`);
return null;
}

Expand All @@ -49,10 +59,10 @@ function getChangelogAndLatest(releases, inject) {
const title = formatTitle(name, getInput);
const description = formatDescription(body, getInput);

return title + "\n\n" + description;
return [title, description].filter(Boolean).join(spacing);
})
.filter(Boolean)
.join("\n\n");
.join(spacing);

return { changelog, latest: latest.tag };
}
Expand Down
162 changes: 103 additions & 59 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,165 @@
const { expect, test } = require("@jest/globals");
const { getChangelogAndLatest } = require(".");

const v0 = {
draft: true,
published_at: "2012-02-27T19:35:32Z",
tag_name: "v0",
name: "title v0",
body: "description v0",
};
const v1 = {
draft: false,
published_at: "2013-02-27T19:35:32Z",
tag_name: "v1",
name: "title v1",
body: "description v1",
};
const v2 = {
draft: false,
published_at: "2014-02-27T19:35:32Z",
tag_name: "v2",
name: "title v2",
body: "description v2",
};
const { getChangelogAndLatest, run } = require(".");
const fixture = require("./raw-fixture.json");

const templates = {
"title-template": "%%TITLE%%",
const inputs = {
"title-template": "# %%TITLE%%",
"description-template": "%%DESCRIPTION%%",
};
const outputs = {};

const request = jest.fn().mockResolvedValue({ data: fixture });
const injections = {
getInput: jest.fn((tag) => templates[tag]),
getInput: jest.fn((k) => inputs[k]),
setOutput: jest.fn((k, v) => (outputs[k] = v)),
listReleases: jest.fn(() => request),
};

describe("run()", () => {
beforeEach(() => run(injections));

test("base test, returns changelog", async () => {
expect(request).toHaveBeenCalledTimes(1);
expect(injections.listReleases).toHaveBeenCalledTimes(1);
expect(injections.setOutput).toHaveBeenCalledTimes(2);
expect(outputs).toHaveProperty("changelog");
expect(outputs.changelog).not.toBeNull();
});

test("returns latest tag name", () => {
expect(outputs).toHaveProperty("latest");
expect(outputs.latest).toBe("0.1.5");
});
});

describe("getChangelogAndLatest()", () => {
const spacing = "\n\n";
const releases = {
v0: {
draft: true,
published_at: "2012-02-27T19:35:32Z",
tag_name: "v0",
name: "title v0",
body: "description v0",
},
v1: {
draft: false,
published_at: "2013-02-27T19:35:32Z",
tag_name: "v1",
name: "title v1",
body: "description v1",
},
v2: {
draft: false,
published_at: "2014-02-27T19:35:32Z",
tag_name: "v2",
name: "title v2",
body: "description v2",
},
};
test("with one valid entry", () => {
const { changelog, latest } = getChangelogAndLatest([v1], injections);
const { changelog, latest } = getChangelogAndLatest(
[releases.v1],
injections
);

expect(latest).toBe(v1.tag_name);
expect(latest).toBe(releases.v1.tag_name);

const expected = [v1.name, v1.body].join("\n\n");
const expected = [releases.v1.name, releases.v1.body].join(spacing);
expect(changelog).toBe(expected);
});

test("with two valid entries", () => {
const { changelog, latest } = getChangelogAndLatest([v1, v2], injections);

expect(latest).toBe(v2.tag_name);

const expected = [v1.name, v1.body, v2.name, v2.body].join("\n\n");
const { changelog, latest } = getChangelogAndLatest(
[releases.v1, releases.v2],
injections
);

expect(latest).toBe(releases.v2.tag_name);

const expected = [
releases.v1.name,
releases.v1.body,
releases.v2.name,
releases.v2.body,
].join(spacing);
expect(changelog).toBe(expected);
});

test("with one valid draft and two valid entries", () => {
const { changelog, latest } = getChangelogAndLatest([v0, v1, v2], injections);

expect(latest).toBe(v2.tag_name);

const expected = [v1.name, v1.body, v2.name, v2.body].join("\n\n");
const { changelog, latest } = getChangelogAndLatest(
[releases.v0, releases.v1, releases.v2],
injections
);

expect(latest).toBe(releases.v2.tag_name);

const expected = [
releases.v1.name,
releases.v1.body,
releases.v2.name,
releases.v2.body,
].join(spacing);
expect(changelog).toBe(expected);
});

describe("invalid entries:", () => {
test("with an invalid date string for `published_at`", () => {
const actual = {...v1, ...{published_at: 'foobar'}};
const actual = { ...releases.v1, ...{ published_at: "foobar" } };

const { changelog, latest } = getChangelogAndLatest([actual], injections);

expect(latest).toBe(actual.tag_name);
const expected = [actual.name, actual.body].join("\n\n");

const expected = [actual.name, actual.body].join(spacing);
expect(changelog).toBe(expected);
});

test("without `name` property", () => {
const actual = {...{}, ...v1};
const actual = { ...{}, ...releases.v1 };
delete actual.name;

const { changelog, latest } = getChangelogAndLatest([actual], injections);

expect(latest).toBe(actual.tag_name);
const expected = [actual.name, actual.body].join("\n\n");

const expected = [actual.body].join(spacing);
expect(changelog).toBe(expected);
});

test("without `body` property", () => {
const actual = {...{}, ...v1};
const actual = { ...{}, ...releases.v1 };
delete actual.body;

const { changelog, latest } = getChangelogAndLatest([actual], injections);

expect(latest).toBe(actual.tag_name);
const expected = [actual.name, actual.body].join("\n\n");

const expected = [actual.name].join(spacing);
expect(changelog).toBe(expected);
});

test("without `tag_name` property", () => {
const actual = {...{}, ...v1};
const actual = { ...{}, ...releases.v1 };
delete actual.tag_name;

const { changelog, latest } = getChangelogAndLatest([actual], injections);

expect(latest).toBe(actual.tag_name);
const expected = [actual.name, actual.body].join("\n\n");

const expected = [actual.name, actual.body].join(spacing);
expect(changelog).toBe(expected);
});

test("without `published_at` property", () => {
const actual = {...{}, ...v1};
const actual = { ...{}, ...releases.v1 };
delete actual.published_at;

const { changelog, latest } = getChangelogAndLatest([actual], injections);

expect(latest).toBe(actual.tag_name);
const expected = [actual.name, actual.body].join("\n\n");

const expected = [actual.name, actual.body].join(spacing);
expect(changelog).toBe(expected);
});
});
Expand Down
Loading

0 comments on commit 88d77ba

Please sign in to comment.