Skip to content

Commit

Permalink
test: fill in test coverage for comments.ts (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg authored Dec 1, 2024
1 parent de2274d commit 3e58c67
Showing 1 changed file with 92 additions and 29 deletions.
121 changes: 92 additions & 29 deletions src/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { describe, expect, it, vitest } from "vitest";

import { forEachComment } from "./comments";
import { createNodeAndSourceFile } from "./test/utils";
import { isTsVersionAtLeast } from "./utils";

describe("forEachComment", () => {
it("does not call the callback when the source has no comments", () => {
it("does not call the callback when the source is a variable with no comments", () => {
const { node, sourceFile } = createNodeAndSourceFile("let value;");
const callback = vitest.fn();

Expand All @@ -15,38 +14,102 @@ describe("forEachComment", () => {
expect(callback).not.toHaveBeenCalled();
});

if (isTsVersionAtLeast(4, 3)) {
it("calls the callback when the source has a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
// hello world
let value;
`);
const callback = vitest.fn();
it("calls the callback when the source is a variable with a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
// hello world
let value;
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);
forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
);
});

it("calls the callback when the source is a inside a JSX element with a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value = (
<div>
{
// hello world
'asdf'
}
</div>
);
});
`);
const callback = vitest.fn();

it("calls the callback when the source has a trailing comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value; // hello world
`);
const callback = vitest.fn();
forEachComment(node, callback, sourceFile);

forEachComment(node, callback, sourceFile);
expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
);
});

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
it("calls the callback when the source is a inside a JSX fragment with a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value = (
<>
{
// hello world
'asdf'
}
</>
);
});
}
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
);
});

it("calls the callback when the source is a inside a JSX self-closing element with a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value = (
<div {
// hello world
} />
);
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
);
});

it("calls the callback when the source is a variable with a trailing comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value; // hello world
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
}),
);
});
});

0 comments on commit 3e58c67

Please sign in to comment.