popup-options.js 14 KB

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