popup-options.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. var t = require('assert')
  2. module.exports = ({popup, advanced, content}) => {
  3. before(async () => {
  4. await advanced.bringToFront()
  5. // remove origin
  6. if (await advanced.evaluate(() => Object.keys(state.origins).length > 1)) {
  7. // expand origin
  8. if (!await advanced.evaluate(() =>
  9. document.querySelector('.m-list li:nth-of-type(2)')
  10. .classList.contains('m-expanded'))) {
  11. await advanced.click('.m-list li:nth-of-type(2)')
  12. }
  13. await advanced.click('.m-list li:nth-of-type(2) .m-footer .m-button:nth-of-type(2)')
  14. }
  15. // add origin
  16. await advanced.select('.m-select', 'http')
  17. await advanced.type('[type=text]', 'localhost:3000')
  18. await advanced.click('button')
  19. // await advanced.waitFor(() => document.querySelectorAll('.m-list li').length === 2)
  20. await advanced.waitFor(200)
  21. // enable header detection
  22. if (!await advanced.evaluate(() => state.header)) {
  23. await advanced.click('.m-switch')
  24. }
  25. })
  26. describe('button - raw/markdown', () => {
  27. before(async () => {
  28. // popup
  29. await popup.bringToFront()
  30. // defaults button
  31. await popup.click('button:nth-of-type(2)')
  32. })
  33. it('render markdown as html', async () => {
  34. // go to page serving markdown as text/markdown
  35. await content.goto('http://localhost:3000/correct-content-type')
  36. await content.bringToFront()
  37. await content.waitFor('#_html')
  38. t.equal(
  39. await content.evaluate(() =>
  40. document.querySelector('#_html p strong').innerText
  41. ),
  42. 'bold',
  43. 'markdown should be rendered'
  44. )
  45. // popup
  46. await popup.bringToFront()
  47. t.strictEqual(
  48. await popup.evaluate(() =>
  49. state.raw
  50. ),
  51. false,
  52. 'state.raw should equal false'
  53. )
  54. t.equal(
  55. await popup.evaluate(() =>
  56. document.querySelector('.m-button:first-child').innerText.toLowerCase()
  57. ),
  58. 'markdown',
  59. 'button text should equal markdown'
  60. )
  61. })
  62. it('display raw markdown', async () => {
  63. // raw button
  64. await content.bringToFront()
  65. await popup.click('button:nth-of-type(1)')
  66. // content auto reloads
  67. await content.waitFor('#_markdown')
  68. t.equal(
  69. await content.evaluate(() =>
  70. document.querySelector('#_markdown').innerText
  71. ),
  72. '**bold**',
  73. 'markdown should not be rendered'
  74. )
  75. // popup
  76. await popup.bringToFront()
  77. t.strictEqual(
  78. await popup.evaluate(() =>
  79. state.raw
  80. ),
  81. true,
  82. 'state.raw should equal true'
  83. )
  84. t.equal(
  85. await popup.evaluate(() =>
  86. document.querySelector('.m-button:first-child').innerText.toLowerCase()
  87. ),
  88. 'html',
  89. 'button text should equal html'
  90. )
  91. })
  92. })
  93. describe('set theme', () => {
  94. before(async () => {
  95. // popup
  96. await popup.bringToFront()
  97. // defaults button
  98. await popup.click('button:nth-of-type(2)')
  99. // theme tab
  100. await popup.click('.m-tabs a:nth-of-type(1)')
  101. })
  102. it('github theme should be set by default', async () => {
  103. // go to page serving markdown as text/markdown
  104. await content.goto('http://localhost:3000/correct-content-type')
  105. await content.bringToFront()
  106. await content.waitFor('#_theme')
  107. t.strictEqual(
  108. await content.evaluate(() =>
  109. /github\.css$/.test(
  110. document.querySelector('#_theme').getAttribute('href')
  111. )
  112. ),
  113. true,
  114. 'github theme styles should be included'
  115. )
  116. })
  117. it('set github-dark theme', async () => {
  118. // select github-dark theme
  119. await content.bringToFront()
  120. await popup.select('.m-panel:nth-of-type(1) select', 'github-dark')
  121. // content auto reloads
  122. await content.waitFor('#_theme')
  123. t.strictEqual(
  124. await content.evaluate(() =>
  125. /github-dark\.css$/.test(
  126. document.querySelector('#_theme').getAttribute('href')
  127. )
  128. ),
  129. true,
  130. 'github-dark theme styles should be included'
  131. )
  132. })
  133. it('popup should preserve state', async () => {
  134. // reload popup
  135. await popup.bringToFront()
  136. await popup.reload()
  137. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  138. // await popup.waitFor('#popup')
  139. await popup.waitFor(200)
  140. t.equal(
  141. await popup.evaluate(() =>
  142. state.theme
  143. ),
  144. 'github-dark',
  145. 'state.theme should equal github-dark'
  146. )
  147. t.equal(
  148. await popup.evaluate(() =>
  149. document.querySelectorAll('.m-panel:nth-of-type(1) select option')[
  150. document.querySelector('.m-panel:nth-of-type(1) select').selectedIndex
  151. ].innerText
  152. ),
  153. 'github-dark',
  154. 'dom select option should be github-dark'
  155. )
  156. })
  157. })
  158. describe('set compiler options - marked', () => {
  159. before(async () => {
  160. // popup
  161. await popup.bringToFront()
  162. // defaults button
  163. await popup.click('button:nth-of-type(2)')
  164. // compiler tab
  165. await popup.click('.m-tabs a:nth-of-type(2)')
  166. })
  167. it('gfm is enabled by default', async () => {
  168. // go to page serving markdown as text/markdown
  169. await content.goto('http://localhost:3000/compiler-options-marked')
  170. await content.bringToFront()
  171. await content.waitFor('#_html')
  172. t.equal(
  173. await content.evaluate(() =>
  174. document.querySelector('#_html p del').innerText
  175. ),
  176. 'strikethrough',
  177. 'gfm should be rendered'
  178. )
  179. })
  180. it('gfm is disabled', async () => {
  181. // disable gfm
  182. await content.bringToFront()
  183. // gfm switch
  184. await popup.click('.m-panel:nth-of-type(2) .m-switch:nth-of-type(2)')
  185. // content auto reloads
  186. await content.waitFor(100)
  187. await content.waitFor('#_html')
  188. t.equal(
  189. await content.evaluate(() =>
  190. document.querySelector('#_html p').innerText
  191. ),
  192. '~~strikethrough~~',
  193. 'gfm should not be rendered'
  194. )
  195. })
  196. it('popup should preserve state', async () => {
  197. // reload popup
  198. await popup.bringToFront()
  199. await popup.reload()
  200. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  201. // await popup.waitFor('#popup')
  202. await popup.waitFor(200)
  203. t.equal(
  204. await popup.evaluate(() =>
  205. document.querySelectorAll('.m-panel:nth-of-type(2) .m-select option')[
  206. document.querySelector('.m-panel:nth-of-type(2) .m-select').selectedIndex
  207. ].innerText
  208. ),
  209. 'marked',
  210. 'dom select option should be marked'
  211. )
  212. t.strictEqual(
  213. await popup.evaluate(() =>
  214. state.options.gfm
  215. ),
  216. false,
  217. 'state.options.gfm should be false'
  218. )
  219. t.strictEqual(
  220. await popup.evaluate(() =>
  221. document.querySelector('.m-panel:nth-of-type(2) .m-switch:nth-of-type(2)')
  222. .classList.contains('is-checked')
  223. ),
  224. false,
  225. 'dom gfm checkbox should be disabled'
  226. )
  227. })
  228. })
  229. describe('set compiler options - remark', () => {
  230. before(async () => {
  231. // popup
  232. await popup.bringToFront()
  233. // defaults button
  234. await popup.click('button:nth-of-type(2)')
  235. // compiler tab
  236. await popup.click('.m-tabs a:nth-of-type(2)')
  237. })
  238. it('marked should not render gfm task lists', async () => {
  239. // go to page serving markdown as text/markdown
  240. await content.goto('http://localhost:3000/compiler-options-remark')
  241. await content.bringToFront()
  242. await content.waitFor('#_html')
  243. t.equal(
  244. await content.evaluate(() =>
  245. document.querySelector('#_html ul li').innerText
  246. ),
  247. '[ ] task',
  248. 'gfm task lists should not be rendered'
  249. )
  250. })
  251. it('remark should render gfm task lists by default', async () => {
  252. // select remark compiler
  253. await content.bringToFront()
  254. await popup.select('.m-panel:nth-of-type(2) select', 'remark')
  255. // content auto reloads
  256. await content.waitFor(100)
  257. await content.waitFor('#_html')
  258. t.equal(
  259. await content.evaluate(() =>
  260. document.querySelector('#_html ul li').getAttribute('class')
  261. ),
  262. 'task-list-item',
  263. 'dom li should have a class set'
  264. )
  265. t.strictEqual(
  266. await content.evaluate(() =>
  267. document.querySelector('#_html ul li [type=checkbox]')
  268. .hasAttribute('disabled')
  269. ),
  270. true,
  271. 'dom li should contain checkbox in it'
  272. )
  273. t.equal(
  274. await content.evaluate(() =>
  275. document.querySelector('#_html ul li').innerText
  276. ),
  277. ' task',
  278. 'dom li should contain the task text'
  279. )
  280. })
  281. it('remark disable gfm', async () => {
  282. // redraw popup
  283. await popup.bringToFront()
  284. await popup.reload()
  285. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  286. // await popup.waitFor('#popup')
  287. await popup.waitFor(200)
  288. // disable gfm
  289. await content.bringToFront()
  290. // gfm switch
  291. await popup.click('.m-panel:nth-of-type(2) .m-switch:nth-of-type(4)')
  292. // content auto reloads
  293. await content.waitFor(100)
  294. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  295. // await content.waitFor('#_html')
  296. await content.waitFor(200)
  297. t.equal(
  298. await content.evaluate(() =>
  299. document.querySelector('#_html ul li').innerText
  300. ),
  301. '[ ] task',
  302. 'gfm task lists should not be rendered'
  303. )
  304. })
  305. it('popup should preserve state', async () => {
  306. // reload popup
  307. await popup.bringToFront()
  308. await popup.reload()
  309. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  310. // await popup.waitFor('#popup')
  311. await popup.waitFor(200)
  312. await popup.waitFor(100)
  313. t.equal(
  314. await popup.evaluate(() =>
  315. document.querySelectorAll('.m-panel:nth-of-type(2) .m-select option')[
  316. document.querySelector('.m-panel:nth-of-type(2) .m-select').selectedIndex
  317. ].innerText
  318. ),
  319. 'remark',
  320. 'dom select option should be remark'
  321. )
  322. t.strictEqual(
  323. await popup.evaluate(() =>
  324. state.options.gfm
  325. ),
  326. false,
  327. 'state.options.gfm should be false'
  328. )
  329. t.strictEqual(
  330. await popup.evaluate(() =>
  331. document.querySelector('.m-panel:nth-of-type(2) .m-switch:nth-of-type(4)')
  332. .classList.contains('is-checked')
  333. ),
  334. false,
  335. 'dom gfm checkbox should be disabled'
  336. )
  337. })
  338. })
  339. describe('set content options - toc', () => {
  340. before(async () => {
  341. // popup
  342. await popup.bringToFront()
  343. // defaults button
  344. await popup.click('button:nth-of-type(2)')
  345. // content tab
  346. await popup.click('.m-tabs a:nth-of-type(3)')
  347. })
  348. it('toc is disabled by default', async () => {
  349. // go to page serving markdown as text/markdown
  350. await content.goto('http://localhost:3000/content-options-toc')
  351. await content.bringToFront()
  352. await content.waitFor('#_html')
  353. t.strictEqual(
  354. await content.evaluate(() =>
  355. document.querySelector('#_toc')
  356. ),
  357. null,
  358. 'toc should be disabled'
  359. )
  360. })
  361. it('enable toc', async () => {
  362. // enable toc
  363. await content.bringToFront()
  364. // toc switch
  365. await popup.click('.m-panel:nth-of-type(3) .m-switch:nth-of-type(3)')
  366. // content auto reloads
  367. await content.waitFor('#_toc')
  368. t.deepStrictEqual(
  369. await content.evaluate(() =>
  370. Array.from(document.querySelectorAll('#_toc #_ul a'))
  371. .map((a) => ({href: a.getAttribute('href'), text: a.innerText}))
  372. ),
  373. [
  374. {href: '#h1', text: 'h1'},
  375. {href: '#h2', text: 'h2'},
  376. {href: '#h3', text: 'h3'},
  377. ],
  378. 'toc should be rendered'
  379. )
  380. })
  381. })
  382. describe('set content options - scroll', () => {
  383. before(async () => {
  384. // popup
  385. await popup.bringToFront()
  386. // defaults button
  387. await popup.click('button:nth-of-type(2)')
  388. // content tab
  389. await popup.click('.m-tabs a:nth-of-type(3)')
  390. })
  391. it('preserve scroll position by default', async () => {
  392. // go to page serving markdown as text/markdown
  393. await content.goto('http://localhost:3000/content-options-scroll')
  394. await content.bringToFront()
  395. await content.waitFor('#_html')
  396. // scroll down 200px
  397. await content.evaluate(() =>
  398. document.querySelector('body').scrollTop = 200
  399. )
  400. await content.waitFor(150)
  401. // reload page
  402. await content.reload()
  403. await content.waitFor('#_html')
  404. await content.waitFor(150)
  405. t.strictEqual(
  406. await content.evaluate(() =>
  407. document.querySelector('body').scrollTop,
  408. ),
  409. 200,
  410. 'scrollTop should be 200px'
  411. )
  412. })
  413. it('scroll to top', async () => {
  414. // disable scroll option
  415. await content.bringToFront()
  416. // scroll switch
  417. await popup.click('.m-panel:nth-of-type(3) .m-switch:nth-of-type(2)')
  418. // content auto reloads
  419. await content.waitFor(100)
  420. await content.waitFor('#_html')
  421. t.strictEqual(
  422. await content.evaluate(() =>
  423. document.querySelector('body').scrollTop,
  424. ),
  425. 0,
  426. 'scrollTop should be 0px'
  427. )
  428. // scroll down 200px
  429. await content.evaluate(() =>
  430. document.querySelector('body').scrollTop = 200
  431. )
  432. await content.waitFor(150)
  433. // reload page
  434. await content.reload()
  435. await content.waitFor('#_html')
  436. await content.waitFor(150)
  437. t.strictEqual(
  438. await content.evaluate(() =>
  439. document.querySelector('body').scrollTop,
  440. ),
  441. 0,
  442. 'scrollTop should be 0px'
  443. )
  444. })
  445. it('scroll to anchor', async () => {
  446. // click on header link
  447. await content.click('h2 a')
  448. await content.waitFor(150)
  449. t.strictEqual(
  450. await content.evaluate(() =>
  451. document.querySelector('body').scrollTop + 1
  452. ),
  453. await content.evaluate(() =>
  454. document.querySelector('h2').offsetTop
  455. ),
  456. 'page should be scrolled to the anchor'
  457. )
  458. // scroll down 200px
  459. await content.evaluate(() =>
  460. document.querySelector('body').scrollTop += 200
  461. )
  462. await content.waitFor(150)
  463. t.strictEqual(
  464. await content.evaluate(() =>
  465. document.querySelector('body').scrollTop + 1
  466. ),
  467. await content.evaluate(() =>
  468. document.querySelector('h2').offsetTop + 200
  469. ),
  470. 'page should be scrolled below the anchor'
  471. )
  472. // reload page
  473. await content.reload()
  474. await content.waitFor('#_html')
  475. await content.waitFor(150)
  476. t.strictEqual(
  477. await content.evaluate(() =>
  478. document.querySelector('body').scrollTop
  479. ),
  480. await content.evaluate(() =>
  481. document.querySelector('h2').offsetTop
  482. ),
  483. 'page should be scrolled back to the anchor'
  484. )
  485. })
  486. })
  487. }