mirror of
https://github.com/jimeh/update-tags-action.git
synced 2026-02-19 01:26:40 +00:00
wip(parse-version): initial test of parse_version input
This commit is contained in:
38
test/parseVersionAndRenderTags.test.js
Normal file
38
test/parseVersionAndRenderTags.test.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const semver = require("semver");
|
||||
const handlebars = require("handlebars");
|
||||
const { parseVersionAndRenderTags } = require("../index"); // replace 'your-file' with the actual file name
|
||||
|
||||
describe("parseVersionAndRenderTags", () => {
|
||||
it("handles empty parseVersion input", () => {
|
||||
const tags = "v1.0.0";
|
||||
expect(parseVersionAndRenderTags("", tags)).toBe(tags);
|
||||
});
|
||||
|
||||
it('removes "refs/tags/" prefix from parseVersion', () => {
|
||||
const parseVersion = "refs/tags/v1.2.3";
|
||||
const tags = "M={{major}}, m={{minor}}, p={{patch}}";
|
||||
const expectedTags = "M=1, m=2, p=3";
|
||||
expect(parseVersionAndRenderTags(parseVersion, tags)).toBe(expectedTags);
|
||||
});
|
||||
|
||||
it("throws an error if parseVersion is invalid and tags contains placeholders", () => {
|
||||
const parseVersion = "invalid-version-string";
|
||||
const tags = "{{major}}.{{minor}}.{{patch}}";
|
||||
expect(() => parseVersionAndRenderTags(parseVersion, tags)).toThrow(
|
||||
`Invalid version string: ${parseVersion}`
|
||||
);
|
||||
});
|
||||
|
||||
it("does not throw an error if parseVersion is invalid and tags does not contain placeholders", () => {
|
||||
const parseVersion = "invalid-version-string";
|
||||
const tags = "v1.0.0";
|
||||
expect(parseVersionAndRenderTags(parseVersion, tags)).toBe(tags);
|
||||
});
|
||||
|
||||
it("renders the tags template with the parsed version", () => {
|
||||
const parseVersion = "v1.2.3";
|
||||
const tags = "M={{major}}, m={{minor}}, p={{patch}}";
|
||||
const expectedTags = "M=1, m=2, p=3";
|
||||
expect(parseVersionAndRenderTags(parseVersion, tags)).toBe(expectedTags);
|
||||
});
|
||||
});
|
||||
54
test/resolveRefToSha.test.js
Normal file
54
test/resolveRefToSha.test.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const nock = require("nock");
|
||||
const { resolveRefToSha } = require("../index");
|
||||
|
||||
describe("resolveRefToSha", () => {
|
||||
const octokit = { rest: { repos: { getCommit: jest.fn() } } };
|
||||
const owner = "testOwner";
|
||||
const repo = "testRepo";
|
||||
const ref = "testRef";
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
test("it returns the correct SHA when the API call is successful", async () => {
|
||||
const expectedSha = "abc123";
|
||||
octokit.rest.repos.getCommit.mockResolvedValueOnce({
|
||||
data: { sha: expectedSha },
|
||||
});
|
||||
|
||||
const sha = await resolveRefToSha(octokit, owner, repo, ref);
|
||||
|
||||
expect(octokit.rest.repos.getCommit).toHaveBeenCalledWith({
|
||||
owner,
|
||||
repo,
|
||||
ref,
|
||||
});
|
||||
expect(sha).toEqual(expectedSha);
|
||||
});
|
||||
|
||||
test("it throws an error when the API call fails with a 404", async () => {
|
||||
octokit.rest.repos.getCommit.mockRejectedValueOnce({
|
||||
status: 404,
|
||||
message: "Not Found",
|
||||
});
|
||||
|
||||
await expect(resolveRefToSha(octokit, owner, repo, ref)).rejects.toThrow(
|
||||
"Failed to resolve ref"
|
||||
);
|
||||
});
|
||||
|
||||
test("it throws an error when the API call fails with a 500", async () => {
|
||||
octokit.rest.repos.getCommit.mockRejectedValueOnce({
|
||||
status: 500,
|
||||
message: "Internal Server Error",
|
||||
});
|
||||
|
||||
await expect(resolveRefToSha(octokit, owner, repo, ref)).rejects.toThrow(
|
||||
"Failed to resolve ref"
|
||||
);
|
||||
});
|
||||
|
||||
// Add more test cases as needed for other error scenarios
|
||||
});
|
||||
Reference in New Issue
Block a user