Home Reference Source Test

test/index.js

  1. import Component from '../source/Component';
  2.  
  3. import { stringifyDOM } from 'dom-renderer';
  4.  
  5. import { readFile } from 'fs-extra';
  6.  
  7. import { join } from 'path';
  8.  
  9. import { toDataURI } from '@tech_query/node-toolkit';
  10.  
  11.  
  12. /**
  13. * @test {Component}
  14. */
  15. describe('Core class', () => {
  16.  
  17. var style, template, fragment;
  18.  
  19. /**
  20. * @test {Component.parseCSS}
  21. */
  22. it('Parse CSS files', async () => {
  23.  
  24. const file = 'test/example-js/index.css';
  25.  
  26. style = await Component.parseCSS( file );
  27.  
  28. style.tagName.should.be.equal('STYLE');
  29.  
  30. style.textContent.should.be.equal((await readFile( file )) + '');
  31. });
  32.  
  33. /**
  34. * @test {Component.parseHTML}
  35. */
  36. it('Parse pure HTML', async () => {
  37.  
  38. const file = 'test/example-js/index.html';
  39.  
  40. template = await Component.parseHTML( file );
  41.  
  42. template.nodeType.should.be.equal( 11 );
  43.  
  44. stringifyDOM( template ).should.be.equal(
  45. (await readFile( file )) + ''
  46. );
  47. });
  48.  
  49. describe('Parse mixed HTML', () => {
  50.  
  51. before(async () =>
  52. fragment = await Component.parseHTML('test/example-html/index.html')
  53. );
  54.  
  55. /**
  56. * @test {Component.findStyle}
  57. * @test {Component.parseCSS}
  58. */
  59. it('Find & Parse styles', async () => {
  60.  
  61. const styles = Component.findStyle( fragment );
  62.  
  63. styles.map(element => element.tagName).should.be.eql([
  64. 'LINK', 'STYLE'
  65. ]);
  66.  
  67. const element = await Component.parseCSS(
  68. styles[1].textContent,
  69. styles[1].type,
  70. 'test/example-html/index.css'
  71. );
  72.  
  73. element.textContent.trim().should.be.equal(`
  74. textarea {
  75. outline: none;
  76. }
  77. textarea {
  78. display: block;
  79. }`.trim()
  80. );
  81. });
  82.  
  83. /**
  84. * @test {Component.packJS}
  85. */
  86. it('Parse script', () => {
  87.  
  88. const code = Component.packJS(join(
  89. 'test/example-html/', fragment.lastElementChild.getAttribute('src')
  90. ));
  91.  
  92. (() => eval( code )).should.not.throw( SyntaxError );
  93.  
  94. code.should.containEql('\'example-html\'');
  95. });
  96. });
  97.  
  98. /**
  99. * @test {Component#toHTML}
  100. */
  101. it('Bundle to HTML', async () => {
  102.  
  103. const fragment = await (new Component('test/example-html')).toHTML(),
  104. tagName = element => element.tagName;
  105.  
  106. Array.from(fragment.children, tagName).should.be.eql(
  107. ['TEMPLATE', 'SCRIPT']
  108. );
  109.  
  110. Array.from(fragment.firstElementChild.content.children, tagName)
  111. .should.be.eql(['STYLE', 'STYLE', 'TEXTAREA']);
  112. });
  113.  
  114. /**
  115. * @test {Component#toJS}
  116. */
  117. it('Bundle to JS', async () => {
  118.  
  119. var component = await (new Component('test/example-js')).toJS();
  120.  
  121. component.includes(
  122. JSON.stringify( style.textContent ).slice(1, -1)
  123. ).should.be.true();
  124.  
  125. component.includes(
  126. JSON.stringify( stringifyDOM( template ) )
  127. .slice(1, -1).replace(/\\"/g, '"')
  128. ).should.be.true();
  129.  
  130. component.includes('name: \'Web components\'').should.be.true();
  131.  
  132. component.includes(
  133. toDataURI('test/example-js/icon.svg')
  134. ).should.be.true();
  135.  
  136. (() => eval( component )).should.not.throw( SyntaxError );
  137. });
  138. });