spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. import { default as JSONFormatter } from '../src/index';
  3. describe('null', () => {
  4. it('should render "null"', () => {
  5. const formatter = new JSONFormatter(null);
  6. const el = formatter.render();
  7. expect(el.textContent).toContain('null');
  8. });
  9. });
  10. describe('undefined', () => {
  11. it('should render "undefined"', () => {
  12. const formatter = new JSONFormatter(undefined);
  13. expect(formatter.render().textContent).toContain('undefined');
  14. });
  15. });
  16. describe('object function constructor', () => {
  17. it('should output "Format"', () => {
  18. function Format() {
  19. }
  20. const obj = new Format();
  21. const formatter = new JSONFormatter(obj);
  22. expect(formatter['constructorName']).toEqual('Format');
  23. });
  24. it('should output "BrokenFormat"', () => {
  25. const failConstructor = 'function BrokenFormat() {Object.assign(}';
  26. const funcNameRegex = /function ([^(]*)/;
  27. const results = (funcNameRegex).exec(failConstructor.toString());
  28. expect(results[1]).toEqual('BrokenFormat');
  29. });
  30. });
  31. describe('function', () => {
  32. it('should render the function', () => {
  33. const formatter = new JSONFormatter(function add(a, b) {
  34. return a + b;
  35. });
  36. const elementText = formatter.render().textContent;
  37. expect(elementText).toContain('function');
  38. expect(elementText).toContain('add');
  39. expect(elementText).toContain('(a, b)');
  40. expect(elementText.trim().match(/function\s[^\(]*\([^\)]*\)\s*(.*)/)[1]).toEqual('{…}');
  41. });
  42. });
  43. describe('string', () => {
  44. it('should render "Hello World!"', () => {
  45. const formatter = new JSONFormatter('Hello World!');
  46. expect(formatter.render().textContent).toContain('Hello World');
  47. });
  48. });
  49. describe('date string', () => {
  50. const formatter = new JSONFormatter(new Date(0).toString());
  51. it('should render "' + (new Date(0)).toString() + '"', () => {
  52. expect(formatter.render().textContent).toContain('"' + (new Date(0)).toString() + '"');
  53. });
  54. it('should assing date class to date string', () => {
  55. const formatter = new JSONFormatter('2015-12-05T18:58:53.727Z');
  56. expect(formatter.render().querySelector('.json-formatter-date')).not.toBeNull();
  57. });
  58. });
  59. describe('date', () => {
  60. const date = new Date();
  61. const formatter = new JSONFormatter(date);
  62. it('should assing date class to date string', () => {
  63. expect(formatter.render().querySelector('.json-formatter-date')).not.toBeNull();
  64. });
  65. it('should render "' + date.toJSON() + '" if useToJSON is true', () => {
  66. expect(formatter.render().textContent).toContain('"' + date.toJSON() + '"');
  67. });
  68. it('should render "No properties" if useToJSON is false', () => {
  69. const formatter = new JSONFormatter(date, 1, { useToJSON: false });
  70. expect(formatter.render().textContent).toContain('Date');
  71. });
  72. });
  73. describe('url string', () => {
  74. const formatter = new JSONFormatter('https://example.com');
  75. it('should render "https://example.com"', () => {
  76. expect(formatter.render().textContent).toContain('"https://example.com"');
  77. });
  78. it('should make a link and add class "url"', () => {
  79. expect(formatter.render().querySelector('a.json-formatter-url')).not.toEqual(null);
  80. });
  81. });
  82. describe('openAtDepth after rendering', () => {
  83. const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
  84. animateOpen: false,
  85. animateClose: false
  86. });
  87. const element = formatter.render();
  88. it('should open at depth 1', () => {
  89. formatter.openAtDepth();
  90. expect(element.outerHTML).toContain('depth1');
  91. expect(element.outerHTML).not.toContain('depth2');
  92. expect(element.outerHTML).not.toContain('depth3');
  93. expect(element.outerHTML).not.toContain('depth4');
  94. });
  95. it('should collapses all', () => {
  96. formatter.openAtDepth(0);
  97. expect(element.outerHTML).not.toContain('depth1');
  98. });
  99. it('should expand all', () => {
  100. formatter.openAtDepth(Infinity);
  101. expect(element.outerHTML).toContain('depth1');
  102. expect(element.outerHTML).toContain('depth2');
  103. expect(element.outerHTML).toContain('depth3');
  104. expect(element.outerHTML).toContain('depth4');
  105. expect(element.outerHTML).toContain('21');
  106. });
  107. });
  108. describe('openAtDepth before any rendering', () => {
  109. const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
  110. animateOpen: false,
  111. animateClose: false
  112. });
  113. it('should open at depth 1', () => {
  114. formatter.openAtDepth();
  115. const element = formatter.render();
  116. expect(element.outerHTML).toContain('depth1');
  117. expect(element.outerHTML).not.toContain('depth2');
  118. expect(element.outerHTML).not.toContain('depth3');
  119. expect(element.outerHTML).not.toContain('depth4');
  120. });
  121. });
  122. describe('toggleOpen after rendering', () => {
  123. it('should toggle', () => {
  124. const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
  125. animateOpen: false,
  126. animateClose: false
  127. });
  128. const element = formatter.render();
  129. expect(element.outerHTML).toContain('Object');
  130. expect(element.outerHTML).toContain('depth1');
  131. formatter.toggleOpen();
  132. expect(element.outerHTML).toContain('Object');
  133. expect(element.outerHTML).not.toContain('depth1');
  134. expect(element.outerHTML).not.toContain('depth2');
  135. expect(element.outerHTML).not.toContain('depth3');
  136. expect(element.outerHTML).not.toContain('depth4');
  137. });
  138. });
  139. describe('toggleOpen before any rendering', () => {
  140. it('should toggle', () => {
  141. const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
  142. animateOpen: false,
  143. animateClose: false
  144. });
  145. formatter.toggleOpen();
  146. const element = formatter.render();
  147. expect(element.outerHTML).toContain('Object');
  148. expect(element.outerHTML).not.toContain('depth1');
  149. expect(element.outerHTML).not.toContain('depth2');
  150. expect(element.outerHTML).not.toContain('depth3');
  151. expect(element.outerHTML).not.toContain('depth4');
  152. });
  153. });