stylePluginScoped.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { compileStyle } from '../src/compileStyle'
  2. import { mockWarn } from '@vue/runtime-test'
  3. function compile(source: string): string {
  4. const res = compileStyle({
  5. source,
  6. filename: 'test.css',
  7. id: 'test'
  8. })
  9. if (res.errors.length) {
  10. res.errors.forEach(err => {
  11. console.error(err)
  12. })
  13. expect(res.errors.length).toBe(0)
  14. }
  15. return res.code
  16. }
  17. describe('SFC scoped CSS', () => {
  18. mockWarn()
  19. test('simple selectors', () => {
  20. expect(compile(`h1 { color: red; }`)).toMatch(`h1[test] { color: red;`)
  21. expect(compile(`.foo { color: red; }`)).toMatch(`.foo[test] { color: red;`)
  22. })
  23. test('descendent selector', () => {
  24. expect(compile(`h1 .foo { color: red; }`)).toMatch(
  25. `h1 .foo[test] { color: red;`
  26. )
  27. })
  28. test('multiple selectors', () => {
  29. expect(compile(`h1 .foo, .bar, .baz { color: red; }`)).toMatch(
  30. `h1 .foo[test], .bar[test], .baz[test] { color: red;`
  31. )
  32. })
  33. test('pseudo class', () => {
  34. expect(compile(`.foo:after { color: red; }`)).toMatch(
  35. `.foo[test]:after { color: red;`
  36. )
  37. })
  38. test('pseudo element', () => {
  39. expect(compile(`::selection { display: none; }`)).toMatch(
  40. '[test]::selection {'
  41. )
  42. })
  43. test('spaces before pseudo element', () => {
  44. const code = compile(`.abc, ::selection { color: red; }`)
  45. expect(code).toMatch('.abc[test],')
  46. expect(code).toMatch('[test]::selection {')
  47. })
  48. test('::v-deep', () => {
  49. expect(compile(`::v-deep(.foo) { color: red; }`)).toMatch(
  50. `[test] .foo { color: red;`
  51. )
  52. expect(compile(`::v-deep(.foo .bar) { color: red; }`)).toMatch(
  53. `[test] .foo .bar { color: red;`
  54. )
  55. expect(compile(`.baz .qux ::v-deep(.foo .bar) { color: red; }`)).toMatch(
  56. `.baz .qux[test] .foo .bar { color: red;`
  57. )
  58. })
  59. test('::v-slotted', () => {
  60. expect(compile(`::v-slotted(.foo) { color: red; }`)).toMatch(
  61. `.foo[test-s] { color: red;`
  62. )
  63. expect(compile(`::v-slotted(.foo .bar) { color: red; }`)).toMatch(
  64. `.foo .bar[test-s] { color: red;`
  65. )
  66. expect(compile(`.baz .qux ::v-slotted(.foo .bar) { color: red; }`)).toMatch(
  67. `.baz .qux[test] .foo .bar[test-s] { color: red;`
  68. )
  69. })
  70. test('::v-global', () => {
  71. expect(compile(`::v-global(.foo) { color: red; }`)).toMatch(
  72. `.foo { color: red;`
  73. )
  74. expect(compile(`::v-global(.foo .bar) { color: red; }`)).toMatch(
  75. `.foo .bar { color: red;`
  76. )
  77. // global ignores anything before it
  78. expect(compile(`.baz .qux ::v-global(.foo .bar) { color: red; }`)).toMatch(
  79. `.foo .bar { color: red;`
  80. )
  81. })
  82. test('scoped keyframes', () => {
  83. const style = compile(`
  84. .anim {
  85. animation: color 5s infinite, other 5s;
  86. }
  87. .anim-2 {
  88. animation-name: color;
  89. animation-duration: 5s;
  90. }
  91. .anim-3 {
  92. animation: 5s color infinite, 5s other;
  93. }
  94. .anim-multiple {
  95. animation: color 5s infinite, opacity 2s;
  96. }
  97. .anim-multiple-2 {
  98. animation-name: color, opacity;
  99. animation-duration: 5s, 2s;
  100. }
  101. @keyframes color {
  102. from { color: red; }
  103. to { color: green; }
  104. }
  105. @-webkit-keyframes color {
  106. from { color: red; }
  107. to { color: green; }
  108. }
  109. @keyframes opacity {
  110. from { opacity: 0; }
  111. to { opacity: 1; }
  112. }
  113. @-webkit-keyframes opacity {
  114. from { opacity: 0; }
  115. to { opacity: 1; }
  116. }
  117. `)
  118. expect(style).toContain(
  119. `.anim[test] {\n animation: color-test 5s infinite, other 5s;`
  120. )
  121. expect(style).toContain(`.anim-2[test] {\n animation-name: color-test`)
  122. expect(style).toContain(
  123. `.anim-3[test] {\n animation: 5s color-test infinite, 5s other;`
  124. )
  125. expect(style).toContain(`@keyframes color-test {`)
  126. expect(style).toContain(`@-webkit-keyframes color-test {`)
  127. expect(style).toContain(
  128. `.anim-multiple[test] {\n animation: color-test 5s infinite,opacity-test 2s;`
  129. )
  130. expect(style).toContain(
  131. `.anim-multiple-2[test] {\n animation-name: color-test,opacity-test;`
  132. )
  133. expect(style).toContain(`@keyframes opacity-test {`)
  134. expect(style).toContain(`@-webkit-keyframes opacity-test {`)
  135. })
  136. // vue-loader/#1370
  137. test('spaces after selector', () => {
  138. const { code } = compileStyle({
  139. source: `.foo , .bar { color: red; }`,
  140. filename: 'test.css',
  141. id: 'test'
  142. })
  143. expect(code).toMatch(`.foo[test], .bar[test] { color: red;`)
  144. })
  145. describe('deprecated syntax', () => {
  146. test('::v-deep as combinator', () => {
  147. expect(compile(`::v-deep .foo { color: red; }`)).toMatch(
  148. `[test] .foo { color: red;`
  149. )
  150. expect(compile(`.bar ::v-deep .foo { color: red; }`)).toMatch(
  151. `.bar[test] .foo { color: red;`
  152. )
  153. expect(
  154. `::v-deep usage as a combinator has been deprecated.`
  155. ).toHaveBeenWarned()
  156. })
  157. test('>>> (deprecated syntax)', () => {
  158. const code = compile(`>>> .foo { color: red; }`)
  159. expect(code).toMatch(`[test] .foo { color: red;`)
  160. expect(
  161. `the >>> and /deep/ combinators have been deprecated.`
  162. ).toHaveBeenWarned()
  163. })
  164. test('/deep/ (deprecated syntax)', () => {
  165. const code = compile(`/deep/ .foo { color: red; }`)
  166. expect(code).toMatch(`[test] .foo { color: red;`)
  167. expect(
  168. `the >>> and /deep/ combinators have been deprecated.`
  169. ).toHaveBeenWarned()
  170. })
  171. })
  172. })