plugin.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { expect, test, describe, it } from 'vitest'
  2. import Plugin from '../../lib/core/plugin'
  3. describe('Plugin', () => {
  4. const testContext = {}
  5. it('install plugins', () => {
  6. const plugin1 = {
  7. name: 'p1',
  8. use() {},
  9. before() {}
  10. }
  11. const plugin2 = {
  12. name: 'p2',
  13. use2() {}
  14. }
  15. test('install of options.plugins', () => {
  16. const pluginInstance = new Plugin({
  17. plugins: [plugin1, plugin2],
  18. hooks: ['before'],
  19. context: testContext
  20. })
  21. expect(pluginInstance.space.p1).toEqual(plugin1)
  22. expect(pluginInstance.space.p2).toEqual(plugin2)
  23. })
  24. test('install of install()', () => {
  25. const pluginInstance = new Plugin({
  26. hooks: ['before'],
  27. context: testContext
  28. })
  29. pluginInstance.install([plugin1, plugin2])
  30. expect(pluginInstance.space.p1).toEqual(plugin1)
  31. expect(pluginInstance.space.p2).toEqual(plugin2)
  32. })
  33. })
  34. it('install and use plugin', () => {
  35. const pluginInstance = new Plugin({
  36. hooks: ['before', 'after'],
  37. context: testContext
  38. })
  39. const samplePlugin = {
  40. name: 'samplePlugin',
  41. use(context) {
  42. context.used = true
  43. },
  44. before() {
  45. console.log('Before hook executed')
  46. },
  47. after() {
  48. console.log('After hook executed')
  49. }
  50. }
  51. pluginInstance.use(samplePlugin)
  52. test('检查 space 调用', () => {
  53. expect(pluginInstance.space.samplePlugin).toEqual(samplePlugin)
  54. })
  55. test('检查 hooks', () => {
  56. expect(pluginInstance.get('before').length).toBe(1)
  57. })
  58. })
  59. it('apply hooks', () => {
  60. const pluginInstance = new Plugin({
  61. hooks: ['customHook'],
  62. context: testContext
  63. })
  64. test('apply customHook', () => {
  65. const customHandler = (value) => {
  66. expect(value).toBe(42)
  67. }
  68. pluginInstance.use({
  69. customHook: customHandler
  70. })
  71. const appliedHandler = pluginInstance.apply('customHook')
  72. appliedHandler(42)
  73. })
  74. test('apply not register hooks', () => {
  75. let result = null
  76. const tempHook = pluginInstance.apply('not-register-hooks', (value) => {
  77. expect(value).toBe('not')
  78. result = value
  79. })
  80. tempHook('not')
  81. expect(result).toBe('not')
  82. })
  83. test('apply not register hooks function', () => {
  84. let result = null
  85. const tempHook = pluginInstance.apply('not-register-hooks', 'xxx')
  86. expect(tempHook).toBe(null)
  87. })
  88. })
  89. it('get hooks', () => {
  90. const pluginInstance = new Plugin({
  91. hooks: ['getHook'],
  92. context: testContext
  93. })
  94. const getHookHandler1 = () => console.log('Get Hook Handler 1')
  95. const getHookHandler2 = () => console.log('Get Hook Handler 2')
  96. pluginInstance.use({
  97. getHook: getHookHandler1
  98. })
  99. pluginInstance.use({
  100. getHook: getHookHandler2
  101. })
  102. test('get has hooks length', () => {
  103. const hooks = pluginInstance.get('getHook')
  104. expect(hooks.length).toBe(2)
  105. })
  106. test('get not has hooks length', () => {
  107. const hooks = pluginInstance.get('getNotHook')
  108. expect(hooks.length).toEqual([])
  109. })
  110. })
  111. })