helpers.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. export { waitForUpdate } from '../helpers/wait-for-update'
  2. export { nextFrame } from 'web/runtime/transition-util'
  3. // toHaveBeenWarned() matcher
  4. function noop() {}
  5. if (typeof console === 'undefined') {
  6. // @ts-ignore
  7. window.console = {
  8. warn: noop,
  9. error: noop
  10. }
  11. }
  12. // avoid info messages during test
  13. console.info = noop
  14. let asserted
  15. function createCompareFn(spy) {
  16. const hasWarned = msg => {
  17. let count = spy.calls.count()
  18. let args
  19. while (count--) {
  20. args = spy.calls.argsFor(count)
  21. if (args.some(containsMsg)) {
  22. return true
  23. }
  24. }
  25. function containsMsg(arg) {
  26. return arg.toString().indexOf(msg) > -1
  27. }
  28. }
  29. return {
  30. compare: msg => {
  31. asserted = asserted.concat(msg)
  32. const warned = Array.isArray(msg) ? msg.some(hasWarned) : hasWarned(msg)
  33. return {
  34. pass: warned,
  35. message: warned
  36. ? 'Expected message "' + msg + '" not to have been warned'
  37. : 'Expected message "' + msg + '" to have been warned'
  38. }
  39. }
  40. }
  41. }
  42. // define custom matcher for warnings
  43. beforeEach(() => {
  44. asserted = []
  45. // @ts-ignore
  46. spyOn(console, 'warn')
  47. // @ts-ignore
  48. spyOn(console, 'error')
  49. jasmine.addMatchers({
  50. toHaveBeenWarned: () => createCompareFn(console.error),
  51. toHaveBeenTipped: () => createCompareFn(console.warn)
  52. })
  53. })
  54. afterEach(done => {
  55. const warned = msg =>
  56. asserted.some(assertedMsg => msg.toString().indexOf(assertedMsg) > -1)
  57. // @ts-ignore
  58. let count = console.error.calls.count()
  59. let args
  60. while (count--) {
  61. // @ts-ignore
  62. args = console.error.calls.argsFor(count)
  63. if (!warned(args[0])) {
  64. // @ts-ignore
  65. done.fail(`Unexpected console.error message: ${args[0]}`)
  66. return
  67. }
  68. }
  69. done()
  70. })
  71. // injectStyles helper
  72. function insertCSS(text) {
  73. const cssEl = document.createElement('style')
  74. cssEl.textContent = text.trim()
  75. document.head.appendChild(cssEl)
  76. }
  77. const duration = process.env.CI ? 200 : 50
  78. const buffer = process.env.CI ? 20 : 5
  79. let injected = false
  80. export function injectStyles() {
  81. if (injected) return { duration, buffer }
  82. injected = true
  83. insertCSS(`
  84. .test {
  85. -webkit-transition: opacity ${duration}ms ease;
  86. transition: opacity ${duration}ms ease;
  87. }
  88. .group-move {
  89. -webkit-transition: -webkit-transform ${duration}ms ease;
  90. transition: transform ${duration}ms ease;
  91. }
  92. .v-appear, .v-enter, .v-leave-active,
  93. .test-appear, .test-enter, .test-leave-active,
  94. .hello, .bye.active,
  95. .changed-enter {
  96. opacity: 0;
  97. }
  98. .test-anim-enter-active {
  99. animation: test-enter ${duration}ms;
  100. -webkit-animation: test-enter ${duration}ms;
  101. }
  102. .test-anim-leave-active {
  103. animation: test-leave ${duration}ms;
  104. -webkit-animation: test-leave ${duration}ms;
  105. }
  106. .test-anim-long-enter-active {
  107. animation: test-enter ${duration * 2}ms;
  108. -webkit-animation: test-enter ${duration * 2}ms;
  109. }
  110. .test-anim-long-leave-active {
  111. animation: test-leave ${duration * 2}ms;
  112. -webkit-animation: test-leave ${duration * 2}ms;
  113. }
  114. @keyframes test-enter {
  115. from { opacity: 0 }
  116. to { opacity: 1 }
  117. }
  118. @-webkit-keyframes test-enter {
  119. from { opacity: 0 }
  120. to { opacity: 1 }
  121. }
  122. @keyframes test-leave {
  123. from { opacity: 1 }
  124. to { opacity: 0 }
  125. }
  126. @-webkit-keyframes test-leave {
  127. from { opacity: 1 }
  128. to { opacity: 0 }
  129. }
  130. `)
  131. return { duration, buffer }
  132. }