usePrismTokens.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import {loadPrismLanguage} from 'sentry/utils/prism';
  3. import {usePrismTokens} from 'sentry/utils/usePrismTokens';
  4. jest.unmock('prismjs');
  5. const JS_CODE = `function foo() {
  6. // Returns 'bar'
  7. return 'bar';
  8. }`;
  9. const SINGLE_LINE_CODE = `const a='b'`;
  10. const NESTED_CODE = `<p class="hey">Test</p>`;
  11. describe('usePrismTokens', () => {
  12. beforeAll(async () => {
  13. // Loading all languagues up front makes tests run consistently
  14. await loadPrismLanguage('javascript', {});
  15. await loadPrismLanguage('html', {});
  16. });
  17. it('splits tokens by line', () => {
  18. const {result} = reactHooks.renderHook(usePrismTokens, {
  19. initialProps: {code: JS_CODE, language: 'javascript'},
  20. });
  21. const lines = result.current;
  22. expect(lines).toHaveLength(4);
  23. expect(lines[0]).toEqual([
  24. {children: 'function', className: 'token keyword'},
  25. {children: ' ', className: 'token'},
  26. {children: 'foo', className: 'token function'},
  27. {children: '(', className: 'token punctuation'},
  28. {children: ')', className: 'token punctuation'},
  29. {children: ' ', className: 'token'},
  30. {children: '{', className: 'token punctuation'},
  31. ]);
  32. expect(lines[1]).toEqual([
  33. {children: ' ', className: 'token'},
  34. {children: "// Returns 'bar'", className: 'token comment'},
  35. ]);
  36. expect(lines[2]).toEqual([
  37. {children: ' ', className: 'token'},
  38. {children: 'return', className: 'token keyword'},
  39. {children: ' ', className: 'token'},
  40. {children: "'bar'", className: 'token string'},
  41. {children: ';', className: 'token punctuation'},
  42. ]);
  43. expect(lines[3]).toEqual([{children: '}', className: 'token punctuation'}]);
  44. });
  45. it('works with single line of code', () => {
  46. const {result} = reactHooks.renderHook(usePrismTokens, {
  47. initialProps: {code: SINGLE_LINE_CODE, language: 'javascript'},
  48. });
  49. const lines = result.current;
  50. expect(lines).toEqual([
  51. [
  52. {children: 'const', className: 'token keyword'},
  53. {children: ' a', className: 'token'},
  54. {children: '=', className: 'token operator'},
  55. {children: "'b'", className: 'token string'},
  56. ],
  57. ]);
  58. });
  59. it('falls back when no grammar is available', () => {
  60. jest.spyOn(console, 'warn').mockImplementation();
  61. const {result} = reactHooks.renderHook(usePrismTokens, {
  62. initialProps: {code: JS_CODE, language: 'not-a-language'},
  63. });
  64. const lines = result.current;
  65. expect(lines).toEqual([
  66. [{children: 'function foo() {', className: 'token'}],
  67. [{children: " // Returns 'bar'", className: 'token'}],
  68. [{children: " return 'bar';", className: 'token'}],
  69. [{children: '}', className: 'token'}],
  70. ]);
  71. });
  72. it('works with nested tokens', () => {
  73. const {result} = reactHooks.renderHook(usePrismTokens, {
  74. initialProps: {code: NESTED_CODE, language: 'html'},
  75. });
  76. const lines = result.current;
  77. expect(lines).toEqual([
  78. [
  79. {children: '<', className: 'token tag punctuation'},
  80. {children: 'p', className: 'token tag'},
  81. {children: ' ', className: 'token tag'},
  82. {children: 'class', className: 'token tag attr-name'},
  83. {children: '=', className: 'token tag attr-value punctuation attr-equals'},
  84. {children: '"', className: 'token tag attr-value punctuation'},
  85. {children: 'hey', className: 'token tag attr-value'},
  86. {children: '"', className: 'token tag attr-value punctuation'},
  87. {children: '>', className: 'token tag punctuation'},
  88. {children: 'Test', className: 'token'},
  89. {children: '</', className: 'token tag punctuation'},
  90. {children: 'p', className: 'token tag'},
  91. {children: '>', className: 'token tag punctuation'},
  92. ],
  93. ]);
  94. });
  95. });