popup-options.js 15 KB

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