jsx-helpers-test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright 2013-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @emails jeffmo@fb.com
  10. */
  11. 'use strict';
  12. require('mock-modules').autoMockOff();
  13. describe('jsx', function() {
  14. var jsx = require('../jsx-helpers');
  15. var utils = require('jstransform/src/utils');
  16. var esprima = require('esprima-fb');
  17. function runWithLiteral(text, isLast) {
  18. var source = '<div>' + text + '</div>';
  19. var ast = esprima.parse(source, { range: true });
  20. var state = utils.createState(source);
  21. var literal = ast.body[0].expression.children[0];
  22. state.g.position = literal.range[0];
  23. jsx.renderJSXLiteral(literal, isLast, state);
  24. return state;
  25. }
  26. function runWithAttribute(inlineAttribute, isLast) {
  27. var source = '<div ' + inlineAttribute + ' />';
  28. var ast = esprima.parse(source, { range: true });
  29. var state = utils.createState(source);
  30. var attribute = ast.body[0].expression.openingElement.attributes[0];
  31. state.g.position = attribute.value.range[0];
  32. jsx.renderJSXExpressionContainer(
  33. function() {},
  34. attribute.value,
  35. isLast,
  36. [],
  37. state
  38. );
  39. return state;
  40. }
  41. it('should render simple literal', function() {
  42. var state = runWithLiteral('a', true);
  43. expect(state.g.buffer).toEqual('"a"');
  44. });
  45. it('should render simple literal with single space before', function() {
  46. var state = runWithLiteral(' a', true);
  47. expect(state.g.buffer).toEqual('" a"');
  48. });
  49. it('should render simple literal with single space before' +
  50. ' from multiple spaces', function() {
  51. var state = runWithLiteral(' \t a', true);
  52. expect(state.g.buffer).toEqual('" a"');
  53. });
  54. it('should render simple literal with single space after', function() {
  55. var state = runWithLiteral('a ', true);
  56. expect(state.g.buffer).toEqual('"a "');
  57. });
  58. it('should render simple literal with single space after' +
  59. ' from multiple spaces', function() {
  60. var state = runWithLiteral('a \t \t ', true);
  61. expect(state.g.buffer).toEqual('"a "');
  62. });
  63. it('should render multiline literal as last', function() {
  64. var state = runWithLiteral(
  65. ' sdfsdfsdf\n' +
  66. ' sdlkfjsdfljs\n' +
  67. ' ',
  68. true);
  69. expect(state.g.buffer).toEqual(
  70. '" sdfsdfsdf" + " " +\n' +
  71. ' "sdlkfjsdfljs"\n' +
  72. ' ');
  73. });
  74. it('should render multiline literal as not last', function() {
  75. var state = runWithLiteral(
  76. ' sdfsdfsdf\n' +
  77. ' sdlkfjsdfljs\n' +
  78. ' ',
  79. false);
  80. expect(state.g.buffer).toEqual(
  81. '" sdfsdfsdf" + " " +\n' +
  82. ' "sdlkfjsdfljs", \n' +
  83. ' ');
  84. });
  85. it('should render attribute expressions', function() {
  86. var state = runWithAttribute('attr={"foo"}', true);
  87. expect(state.g.buffer).toEqual('"foo"');
  88. });
  89. it('should render attribute expressions as not last', function() {
  90. var state = runWithAttribute('attr={"foo"}', false);
  91. expect(state.g.buffer).toEqual('"foo", ');
  92. });
  93. it('should render attribute expressions with spaces', function() {
  94. var state = runWithAttribute('attr={ "foo"\n }', true);
  95. expect(state.g.buffer).toEqual(' "foo"\n ');
  96. });
  97. it('should render attribute expressions with commas before trailing ' +
  98. 'whitespace', function() {
  99. var state = runWithAttribute('attr={\n"foo"\n }', false);
  100. expect(state.g.buffer).toEqual('\n"foo", \n ');
  101. });
  102. it('should render empty child expressions with comments', function() {
  103. var source = '<div>{/*comment*/}</div>';
  104. var ast = esprima.parse(source, {range: true});
  105. var child = ast.body[0].expression.children[0];
  106. var state = utils.createState(source, child);
  107. state.g.position = child.range[0];
  108. jsx.renderJSXExpressionContainer(function() {}, child, true, [], state);
  109. expect(state.g.buffer).toBe('/*comment*/');
  110. });
  111. it('should not render commas after empty child expressions even if they\'re' +
  112. 'not last', function() {
  113. var source = '<div>{/*comment*/}</div>';
  114. var ast = esprima.parse(source, {range: true});
  115. var child = ast.body[0].expression.children[0];
  116. var state = utils.createState(source, child);
  117. state.g.position = child.range[0];
  118. jsx.renderJSXExpressionContainer(function() {}, child, false, [], state);
  119. expect(state.g.buffer).toBe('/*comment*/');
  120. });
  121. });