Skip to content

Commit

Permalink
refactor: 💡 split formatter test into categories
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Jan 11, 2025
1 parent 9eddef6 commit d82ab9a
Show file tree
Hide file tree
Showing 32 changed files with 6,031 additions and 5,876 deletions.
5,876 changes: 0 additions & 5,876 deletions __tests__/formatter.test.ts

This file was deleted.

48 changes: 48 additions & 0 deletions __tests__/formatter/alpine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, test } from "vitest";
import * as util from "../support/util";

describe("formatter alpine test", () => {
test("shorthand binding #557", async () => {
const content = [
`@section('body')`,
` <forms.radios legend="Meeting Schedule" name="meeting_type" value="single" v-model="form.meeting_type"`,
` :inline="true"`,
` :options="[`,
` 'single' => ['label' => 'Default'],`,
` 'series' => ['label' => 'Recurring meeting'],`,
` 'scheduler' => ['label' => 'Find a meeting date'],`,
`]" />`,
"@endsection",
].join("\n");

const expected = [
`@section('body')`,
` <forms.radios legend="Meeting Schedule" name="meeting_type" value="single" v-model="form.meeting_type"`,
` :inline="true"`,
` :options="[`,
` 'single' => ['label' => 'Default'],`,
` 'series' => ['label' => 'Recurring meeting'],`,
` 'scheduler' => ['label' => 'Find a meeting date'],`,
` ]" />`,
"@endsection",
"",
].join("\n");

await util.doubleFormatCheck(content, expected);
});

test("x-bind binding", async () => {
const content = [
`<div x-bind:class="imageLoaded?'blur-none':'blur-3xl'">`,
"</div>",
].join("\n");

const expected = [
`<div x-bind:class="imageLoaded ? 'blur-none' : 'blur-3xl'">`,
"</div>",
"",
].join("\n");

await util.doubleFormatCheck(content, expected);
});
});
47 changes: 47 additions & 0 deletions __tests__/formatter/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import assert from "node:assert";
import { describe, test } from "vitest";
import { BladeFormatter } from "../../src/main.js";

describe("formatter api test", () => {
test("format API", async () => {
const content = [
"<table>",
"<th><?= $userName ?></th>",
"</table>",
"",
].join("\n");

const expected = [
"<table>",
" <th><?= $userName ?></th>",
"</table>",
"",
].join("\n");

return new BladeFormatter().format(content).then((result: any) => {
assert.equal(result, expected);
});
});

test("format API with option", async () => {
const content = [
"<table>",
"<th><?= $userName ?></th>",
"</table>",
"",
].join("\n");

const expected = [
"<table>",
" <th><?= $userName ?></th>",
"</table>",
"",
].join("\n");

return new BladeFormatter({ indentSize: 2 })
.format(content)
.then((result: any) => {
assert.equal(result, expected);
});
});
});
47 changes: 47 additions & 0 deletions __tests__/formatter/attributes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, test } from "vitest";
import * as util from "../support/util";

describe("formatter attributes test", () => {
test("empty class atrbitue", async () => {
let content = [`<div class=""></div>`].join("\n");
let expected = [`<div class=""></div>`, ""].join("\n");

await util.doubleFormatCheck(content, expected);

content = [
`<input class="" type="file" name="product_images[]" multiple />
`,
].join("\n");
expected = [
`<input class="" type="file" name="product_images[]" multiple />`,
"",
].join("\n");

await util.doubleFormatCheck(content, expected);
});

test("keep html attribute indentation", async () => {
const content = [
`@component('some.file')`,
" <div>",
` <input type="text" an-object="{`,
` 'Some error': 1,`,
` }" />`,
" </div>",
"@endcomponent",
].join("\n");

const expected = [
`@component('some.file')`,
" <div>",
` <input type="text" an-object="{`,
` 'Some error': 1,`,
` }" />`,
" </div>",
"@endcomponent",
"",
].join("\n");

await util.doubleFormatCheck(content, expected);
});
});
44 changes: 44 additions & 0 deletions __tests__/formatter/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import assert from "node:assert";
import { describe, test } from "vitest";
import { Formatter } from "../../src/main.js";

const formatter = () => {
return new Formatter({ indentSize: 4 });
};

describe("basic formatter test", () => {
test("can format plain text", () => {
const content = "aaa\n";
const expected = "aaa\n";

return formatter()
.formatContent(content)
.then((result: any) => {
assert.equal(result, expected);
});
});

test("outputs end with new line", () => {
const content = "aaa";
const expected = "aaa\n";

return formatter()
.formatContent(content)
.then((result: any) => {
assert.equal(result, expected);
});
});

test("can format simple html tag", () => {
const content = "<html><body></body></html>";
const expected = ["<html>", "", "<body></body>", "", "</html>", ""].join(
"\n",
);

return formatter()
.formatContent(content)
.then((result: any) => {
assert.equal(result, expected);
});
});
});
Loading

0 comments on commit d82ab9a

Please sign in to comment.