import { describe, test, expect, beforeAll } from "vitest"
import { Herb } from "@herb-tools/node-wasm"
import { Formatter } from "../../src"
import dedent from "dedent"
let formatter: Formatter
describe("@herb-tools/formatter - Conditional HTML Elements", () => {
beforeAll(async () => {
await Herb.load()
formatter = new Formatter(Herb, {
indentWidth: 2,
maxLineLength: 80
})
})
describe("basic conditional elements", () => {
test("formats simple conditional element with if", () => {
const input = dedent`
<% if @with_icon %>
<% end %>
Hello
<% if @with_icon %>
<% end %>
`
const expected = dedent`
<% if @with_icon %>
<% end %>
Hello
<% if @with_icon %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
test("formats conditional element with unless", () => {
const input = dedent`
<% unless @hide_wrapper %>
<% end %>
Content
<% unless @hide_wrapper %>
<% end %>
`
const expected = dedent`
<% unless @hide_wrapper %>
<% end %>
Content
<% unless @hide_wrapper %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
})
describe("conditional elements with attributes", () => {
test("formats conditional element with attributes", () => {
const input = dedent`
<% if @show_container %>
<% end %>
Inside
<% if @show_container %>
<% end %>
`
const expected = dedent`
<% if @show_container %>
<% end %>
Inside
<% if @show_container %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
})
describe("conditional elements with body content", () => {
test("formats conditional element with multiple body elements", () => {
const input = dedent`
<% if @with_wrapper %>
<% end %>
Title
Description
<% if @with_wrapper %>
<% end %>
`
const expected = dedent`
<% if @with_wrapper %>
<% end %>
Title
Description
<% if @with_wrapper %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
test("formats conditional element with ERB content in body", () => {
const input = dedent`
<% if @show_card %>
<% end %>
<%= @title %>
<%= @description %>
<% if @show_card %>
<% end %>
`
const expected = dedent`
<% if @show_card %>
<% end %>
<% if @inner %>
<% end %>
Content
<% if @inner %>
<% end %>
<% if @outer %>
<% end %>
`
const expected = dedent`
<% if @outer %>
<% end %>
<% if @inner %>
<% end %>
Content
<% if @inner %>
<% end %>
<% if @outer %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
test("formats conditional element with nested ERB if", () => {
const input = dedent`
<% if @with_wrapper %>
<% end %>
<% if true %>
Conditional inside
<% end %>
<% if @with_wrapper %>
<% end %>
`
const expected = dedent`
<% if @with_wrapper %>
<% end %>
<% if true %>
Conditional inside
<% end %>
<% if @with_wrapper %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
})
describe("multiple sequential conditional elements", () => {
test("formats multiple sequential conditional elements", () => {
const input = dedent`
<% if @show_header %>
<% end %>
Header
<% if @show_header %>
<% end %>
<% if @show_footer %>
<% end %>
`
const expected = dedent`
<% if @show_header %>
<% end %>
Header
<% if @show_header %>
<% end %>
<% if @show_footer %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
})
describe("conditional elements with void elements", () => {
test("formats conditional element with void element siblings", () => {
const input = dedent`
<% if @with_container %>
<% end %>
<% if @with_container %>
<% end %>
`
const expected = dedent`
<% if @with_container %>
<% end %>
<% if @with_container %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
})
describe("conditional elements inside blocks", () => {
test("formats conditional element inside block", () => {
const input = dedent`
<%= form_with do %>
<% if @show_wrapper %>
<% end %>
<% if @show_wrapper %>
<% end %>
<% end %>
`
const expected = dedent`
<%= form_with do %>
<% if @show_wrapper %>
<% end %>
<% if @show_wrapper %>
<% end %>
<% end %>
`
const output = formatter.format(input)
expect(output).toEqual(expected)
})
})
describe("preserves original formatting when appropriate", () => {
test("preserves already well-formatted conditional element", () => {
const input = dedent`
<% if @with_icon %>