getSortOrder.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import resolveConfig from '../src/public/resolve-config'
  2. import { createContext } from '../src/lib/setupContextUtils'
  3. import bigSign from '../src/util/bigSign'
  4. /**
  5. * This is a function that the prettier-plugin-tailwindcss would use. It would
  6. * do the actual sorting based on the classes and order we return from `getClassOrder`.
  7. *
  8. * This way the actual sorting logic is done in the plugin which allows you to
  9. * put unknown classes at the end for example.
  10. *
  11. * @param {Array<[string, bigint]>} arrayOfTuples
  12. * @returns {string}
  13. */
  14. function defaultSort(arrayOfTuples) {
  15. return arrayOfTuples
  16. .sort(([, a], [, z]) => {
  17. if (a === z) return 0
  18. if (a === null) return -1
  19. if (z === null) return 1
  20. return bigSign(a - z)
  21. })
  22. .map(([className]) => className)
  23. .join(' ')
  24. }
  25. it('should return a list of tuples with the sort order', () => {
  26. let input = 'font-bold underline hover:font-medium unknown'
  27. let config = {}
  28. let context = createContext(resolveConfig(config))
  29. expect(context.getClassOrder(input.split(' '))).toEqual([
  30. ['font-bold', expect.any(BigInt)],
  31. ['underline', expect.any(BigInt)],
  32. ['hover:font-medium', expect.any(BigInt)],
  33. // Unknown values receive `null`
  34. ['unknown', null],
  35. ])
  36. })
  37. it.each([
  38. // Utitlies
  39. ['px-3 p-1 py-3', 'p-1 px-3 py-3'],
  40. // Utitlies and components
  41. ['px-4 container', 'container px-4'],
  42. // Utilities with variants
  43. ['px-3 focus:hover:p-3 hover:p-1 py-3', 'px-3 py-3 hover:p-1 focus:hover:p-3'],
  44. // Utitlies with important
  45. ['px-3 !py-4', 'px-3 !py-4'],
  46. ['!py-4 px-3', '!py-4 px-3'],
  47. // Components with variants
  48. ['hover:container container', 'container hover:container'],
  49. // Components and utilities with variants
  50. [
  51. 'focus:hover:container hover:underline hover:container p-1',
  52. 'p-1 hover:container hover:underline focus:hover:container',
  53. ],
  54. // Leave user css order alone, and move to the front
  55. ['b p-1 a', 'b a p-1'],
  56. ['hover:b focus:p-1 a', 'hover:b a focus:p-1'],
  57. // Add special treatment for `group` and `peer`
  58. ['a peer container underline', 'a peer container underline'],
  59. ])('should sort "%s" based on the order we generate them in to "%s"', (input, output) => {
  60. let config = {}
  61. let context = createContext(resolveConfig(config))
  62. expect(defaultSort(context.getClassOrder(input.split(' ')))).toEqual(output)
  63. })
  64. it.each([
  65. // Utitlies
  66. ['tw-px-3 tw-p-1 tw-py-3', 'tw-p-1 tw-px-3 tw-py-3'],
  67. // Utitlies and components
  68. ['tw-px-4 tw-container', 'tw-container tw-px-4'],
  69. // Utilities with variants
  70. [
  71. 'tw-px-3 focus:hover:tw-p-3 hover:tw-p-1 tw-py-3',
  72. 'tw-px-3 tw-py-3 hover:tw-p-1 focus:hover:tw-p-3',
  73. ],
  74. // Utitlies with important
  75. ['tw-px-3 !tw-py-4', 'tw-px-3 !tw-py-4'],
  76. ['!tw-py-4 tw-px-3', '!tw-py-4 tw-px-3'],
  77. // Components with variants
  78. ['hover:tw-container tw-container', 'tw-container hover:tw-container'],
  79. // Components and utilities with variants
  80. [
  81. 'focus:hover:tw-container hover:tw-underline hover:tw-container tw-p-1',
  82. 'tw-p-1 hover:tw-container hover:tw-underline focus:hover:tw-container',
  83. ],
  84. // Leave user css order alone, and move to the front
  85. ['b tw-p-1 a', 'b a tw-p-1'],
  86. ['hover:b focus:tw-p-1 a', 'hover:b a focus:tw-p-1'],
  87. // Add special treatment for `group` and `peer`
  88. ['a tw-peer tw-container tw-underline', 'a tw-peer tw-container tw-underline'],
  89. ])(
  90. 'should sort "%s" with prefixex based on the order we generate them in to "%s"',
  91. (input, output) => {
  92. let config = { prefix: 'tw-' }
  93. let context = createContext(resolveConfig(config))
  94. expect(defaultSort(context.getClassOrder(input.split(' ')))).toEqual(output)
  95. }
  96. )