import { render } from "@testing-library/react"
import * as React from "react"
import { formatElements } from "./format"
import { mockConsole } from "@lingui/test-utils"
describe("formatElements", function () {
const html = (elements: React.ReactNode) =>
render(elements as React.ReactElement).container.innerHTML
it("should return string when there are no elements", function () {
expect(formatElements("")).toEqual("")
expect(formatElements("Text only")).toEqual("Text only")
})
it("should format unpaired elements", function () {
expect(html(formatElements("<0/>", { 0:
}))).toEqual("
")
})
it("should format paired elements", function () {
expect(html(formatElements("<0>Inner0>", { 0: }))).toEqual(
"Inner",
)
expect(
html(formatElements("Before <0>Inner0> After", { 0: })),
).toEqual("Before Inner After")
})
it("should preserve element props", function () {
expect(
html(formatElements("<0>About0>", { 0: })),
).toEqual('About')
})
it("should preserve newlines", function () {
expect(html(formatElements("<0>Inn\ner0>", { 0: }))).toEqual(
"Inn\ner",
)
expect(
html(formatElements("Before <0>Inn\r\ner0> After", { 0: })),
).toEqual("Before Inn\r\ner After")
expect(
html(formatElements("<0>Ab\rout0>", { 0: })),
).toEqual('Ab\rout')
})
it("should preserve named element props", function () {
expect(
html(
formatElements("
})),
).toEqual("text
")
expect(
html(formatElements("text
})),
).toEqual("text
")
expect(
html(formatElements("text
})),
).toEqual("text
")
expect(
html(formatElements("text
})),
).toEqual("text
")
})
it("should preserve nested named element props", function () {
expect(
html(
formatElements("
},
),
),
).toEqual(
'Before \nInside \nNested\n Between
After',
)
})
it("should ignore non existing element", function () {
mockConsole((console) => {
expect(html(formatElements("<0>First0>"))).toEqual("First")
expect(html(formatElements("<0>First0>Second"))).toEqual("FirstSecond")
expect(html(formatElements("First<0>Second0>Third"))).toEqual(
"FirstSecondThird",
)
expect(html(formatElements("Fir<0/>st"))).toEqual("First")
expect(html(formatElements("
"))).toEqual("text ")
expect(console.warn).not.toBeCalled()
expect(console.error).toBeCalledTimes(6)
})
})
it("should ignore elements named like Object.prototype members", function () {
mockConsole((console) => {
expect(
html(formatElements("
}))).toEqual("text")
expect(console.warn).not.toBeCalled()
expect(console.error).toBeCalled()
})
})
it("should ignore unpaired named element used as paired", function () {
mockConsole((console) => {
expect(
html(formatElements("
})),
).toEqual("text")
expect(console.warn).not.toBeCalled()
expect(console.error).toBeCalledTimes(1)
})
})
it("should ignore paired element used as unpaired", function () {
expect(html(formatElements("text<0/>", { 0: }))).toEqual(
"text",
)
})
it("should ignore paired named element used as unpaired", function () {
expect(html(formatElements("text