Skip to content

Commit ed359d2

Browse files
committed
fix: UTs
1 parent 928f15e commit ed359d2

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"apollo-server-express": "2.25.3",
136136
"chai-as-promised": "7.1.2",
137137
"chai-subset": "1.6.0",
138+
"cheerio": "^1.0.0",
138139
"contributor-faces": "1.1.0",
139140
"documentation": "12.3.0",
140141
"electron": "31.3.1",

test/unit/html_test.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ let expect
55
import('chai').then((chai) => {
66
expect = chai.expect
77
})
8-
const xpath = require('xpath')
9-
const Dom = require('@xmldom/xmldom').DOMParser
8+
const cheerio = require('cheerio')
109
const { scanForErrorMessages, removeNonInteractiveElements, minifyHtml, splitByChunks } = require('../../lib/html')
1110

1211
const opts = {
@@ -53,29 +52,31 @@ describe('HTML module', () => {
5352

5453
describe('#removeNonInteractiveElements', () => {
5554
it('should cut out all non-interactive elements from GitHub HTML', async () => {
56-
// Call the function with the loaded HTML
5755
html = fs.readFileSync(path.join(__dirname, '../data/github.html'), 'utf8')
5856
const result = removeNonInteractiveElements(html, opts)
59-
let doc = new Dom().parseFromString(result)
60-
const nodes = xpath.select('//input[@name="q"]', doc)
57+
58+
let $ = cheerio.load(result)
59+
60+
const nodes = $('input[name="q"]')
6161
expect(nodes).to.have.length(1)
6262
expect(result).not.to.include('Let’s build from here')
63+
6364
const minified = await minifyHtml(result)
64-
doc = new Dom().parseFromString(minified)
65-
const nodes2 = xpath.select('//input[@name="q"]', doc)
65+
$ = cheerio.load(minified)
66+
67+
const nodes2 = $('input[name="q"]')
6668
expect(nodes2).to.have.length(1)
6769
})
6870

69-
it('should keep interactive html elements', () => {
71+
it('should keep interactive HTML elements', () => {
7072
html = `
7173
<div id="onetrust-pc-sdk" class="otPcTab ot-hide ot-fade-in" lang="en" aria-label="Preference center" role="region">
7274
<div role="alertdialog" aria-modal="true" aria-describedby="ot-pc-desc" style="height: 100%;" aria-label="Privacy Preference Center">
7375
<!-- pc header --><div class="ot-pc-header" role="presentation">
7476
<div class="ot-title-cntr">
7577
<h2 id="ot-pc-title">Privacy Preference Center</h2>
7678
<div class="ot-close-cntr">
77-
<button id="close-pc-btn-handler" class="ot-close-icon" aria-label="Close">
78-
</button>
79+
<button id="close-pc-btn-handler" class="ot-close-icon" aria-label="Close"></button>
7980
</div>
8081
</div>
8182
</div>`
@@ -88,8 +89,7 @@ describe('HTML module', () => {
8889
<ul>
8990
<li>
9091
<div class="flex">
91-
<button class="hamburger hamburger--arrowalt outline-none focus:outline-none
92-
" style="line-height: 0; margin-top: 3px; margin-bottom: 3px;" type="button">
92+
<button class="hamburger hamburger--arrowalt outline-none focus:outline-none" style="line-height: 0; margin-top: 3px; margin-bottom: 3px;" type="button">
9393
<span class="hamburger-box">
9494
<span class="hamburger-inner"></span>
9595
</span>
@@ -98,11 +98,11 @@ describe('HTML module', () => {
9898
</li>
9999
<li>
100100
<a id="ember6" class="ember-view flex items-center" href="/projects/codeceptjs-cucumber/runs" aria-describedby="ember7-popper">
101-
<svg class="md-icon md-icon-play-circle-outline " width="30" height="30" viewBox="0 0 24 24" role="img">
102-
<path d="aaaa">aaa</path>
103-
</svg>
104-
</a>
105-
</li>
101+
<svg class="md-icon md-icon-play-circle-outline" width="30" height="30" viewBox="0 0 24 24" role="img">
102+
<path d="aaaa">aaa</path>
103+
</svg>
104+
</a>
105+
</li>
106106
</ul>
107107
</div>`
108108
const result = await minifyHtml(removeNonInteractiveElements(html, opts))
@@ -113,55 +113,42 @@ describe('HTML module', () => {
113113
})
114114

115115
it('should cut out all non-interactive elements from HTML', () => {
116-
// Call the function with the loaded HTML
117116
html = fs.readFileSync(path.join(__dirname, '../data/checkout.html'), 'utf8')
118117
const result = removeNonInteractiveElements(html, opts)
119118
expect(result).to.include('Name on card')
120119
expect(result).to.not.include('<script')
121120
})
122121

123122
it('should allow adding new elements', () => {
124-
const html = '<div><h6>Hey</h6></div>'
125-
126-
const result = removeNonInteractiveElements(html, {
127-
textElements: ['h6'],
128-
})
129-
123+
const html = '<div><h6>Hey</h6></div>';
124+
const result = removeNonInteractiveElements(html, { textElements: ['h6'] })
130125
expect(result).to.include('<h6>Hey</h6>')
131126
})
132127

133128
it('should cut out all non-interactive elements from GitLab HTML', () => {
134-
// Call the function with the loaded HTML
135129
html = fs.readFileSync(path.join(__dirname, '../data/gitlab.html'), 'utf8')
136-
// console.log(html);
137130
const result = removeNonInteractiveElements(html, opts)
138-
139131
result.should.include('Get free trial')
140132
result.should.include('Sign in')
141133
result.should.include('<button')
142-
const doc = new Dom().parseFromString(result)
143-
const nodes = xpath.select('//input[@placeholder="Search"]', doc)
134+
135+
const $ = cheerio.load(result)
136+
const nodes = $('input[placeholder="Search"]')
144137
expect(nodes).to.have.length(1)
145138
})
146139

147140
it('should cut out and minify Testomatio HTML', () => {
148-
// Call the function with the loaded HTML
149141
html = fs.readFileSync(path.join(__dirname, '../data/testomat.html'), 'utf8')
150-
// console.log(html);
151142
const result = removeNonInteractiveElements(html, opts)
152143
result.should.include('<svg class="md-icon md-icon-check-bold')
153-
// console.log(await minifyHtml(result));
154144
})
155145
})
156146

157147
describe('#splitByChunks', () => {
158-
it('should cut long htmls into chunks and add paths into them', () => {
159-
// Call the function with the loaded HTML
148+
it('should cut long HTMLs into chunks and add paths into them', () => {
160149
html = fs.readFileSync(path.join(__dirname, '../data/github.html'), 'utf8')
161150
const result = splitByChunks(html, 10000)
162151
expect(result).to.have.length(21)
163-
// console.log(result[10])
164-
// expect(result[10].startsWith('<div data-turbo-body=""')).to.be.true;
165152
for (const chunk of result) {
166153
expect(chunk.startsWith('<')).to.be.true
167154
}

test/unit/locator_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const xml = `<body>
197197

198198
describe('Locator', () => {
199199
beforeEach(() => {
200-
doc = new DOMParser().parseFromString(xml, 'application/xhtml+xml')
200+
doc = new DOMParser().parseFromString(xml, 'text/xml')
201201
})
202202

203203
describe('constructor', () => {

0 commit comments

Comments
 (0)