1 | const beautify = require('../src/index');
|
---|
2 | const fs = require('fs')
|
---|
3 |
|
---|
4 |
|
---|
5 | describe("xml-beautifier", () => {
|
---|
6 | it("should indent tags", () => {
|
---|
7 | const ori = "<div><span></span></div>"
|
---|
8 | const expected =
|
---|
9 | `<div>
|
---|
10 | <span>
|
---|
11 | </span>
|
---|
12 | </div>`
|
---|
13 |
|
---|
14 | expect(beautify(ori)).toEqual(expected)
|
---|
15 | })
|
---|
16 |
|
---|
17 | it("should indent self closing tags correctly", () => {
|
---|
18 | const ori = `<div><span><img src="" /></span></div>`
|
---|
19 | const expected =
|
---|
20 | `<div>
|
---|
21 | <span>
|
---|
22 | <img src="" />
|
---|
23 | </span>
|
---|
24 | </div>`
|
---|
25 |
|
---|
26 | expect(beautify(ori)).toEqual(expected)
|
---|
27 | })
|
---|
28 |
|
---|
29 | it("should put text nodes on a new line", () => {
|
---|
30 | const ori = '<div><span>foo bar</span></div>'
|
---|
31 | const expected =
|
---|
32 | `<div>
|
---|
33 | <span>
|
---|
34 | foo bar
|
---|
35 | </span>
|
---|
36 | </div>`
|
---|
37 |
|
---|
38 | expect(beautify(ori)).toEqual(expected)
|
---|
39 | })
|
---|
40 |
|
---|
41 | it('should handle nodes containing "/"', () => {
|
---|
42 | const ori = '<div><a href="/">foo bar</a></div>'
|
---|
43 | const expected =
|
---|
44 | `<div>
|
---|
45 | <a href="/">
|
---|
46 | foo bar
|
---|
47 | </a>
|
---|
48 | </div>`
|
---|
49 |
|
---|
50 | expect(beautify(ori)).toEqual(expected)
|
---|
51 | })
|
---|
52 |
|
---|
53 | describe("configuration options", () => {
|
---|
54 | it("should use custom indentation characters specified in `indentor`", () => {
|
---|
55 | const ori = "<div><span></span></div>"
|
---|
56 | const expected = "<div>\n\t<span>\n\t</span>\n</div>"
|
---|
57 |
|
---|
58 | expect(beautify(ori, {
|
---|
59 | indentor: "\t"
|
---|
60 | })).toEqual(expected)
|
---|
61 | })
|
---|
62 |
|
---|
63 | it("should compress text nodes onto the same line if `textNodesOnSameLine` is truthy", () => {
|
---|
64 | const ori = '<div><span>foo bar</span></div>'
|
---|
65 | const expected =
|
---|
66 | `<div>
|
---|
67 | <span>foo bar</span>
|
---|
68 | </div>`
|
---|
69 |
|
---|
70 | expect(beautify(ori, {
|
---|
71 | textNodesOnSameLine: true
|
---|
72 | })).toEqual(expected)
|
---|
73 | })
|
---|
74 |
|
---|
75 | })
|
---|
76 |
|
---|
77 | describe("performance", () => {
|
---|
78 | it("should process 2MB of XML quickly", () => {
|
---|
79 | const xml = fs.readFileSync(__dirname + '/huge.xml', 'utf8')
|
---|
80 | const startTime = Date.now()
|
---|
81 | beautify(xml)
|
---|
82 | const endTime = Date.now()
|
---|
83 | expect(endTime - startTime).toBeLessThan(2500)
|
---|
84 | })
|
---|
85 |
|
---|
86 | it("should process 2MB of XML quickly with `textNodesOnSameLine`", () => {
|
---|
87 | const xml = fs.readFileSync(__dirname + '/huge.xml', 'utf8')
|
---|
88 | const startTime = Date.now()
|
---|
89 | beautify(xml, {
|
---|
90 | textNodesOnSameLine: true
|
---|
91 | })
|
---|
92 | const endTime = Date.now()
|
---|
93 | expect(endTime - startTime).toBeLessThan(5000)
|
---|
94 | })
|
---|
95 |
|
---|
96 | })
|
---|
97 | })
|
---|