Skip to content

Commit

Permalink
test: update tideliftMeUpCli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maggienegm committed Jan 29, 2025
1 parent 4ad0367 commit f9477fd
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/cli/tideliftMeUpCli.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from "vitest";

import { tideliftMeUpCli } from "./tideliftMeUpCli.js";
import chalk from "chalk";

const mockGetNpmWhoami = vi.fn();

Expand Down Expand Up @@ -65,10 +66,14 @@ describe("tideliftMeUpCli", () => {
);
});

it("logs packages for a username when --username is provided", async () => {
const username = "abc123";
it("logs message when an invalid --username is provided", async () => {
const username = "#JI*#@%OJSL";

const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);

mockTideliftMeUp.mockResolvedValue([]);
mockTideliftMeUp.mockImplementation(() => {
throw new Error(`No packages found for npm username: ${username}.`);
});

await tideliftMeUpCli(["--username", username]);

Expand All @@ -77,19 +82,56 @@ describe("tideliftMeUpCli", () => {
expect(mockTideliftMeUp).toHaveBeenCalledWith({
username,
});
expect(logger).toHaveBeenCalledWith(
chalk.red(`Could not find packages for ${username}`),
);

logger.mockRestore();
});

it("logs packages for a username when --username is not provided and the user is logged in", async () => {
it("logs message when valid --username is provided and user has no packages", async () => {
const username = "abc123";

const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);

mockTideliftMeUp.mockImplementation(() => {
throw new Error(`No packages found for npm username: ${username}.`);
});

await tideliftMeUpCli(["--username", username]);

expect(mockLogHelp).not.toHaveBeenCalled();
expect(mockGetNpmWhoami).not.toHaveBeenCalled();
expect(mockTideliftMeUp).toHaveBeenCalledWith({
username,
});
expect(logger).toHaveBeenCalledWith(
chalk.red(`Could not find packages for ${username}`),
);

logger.mockRestore();
});

it("logs message when --username is not provided, user is logged in and user has no packages", async () => {
const username = "abc123";

const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);

mockGetNpmWhoami.mockResolvedValue(username);
mockTideliftMeUp.mockResolvedValue([]);
mockTideliftMeUp.mockImplementation(() => {
throw new Error(`No packages found for npm username: ${username}.`);
});

await tideliftMeUpCli([]);

expect(mockLogHelp).not.toHaveBeenCalled();
expect(mockTideliftMeUp).toHaveBeenCalledWith({
username,
});
expect(logger).toHaveBeenCalledWith(
chalk.red(`Could not find packages for ${username}`),
);

logger.mockRestore();
});
});

0 comments on commit f9477fd

Please sign in to comment.