jscodeshift-mode.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const adapter = require('../src/index');
  2. const sfc = require('./make-sfc');
  3. const template = `
  4. <div class="widget">{{name}}</div>
  5. `;
  6. const script = `
  7. export default {
  8. props: {
  9. name: String
  10. }
  11. };
  12. `;
  13. const style = `
  14. .widget {
  15. color: red;
  16. }
  17. `;
  18. describe('jscodeshift mode', () => {
  19. describe('transforming js file', () => {
  20. test('passes js content as fileInfo.source', () => {
  21. let js = null;
  22. const adapted = adapter(function transform(fileInfo, api, options) {
  23. js = fileInfo.source;
  24. });
  25. adapted({
  26. source: 'const b = 400;',
  27. path: 'util.js'
  28. }, {}, {});
  29. expect(js).toBe('const b = 400;');
  30. });
  31. });
  32. describe('transforming vue component', () => {
  33. test('passes component <script> content as fileInfo.source', () => {
  34. let source = null;
  35. const adapted = adapter(function transform(fileInfo, api, options) {
  36. source = fileInfo.source;
  37. });
  38. adapted({
  39. source: sfc({template, script, style}),
  40. path: 'Widget.vue'
  41. }, {}, {});
  42. expect(source).toBe(script);
  43. });
  44. test('is no-op and skips transform when there is no <script>', () => {
  45. let invokedTransform = false;
  46. const adapted = adapter(function transform(fileInfo, api, options) {
  47. invokedTransform = true;
  48. return 'var fail = 4;';
  49. });
  50. const result = adapted({
  51. source: sfc({ template, style }),
  52. path: 'Widget.vue'
  53. }, {}, {});
  54. expect(result).toBe(undefined);
  55. expect(invokedTransform).toBe(false);
  56. });
  57. test('passes component path as fileInfo.path', () => {
  58. let path = null;
  59. const adapted = adapter(function transform(fileInfo, api, options) {
  60. path = fileInfo.path;
  61. });
  62. adapted({
  63. path: '/the/path/Widget.vue',
  64. source: sfc({template, script, style})
  65. }, {}, {});
  66. expect(path).toBe('/the/path/Widget.vue');
  67. });
  68. test('passes api to transform', () => {
  69. const apiPassed = {
  70. jscodeshift: () => { },
  71. stats: () => { }
  72. };
  73. let apiSeen = null;
  74. const adapted = adapter(function transform(fileInfo, api, options) {
  75. apiSeen = api;
  76. });
  77. adapted({
  78. source: sfc({template, script, style}),
  79. path: 'Widget.vue'
  80. }, apiPassed, {});
  81. expect(apiSeen.jscodeshift).toBe(apiPassed.jscodeshift);
  82. expect(apiSeen.stats).toBe(apiPassed.stats);
  83. });
  84. test('passes options to transform', () => {
  85. const optionsPassed = {
  86. blah: 1
  87. };
  88. let optionsSeen = null;
  89. const adapted = adapter(function transform(fileInfo, api, options) {
  90. optionsSeen = options;
  91. });
  92. const result = adapted({
  93. source: sfc({template, script, style}),
  94. path: 'Widget.vue'
  95. }, {}, optionsPassed);
  96. expect(optionsSeen.blah).toBe(optionsPassed.blah);
  97. });
  98. test('is no-op if transform returns undefined', () => {
  99. const adapted = adapter(function transform(fileInfo, api, options) {
  100. return undefined;
  101. });
  102. const result = adapted({
  103. source: sfc({template, script, style}),
  104. path: 'Widget.vue'
  105. }, {}, {});
  106. expect(result).toBe(undefined);
  107. });
  108. test('is no-op if transform returns null', () => {
  109. const adapted = adapter(function transform(fileInfo, api, options) {
  110. return null;
  111. });
  112. const result = adapted({
  113. source: sfc({template, script, style}),
  114. path: 'Widget.vue'
  115. }, {}, {});
  116. expect(result).toBe(undefined);
  117. });
  118. test('is no-op if transform returns empty string', () => {
  119. const adapted = adapter(function transform(fileInfo, api, options) {
  120. return '';
  121. });
  122. const result = adapted({
  123. source: sfc({template, script, style}),
  124. path: 'Widget.vue'
  125. }, {}, {});
  126. expect(result).toBe(undefined);
  127. });
  128. })
  129. });