-
Notifications
You must be signed in to change notification settings - Fork 97
test: add vitest and initial utils tests #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { | ||
| it, | ||
| describe, | ||
| expect, | ||
| vi, | ||
| beforeEach, | ||
| afterEach, | ||
| type MockInstance, | ||
| } from "vitest"; | ||
| import type { PackageManifest } from "query-registry"; | ||
| import * as utils from "./index.js"; | ||
|
|
||
| describe("utils", () => { | ||
| describe("extractOwnerAndRepo", () => { | ||
| it("is null for URLs with trailing characters", () => { | ||
| expect( | ||
| utils.extractOwnerAndRepo("https://github.com/org/repo.gitpewpew"), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("is null for URLs with leading characters", () => { | ||
| expect( | ||
| utils.extractOwnerAndRepo("pewpewhttps://github.com/org/repo.git"), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns org and repo for valid https URLs", () => { | ||
| expect( | ||
| utils.extractOwnerAndRepo("http://github.com/org/repo.git"), | ||
| ).toEqual(["org", "repo"]); | ||
| }); | ||
|
|
||
| it("returns org and repo for valid http URLs", () => { | ||
| expect( | ||
| utils.extractOwnerAndRepo("https://github.com/org/repo.git"), | ||
| ).toEqual(["org", "repo"]); | ||
| }); | ||
|
|
||
| it("returns org and repo for valid git+https URLs", () => { | ||
| expect( | ||
| utils.extractOwnerAndRepo("git+https://github.com/org/repo.git"), | ||
| ).toEqual(["org", "repo"]); | ||
| }); | ||
|
|
||
| it("returns org and repo for valid git+http URLs", () => { | ||
| expect( | ||
| utils.extractOwnerAndRepo("git+http://github.com/org/repo.git"), | ||
| ).toEqual(["org", "repo"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("extractRepository", () => { | ||
| it("returns undefined if no repository", () => { | ||
| expect(utils.extractRepository({} as PackageManifest)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined if repository is object with no URL", () => { | ||
| expect( | ||
| utils.extractRepository({ | ||
| repository: {}, | ||
| } as PackageManifest), | ||
| ).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns URL if repository is string", () => { | ||
| expect( | ||
| utils.extractRepository({ | ||
| repository: "foo", | ||
| } as PackageManifest), | ||
| ).toBe("foo"); | ||
| }); | ||
|
|
||
| it("returns URL if repository is object with URL", () => { | ||
| expect( | ||
| utils.extractRepository({ | ||
| repository: { | ||
| url: "foo", | ||
| }, | ||
| } as PackageManifest), | ||
| ).toBe("foo"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("abbreviateCommitHash", () => { | ||
| it("returns the first 7 characters of a hash", () => { | ||
| expect( | ||
| utils.abbreviateCommitHash("09efd0553374ff7d3e62b79378e3184f5eb57571"), | ||
| ).toBe("09efd05"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isPullRequest", () => { | ||
| it("returns true if ref is non-nan number", () => { | ||
| expect(utils.isPullRequest("808")).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false if ref is nan number", () => { | ||
| expect(utils.isPullRequest("foo")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isWhitelisted", () => { | ||
| let fetchSpy: MockInstance; | ||
| let whitelist: string; | ||
|
|
||
| beforeEach(() => { | ||
| whitelist = ""; | ||
| fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(() => { | ||
| return Promise.resolve(new Response(whitelist, { status: 200 })); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("should return true if repo is in whitelist", async () => { | ||
| whitelist = ` | ||
| foo/bar | ||
| org/repo | ||
| baz/zab | ||
| `; | ||
| const result = await utils.isWhitelisted("org", "repo"); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("should return false if repo is not in whitelist", async () => { | ||
| const result = await utils.isWhitelisted("org", "repo"); | ||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| it("should return false if fetch fails", async () => { | ||
| fetchSpy.mockRejectedValue(new Error("bleep bloop")); | ||
| const result = await utils.isWhitelisted("org", "repo"); | ||
| expect(result).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.