refactor: improve changelog logic (#197)

* refactor: improve changelog logic

Signed-off-by: Ardalan Amini <[email protected]>

* chore: update license comment formatting across all files

Replaced `/*` with `/**` for correct JSDoc-style license comments in all files.
This ensures consistency and compatibility with documentation tools that rely on JSDoc conventions.

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for inputs

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for utils

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for outputs

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for nodes

Signed-off-by: Ardalan Amini <[email protected]>

* test: add missing unit tests

Signed-off-by: Ardalan Amini <[email protected]>

* chore: address PR comments

Signed-off-by: Ardalan Amini <[email protected]>

* chore: address PR comments

Signed-off-by: Ardalan Amini <[email protected]>

---------

Signed-off-by: Ardalan Amini <[email protected]>
This commit is contained in:
Ardalan Amini authored and GitHub committed 2025-06-07 15:37:31 +03:30
1 parent 3c1177539c
commit 9bd6b8698c
95 files changed
+4381 -302

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { booleanInput, cache } from "../../src/utils";
it("should get and parse a boolean input", () => {
const inputName = "foo";
const inputValue = true;
jest.mocked(getBooleanInput).mockReturnValueOnce(inputValue);
const result = booleanInput(inputName);
expect(result).toEqual(inputValue);
expect(getBooleanInput).toHaveBeenCalledTimes(1);
expect(getBooleanInput).toHaveBeenCalledWith(inputName, { required: true });
});
it("should get a cached boolean input", () => {
const inputName = "foo";
const inputValue = true;
cache(inputName, () => inputValue);
const result = booleanInput(inputName);
expect(result).toEqual(inputValue);
expect(getBooleanInput).toHaveBeenCalledTimes(0);
});
it("should get and parse an optional boolean input", () => {
const inputName = "foo";
const inputValue = false;
jest.mocked(getBooleanInput).mockReturnValueOnce(inputValue);
const result = booleanInput(inputName, false);
expect(result).toEqual(inputValue);
expect(getBooleanInput).toHaveBeenCalledTimes(1);
expect(getBooleanInput).toHaveBeenCalledWith(inputName, { required: false });
});
+90
View File
@@ -0,0 +1,90 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { cache, clearCache } from "../../src/utils";
it("should cache value", () => {
const name = "foo";
const value = "bar";
const fn = jest.fn(() => value);
const firstCallResult = cache(name, fn);
expect(firstCallResult).toBe(value);
expect(fn).toHaveBeenCalledTimes(1);
fn.mockClear();
const secondCallResult = cache(name, fn);
expect(secondCallResult).toBe(value);
expect(fn).toHaveBeenCalledTimes(0);
});
it("should overwrite cache value", () => {
const name = "foo";
const value = "bar";
const fn = jest.fn(() => value);
const firstCallResult = cache(name, fn);
expect(firstCallResult).toBe(value);
expect(fn).toHaveBeenCalledTimes(1);
fn.mockClear();
const secondCallResult = cache(name, fn, true);
expect(secondCallResult).toBe(value);
expect(fn).toHaveBeenCalledTimes(1);
});
it("should clear cache", () => {
const name = "foo";
const value = "bar";
const fn = jest.fn(() => value);
const firstCallResult = cache(name, fn);
expect(firstCallResult).toBe(value);
expect(fn).toHaveBeenCalledTimes(1);
fn.mockClear();
clearCache();
const secondCallResult = cache(name, fn);
expect(secondCallResult).toBe(value);
expect(fn).toHaveBeenCalledTimes(1);
});
+92
View File
@@ -0,0 +1,92 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getInput } from "@actions/core";
import { getOctokit } from "@actions/github";
import { GitHub } from "@actions/github/lib/utils";
import fetchMock from "fetch-mock";
import { iterateCommits } from "../../src/utils";
it("should iterate over commits", async () => {
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const gitHubTokenInputName = "github-token";
const gitHubTokenInputValue = "github-token-value";
jest.mocked(getInput).mockImplementationOnce((name) => {
if (name === gitHubTokenInputName) return gitHubTokenInputValue;
return "";
});
const commit = {
author: {
login: "ardalanamini",
},
commit: {
message: "ci: update test workflow\n\nSigned-off-by: Ardalan Amini <[email protected]>",
},
sha: "3c1177539c1a216084f922ea52e56dd719a25945",
};
const fromSha = "7a94b0db6150850cb1ae7243b13a4c972e9db261";
const fetch = fetchMock.getOnce(
"begin:https://api.github.com/repos/ardalanamini/auto-changelog/commits",
[
commit, {
author: {
login: "ardalanamini",
},
commit: {
message: "chore: update copyright year\n\nSigned-off-by: Ardalan Amini <[email protected]>",
},
sha: fromSha,
},
],
);
const gitHub = new GitHub({ request: { fetch: fetch.fetchHandler } });
jest.mocked(getOctokit).mockImplementationOnce(() => gitHub);
const result = iterateCommits(repo.owner, repo.repo, fromSha);
expect(await result.next()).toEqual({
done : false,
value: commit,
});
expect(await result.next()).toEqual({
done: true,
});
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith(gitHubTokenInputName, { required: true });
expect(getOctokit).toHaveBeenCalledTimes(1);
expect(getOctokit).toHaveBeenCalledWith(gitHubTokenInputValue);
});
+93
View File
@@ -0,0 +1,93 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getInput } from "@actions/core";
import { cache, input } from "../../src/utils";
it("should get input value", () => {
const name = "foo";
const value = "bar";
jest.mocked(getInput).mockReturnValueOnce(value);
const result = input(name);
expect(result).toBe(value);
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith(name, { required: true });
});
it("should get cached input value", () => {
const name = "foo";
const value = "bar";
cache(name, () => value);
const result = input(name);
expect(result).toBe(value);
expect(getInput).toHaveBeenCalledTimes(0);
});
it("should get optional input value", () => {
const name = "foo";
const value = "";
jest.mocked(getInput).mockReturnValueOnce(value);
const result = input(name, false);
expect(result).toBe(value);
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith(name, { required: false });
});
it("should get parsed input value", () => {
const name = "foo";
jest.mocked(getInput).mockReturnValueOnce("1");
const result = input(name, inputValue => +inputValue);
expect(result).toBe(1);
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith(name, { required: true });
});
it("should get parsed optional input value", () => {
const name = "foo";
jest.mocked(getInput).mockReturnValueOnce("");
const result = input(name, inputValue => +(inputValue || "1"), false);
expect(result).toBe(1);
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith(name, { required: false });
});
+51
View File
@@ -0,0 +1,51 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getInput } from "@actions/core";
import { getOctokit } from "@actions/github";
import { GitHub } from "@actions/github/lib/utils";
import { octokit } from "../../src/utils";
it("should get octokit instance", () => {
const gitHubTokenInputName = "github-token";
const gitHubTokenInputValue = "github-token-value";
jest.mocked(getInput).mockImplementationOnce((name) => {
if (name === gitHubTokenInputName) return gitHubTokenInputValue;
return "";
});
jest.mocked(getOctokit).mockImplementationOnce(() => new GitHub);
const result = octokit();
expect(result).toBeInstanceOf(GitHub);
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith(gitHubTokenInputName, { required: true });
expect(getOctokit).toHaveBeenCalledTimes(1);
expect(getOctokit).toHaveBeenCalledWith(gitHubTokenInputValue);
});
+39
View File
@@ -0,0 +1,39 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { info, setOutput } from "@actions/core";
import { output } from "../../src/utils";
it("should output value", () => {
const name = "foo";
const value = "bar";
output(name, value);
expect(info).toHaveBeenCalledTimes(1);
expect(info).toHaveBeenCalledWith(`output -> ${ name }: ${ value }`);
expect(setOutput).toHaveBeenCalledTimes(1);
expect(setOutput).toHaveBeenCalledWith(name, value);
});
+1 -2
View File
@@ -1,4 +1,4 @@
/*
/**
* MIT License
*
* Copyright (c) 2024-2025 Ardalan Amini
@@ -20,7 +20,6 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/* eslint-disable no-undefined */
+104
View File
@@ -0,0 +1,104 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getInput } from "@actions/core";
import { parseSemVer } from "../../src/utils";
it("should parse the semver version string", () => {
const major = 1;
const minor = 2;
const patch = 3;
const prerelease = "alpha";
const prereleaseVersion = 4;
const version = `${ major }.${ minor }.${ patch }-${ prerelease }.${ prereleaseVersion }`;
const raw = `v${ version }`;
const releaseNamePrefix = "@actions/github/";
jest.mocked(getInput).mockImplementationOnce((name) => {
if (name === "release-name-prefix") return releaseNamePrefix;
return "";
});
const result = parseSemVer(raw);
expect(result).toBeDefined();
expect(result).toEqual({
build : [],
includePrerelease: true,
loose : false,
major,
minor,
options : { includePrerelease: true },
patch,
prerelease : [prerelease, prereleaseVersion],
raw,
version,
});
expect(getInput).toHaveBeenCalledTimes(1);
expect(getInput).toHaveBeenCalledWith("release-name-prefix", { required: false });
});
it("should parse the release name by default", () => {
const major = 1;
const minor = 2;
const patch = 3;
const prerelease = "alpha";
const prereleaseVersion = 4;
const version = `${ major }.${ minor }.${ patch }-${ prerelease }.${ prereleaseVersion }`;
const raw = `v${ version }`;
const releaseNamePrefix = "@actions/github/";
jest.mocked(getInput).mockImplementation((name) => {
switch (name) {
case "release-name":
return raw;
case "release-name-prefix":
return releaseNamePrefix;
default:
return "";
}
});
const result = parseSemVer();
expect(result).toBeDefined();
expect(result).toEqual({
build : [],
includePrerelease: true,
loose : false,
major,
minor,
options : { includePrerelease: true },
patch,
prerelease : [prerelease, prereleaseVersion],
raw,
version,
});
expect(getInput).toHaveBeenCalledTimes(2);
expect(getInput).toHaveBeenNthCalledWith(1, "release-name", { required: true });
expect(getInput).toHaveBeenNthCalledWith(2, "release-name-prefix", { required: false });
});
+46
View File
@@ -0,0 +1,46 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { context } from "@actions/github";
import { repository } from "../../src/utils";
it("should get the repository information with constructed URL", () => {
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const serverUrl = context.serverUrl;
expect(serverUrl).toEqual("https://github.com");
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
const result = repository();
expect(result).toEqual({
...repo,
url: `${ serverUrl }/${ repo.owner }/${ repo.repo }`,
});
});
+63
View File
@@ -0,0 +1,63 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { context } from "@actions/github";
import { sha } from "../../src/utils";
it("should get the current commit sha", () => {
const shaValue = "3c1177539c1a216084f922ea52e56dd719a25945";
const original = jest.mocked(context).sha;
jest.mocked(context).sha = shaValue;
const result = sha();
jest.mocked(context).sha = original;
expect(result).toBe(shaValue);
});
it("should cache the sha value", () => {
const shaValue: string = "3c1177539c1a216084f922ea52e56dd719a25945";
const original = jest.mocked(context).sha;
jest.mocked(context).sha = shaValue;
// The first call should access context.sha
const result1 = sha();
// Change the mocked value
jest.mocked(context).sha = "different-sha";
// Second call should return cached value, not the new mocked value
const result2 = sha();
jest.mocked(context).sha = original;
expect(result1).toBe(shaValue);
// Should be cached
expect(result2).toBe(shaValue);
});
+54
View File
@@ -0,0 +1,54 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { trim } from "../../src/utils/trim.js";
it("should remove leading and trailing whitespace", () => {
expect(trim(" hello ")).toBe("hello");
});
it("should replace multiple spaces with a single space", () => {
expect(trim("hello world")).toBe("hello world");
});
it("should handle null value", () => {
expect(trim(null)).toBeNull();
});
it("should handle undefined value", () => {
// eslint-disable-next-line no-undefined,@typescript-eslint/no-confusing-void-expression
expect(trim(undefined)).toBeUndefined();
});
it("should handle empty string", () => {
expect(trim("")).toBe("");
});
it("should handle string with no extra spaces", () => {
expect(trim("hello")).toBe("hello");
});
it("should handle strings with only spaces", () => {
expect(trim(" ")).toBe("");
});