Skip to content

Commit

Permalink
test: Cover tabWidth option.
Browse files Browse the repository at this point in the history
  • Loading branch information
avocadowastaken committed Jan 11, 2022
1 parent 7aa6cfc commit 1bac04f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 28 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ npm i -D prettier-plugin-prisma
# or with yarn
yarn add -D prettier-plugin-prisma
```

### Supported options

- Tab width: https://prettier.io/docs/en/options.html#tab-width
95 changes: 67 additions & 28 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ const UNFORMATTED_FIXTURE = fs.readFileSync(
"utf8"
);

test("basic", () => {
const formatted = format(UNFORMATTED_FIXTURE, {
plugins: [plugin],
filepath: "./prisma/schema.prisma",
});

/**
* @param {string} text
* @param {Omit<import("prettier").Options, "plugins">} [options]
*/
function formatWithPlugin(
text,
{ filepath = "./prisma/schema.prisma", ...options } = {}
) {
const formatted = format(text, { ...options, filepath, plugins: [plugin] });
registerRawSnapshot(formatted);
return formatted;
}

test("basic", () => {
const formatted = formatWithPlugin(UNFORMATTED_FIXTURE);

expect(formatted).toMatchInlineSnapshot(`
generator client {
Expand Down Expand Up @@ -65,23 +73,16 @@ test("basic", () => {
check(formatted, { plugins: [plugin], filepath: "./prisma/schema.prisma" })
).toBe(true);

expect(
format(formatted, {
plugins: [plugin],
filepath: "./prisma/schema.prisma",
})
).toBe(formatted);
expect(formatWithPlugin(formatted)).toBe(formatted);
});

test("markdown", () => {
const fromFormatted = format(
["### Example 1", "```prisma", UNFORMATTED_FIXTURE, "```"].join("\n"),
{ plugins: [plugin], filepath: "./README.md" }
);

registerRawSnapshot(fromFormatted);

expect(fromFormatted).toMatchInlineSnapshot(`
expect(
formatWithPlugin(
["### Example 1", "```prisma", UNFORMATTED_FIXTURE, "```"].join("\n"),
{ filepath: "./README.md" }
)
).toMatchInlineSnapshot(`
### Example 1
\`\`\`prisma
Expand Down Expand Up @@ -122,14 +123,12 @@ test("markdown", () => {
`);

const fromUnformatted = format(
["### Example 1", "```prisma", UNFORMATTED_FIXTURE, "```"].join("\n"),
{ plugins: [plugin], filepath: "./README.md" }
);

registerRawSnapshot(fromUnformatted);

expect(fromUnformatted).toMatchInlineSnapshot(`
expect(
formatWithPlugin(
["### Example 1", "```prisma", UNFORMATTED_FIXTURE, "```"].join("\n"),
{ filepath: "./README.md" }
)
).toMatchInlineSnapshot(`
### Example 1
\`\`\`prisma
Expand Down Expand Up @@ -170,3 +169,43 @@ test("markdown", () => {
`);
});

test("tabWidth", () => {
expect(formatWithPlugin(UNFORMATTED_FIXTURE, { tabWidth: 4 }))
.toMatchInlineSnapshot(`
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
title String
content String?
published Boolean @default(false)
User User @relation(fields: [authorId], references: [id])
authorId Int
}
model Profile {
id Int @id @default(autoincrement())
bio String?
User User @relation(fields: [userId], references: [id])
userId Int @unique
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
Post Post[]
Profile Profile?
}
`);
});

0 comments on commit 1bac04f

Please sign in to comment.