You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.0 KiB

4 years ago
  1. import addStylesClient from '../lib/addStylesClient'
  2. import addStylesServer from '../lib/addStylesServer'
  3. const mockedList = [
  4. [1, 'h1 { color: red; }', ''],
  5. [1, 'p { color: green; }', ''],
  6. [2, 'span { color: blue; }', ''],
  7. [2, 'span { color: blue; }', 'print']
  8. ]
  9. test('addStylesClient (dev)', () => {
  10. const update = addStylesClient('foo', mockedList, false)
  11. assertStylesMatch(mockedList)
  12. const mockedList2 = mockedList.slice(1, 3)
  13. update(mockedList2)
  14. assertStylesMatch(mockedList2)
  15. update()
  16. expect(document.querySelectorAll('style').length).toBe(0)
  17. })
  18. test('addStylesClient (prod)', () => {
  19. const update = addStylesClient('foo', mockedList, true)
  20. assertStylesMatch(mockedList)
  21. const mockedList2 = mockedList.slice(2)
  22. update(mockedList2)
  23. assertStylesMatch(mockedList2)
  24. update()
  25. expect(document.querySelectorAll('style').length).toBe(0)
  26. })
  27. test('addStylesClient (dev + ssr)', () => {
  28. mockSSRTags(mockedList, 'foo')
  29. const update = addStylesClient('foo', mockedList, false)
  30. assertStylesMatch(mockedList)
  31. update()
  32. expect(document.querySelectorAll('style').length).toBe(0)
  33. })
  34. test('addStylesClient (prod + ssr)', () => {
  35. mockProdSSRTags(mockedList, 'foo')
  36. const update = addStylesClient('foo', mockedList, true)
  37. expect(document.querySelectorAll('style').length).toBe(1)
  38. })
  39. test('addStylesServer (dev)', () => {
  40. const context = global.__VUE_SSR_CONTEXT__ = {}
  41. addStylesServer('foo', mockedList, false)
  42. expect(context.styles).toBe(
  43. `<style data-vue-ssr-id="foo:0">h1 { color: red; }</style>` +
  44. `<style data-vue-ssr-id="foo:1">p { color: green; }</style>` +
  45. `<style data-vue-ssr-id="foo:2">span { color: blue; }</style>` +
  46. `<style data-vue-ssr-id="foo:3" media="print">span { color: blue; }</style>`
  47. )
  48. })
  49. test('addStylesServer (prod)', () => {
  50. const context = global.__VUE_SSR_CONTEXT__ = {}
  51. addStylesServer('foo', mockedList, true)
  52. expect(context.styles).toBe(
  53. `<style data-vue-ssr-id="foo:0 foo:1 foo:2">` +
  54. `h1 { color: red; }\np { color: green; }\nspan { color: blue; }` +
  55. `</style>` +
  56. `<style data-vue-ssr-id="foo:3" media="print">span { color: blue; }</style>`
  57. )
  58. })
  59. // --- helpers ---
  60. function assertStylesMatch (list) {
  61. const styles = document.querySelectorAll('style')
  62. expect(styles.length).toBe(list.length)
  63. ;[].forEach.call(styles, (style, i) => {
  64. expect(style.textContent.indexOf(list[i][1]) > -1).toBe(true)
  65. })
  66. }
  67. function mockSSRTags (list, parentId) {
  68. list.forEach((item, i) => {
  69. const style = document.createElement('style')
  70. style.setAttribute('data-vue-ssr-id', `${parentId}:${i}`)
  71. style.textContent = item[1]
  72. if (item[2]) {
  73. style.setAttribute('media', item[2])
  74. }
  75. document.head.appendChild(style)
  76. })
  77. }
  78. function mockProdSSRTags (list, parentId) {
  79. const style = document.createElement('style')
  80. style.setAttribute('data-vue-ssr-id', list.map((item, i) => `${parentId}:${i}`).join(' '))
  81. style.textContent = list.map(item => item[1]).join('\n')
  82. document.head.appendChild(style)
  83. }