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>Inner", { 0: }))).toEqual( "Inner", ) expect( html(formatElements("Before <0>Inner After", { 0: })), ).toEqual("Before Inner After") }) it("should preserve element props", function () { expect( html(formatElements("<0>About", { 0: })), ).toEqual('About') }) it("should preserve newlines", function () { expect(html(formatElements("<0>Inn\ner", { 0: }))).toEqual( "Inn\ner", ) expect( html(formatElements("Before <0>Inn\r\ner After", { 0: })), ).toEqual("Before Inn\r\ner After") expect( html(formatElements("<0>Ab\rout", { 0: })), ).toEqual('Ab\rout') }) it("should preserve named element props", function () { expect( html( formatElements("About", { named: }), ), ).toEqual('About') }) it("should format paired placeholders whose names use the macro charset", function () { // camelCase already works; hyphen / underscore / dot must as well (#2676) expect( html( formatElements("Terms", { termsLink: , }), ), ).toEqual('Terms') expect( html( formatElements("Terms", { "terms-link": , }), ), ).toEqual('Terms') expect( html( formatElements("Terms", { terms_link: , }), ), ).toEqual('Terms') expect( html( formatElements("Terms", { "terms.link": , }), ), ).toEqual('Terms') expect( html( formatElements( "I agree to the Terms of Service and the Privacy Policy.", { "terms-link": , "privacy-link": , }, ), ), ).toEqual( 'I agree to the Terms of Service and the Privacy Policy.', ) }) it("should format unpaired placeholders whose names use the macro charset", function () { expect( html(formatElements("text", { termsLink:
})), ).toEqual("text
") expect( html(formatElements("text", { "terms-link":
})), ).toEqual("text
") expect( html(formatElements("text", { terms_link:
})), ).toEqual("text
") expect( html(formatElements("text", { "terms.link":
})), ).toEqual("text
") }) it("should preserve nested named element props", function () { expect( html( formatElements("About us", { named: , b: , }), ), ).toEqual('About us') }) it("should format nested elements", function () { expect( html( formatElements("<0><1>Deep", { 0: , 1: , }), ), ).toEqual('Deep') expect( html( formatElements( "Before \n<0>Inside <1>\nNested\n Between <2/> After", { 0: , 1: , 2:
}, ), ), ).toEqual( 'Before \n
Inside \nNested\n Between
After
', ) }) it("should ignore non existing element", function () { mockConsole((console) => { expect(html(formatElements("<0>First"))).toEqual("First") expect(html(formatElements("<0>FirstSecond"))).toEqual("FirstSecond") expect(html(formatElements("First<0>SecondThird"))).toEqual( "FirstSecondThird", ) expect(html(formatElements("Fir<0/>st"))).toEqual("First") expect(html(formatElements("text"))).toEqual("text") expect(html(formatElements("text
"))).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("ab", { 0: })), ).toEqual("ab") expect(html(formatElements("ab"))).toEqual("ab") expect( html( formatElements("a", { 0: }), ), ).toEqual("a") expect(console.error).toBeCalledTimes(3) }) }) it("should ignore incorrect tags and print them as a text", function () { mockConsole((console) => { expect(html(formatElements("text"))).toEqual("text</0>") expect(html(formatElements("text<0 />"))).toEqual("text<0 />") expect(console.warn).not.toBeCalled() expect(console.error).not.toBeCalled() }) }) it("should ignore unpaired element used as paired", function () { mockConsole((console) => { expect(html(formatElements("<0>text", { 0:
}))).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("text", { named:
})), ).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", { named: }))).toEqual( "text", ) }) it("should create two children with different keys", function () { const cleanPrefix = (str: string): number => Number.parseInt(str.replace("$lingui$_", ""), 10) const elements = formatElements("
<0/><0/>
", { div:
, 0: hi, }) expect(React.isValidElement(elements)).toBe(true) const childElements = ( elements as React.ReactElement<{ children: React.ReactElement[] }> ).props.children const childKeys = childElements.map((el) => el?.key).filter(Boolean) expect(cleanPrefix(childKeys[0] as string)).toBeLessThan( cleanPrefix(childKeys[1] as string), ) }) })