test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const fs = require('fs');
  2. const compiler = require('vue-template-compiler');
  3. const toString = require('./index');
  4. it('is inverse of `compiler.parseComponent()`', () => {
  5. const source = fs.readFileSync('./test-components/Vanilla.vue', 'utf8');
  6. const descriptor = compiler.parseComponent(source);
  7. const result = toString(descriptor);
  8. expect(result).toBe(source)
  9. });
  10. it('vanilla sfc', async () => {
  11. const descriptor = await parse('./test-components/Vanilla.vue');
  12. const result = toString(descriptor);
  13. expect(result).toMatchSnapshot();
  14. });
  15. it('different order blocks', async () => {
  16. const descriptor = await parse('./test-components/DifferentOrderBlocks.vue');
  17. const result = toString(descriptor);
  18. expect(result).toMatchSnapshot();
  19. });
  20. it('leading newlines', async () => {
  21. const descriptor = await parse('./test-components/LeadingNewlines.vue');
  22. const result = toString(descriptor);
  23. expect(result).toMatchSnapshot();
  24. });
  25. it('no script', async () => {
  26. const descriptor = await parse('./test-components/NoScript.vue');
  27. const result = toString(descriptor);
  28. expect(result).toMatchSnapshot();
  29. });
  30. it('no template', async () => {
  31. const descriptor = await parse('./test-components/NoTemplate.vue');
  32. const result = toString(descriptor);
  33. expect(result).toMatchSnapshot();
  34. });
  35. it('no style', async () => {
  36. const descriptor = await parse('./test-components/NoStyle.vue');
  37. const result = toString(descriptor);
  38. expect(result).toMatchSnapshot();
  39. });
  40. it('multiple styles', async () => {
  41. const descriptor = await parse('./test-components/MultipleStyles.vue');
  42. const result = toString(descriptor);
  43. expect(result).toMatchSnapshot();
  44. });
  45. it('attributes on blocks', async () => {
  46. const descriptor = await parse('./test-components/AttributesOnBlocks.vue');
  47. const result = toString(descriptor);
  48. expect(result).toMatchSnapshot();
  49. });
  50. it('custom blocks', async () => {
  51. const descriptor = await parse('./test-components/CustomBlocks.vue');
  52. const result = toString(descriptor);
  53. expect(result).toMatchSnapshot();
  54. });
  55. it('extra newlines between blocks', async () => {
  56. const descriptor = await parse('./test-components/ExtraNewlinesBetweenBlocks.vue');
  57. const result = toString(descriptor);
  58. expect(result).toMatchSnapshot();
  59. });
  60. it('accepts custom indent amounts for every block', async () => {
  61. const descriptor = await parse('./test-components/CustomIndents.vue');
  62. const result = toString(descriptor, {
  63. indents: {
  64. template: 4,
  65. script: 2,
  66. style: 1,
  67. custom1: 10
  68. }
  69. });
  70. expect(result).toMatchSnapshot();
  71. });
  72. // helpers
  73. function parse(pathToComponent) {
  74. return new Promise((resolve, reject) => {
  75. fs.readFile(pathToComponent, 'utf8', (err, sfc) => {
  76. if (err) return reject(err);
  77. try {
  78. const descriptor = compiler.parseComponent(sfc);
  79. return resolve(descriptor);
  80. } catch (e) {
  81. return reject(e);
  82. }
  83. })
  84. });
  85. }