growi-plugin.integ.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { GrowiPluginType } from '@growi/core';
  2. import { GrowiPlugin } from './growi-plugin';
  3. describe('GrowiPlugin find methods', () => {
  4. beforeAll(async() => {
  5. await GrowiPlugin.insertMany([
  6. {
  7. isEnabled: false,
  8. installedPath: 'weseek/growi-plugin-unenabled1',
  9. organizationName: 'weseek',
  10. origin: {
  11. url: 'https://github.com/weseek/growi-plugin-unenabled1',
  12. },
  13. meta: {
  14. name: '@growi/growi-plugin-unenabled1',
  15. types: [GrowiPluginType.Script],
  16. },
  17. },
  18. {
  19. isEnabled: false,
  20. installedPath: 'weseek/growi-plugin-unenabled2',
  21. organizationName: 'weseek',
  22. origin: {
  23. url: 'https://github.com/weseek/growi-plugin-unenabled2',
  24. },
  25. meta: {
  26. name: '@growi/growi-plugin-unenabled2',
  27. types: [GrowiPluginType.Template],
  28. },
  29. },
  30. {
  31. isEnabled: true,
  32. installedPath: 'weseek/growi-plugin-example1',
  33. organizationName: 'weseek',
  34. origin: {
  35. url: 'https://github.com/weseek/growi-plugin-example1',
  36. },
  37. meta: {
  38. name: '@growi/growi-plugin-example1',
  39. types: [GrowiPluginType.Script],
  40. },
  41. },
  42. {
  43. isEnabled: true,
  44. installedPath: 'weseek/growi-plugin-example2',
  45. organizationName: 'weseek',
  46. origin: {
  47. url: 'https://github.com/weseek/growi-plugin-example2',
  48. },
  49. meta: {
  50. name: '@growi/growi-plugin-example2',
  51. types: [GrowiPluginType.Template],
  52. },
  53. },
  54. ]);
  55. });
  56. afterAll(async() => {
  57. await GrowiPlugin.deleteMany({});
  58. });
  59. describe.concurrent('.findEnabledPlugins', () => {
  60. it('shoud returns documents which isEnabled is true', async() => {
  61. // when
  62. const results = await GrowiPlugin.findEnabledPlugins();
  63. const pluginNames = results.map(p => p.meta.name);
  64. // then
  65. expect(results.length === 2).toBeTruthy();
  66. expect(pluginNames.includes('@growi/growi-plugin-example1')).toBeTruthy();
  67. expect(pluginNames.includes('@growi/growi-plugin-example2')).toBeTruthy();
  68. });
  69. });
  70. describe.concurrent('.findEnabledPluginsByType', () => {
  71. it("shoud returns documents which type is 'template'", async() => {
  72. // when
  73. const results = await GrowiPlugin.findEnabledPluginsByType(GrowiPluginType.Template);
  74. const pluginNames = results.map(p => p.meta.name);
  75. // then
  76. expect(results.length === 1).toBeTruthy();
  77. expect(pluginNames.includes('@growi/growi-plugin-example2')).toBeTruthy();
  78. });
  79. });
  80. });
  81. describe('GrowiPlugin activate/deactivate', () => {
  82. beforeAll(async() => {
  83. await GrowiPlugin.insertMany([
  84. {
  85. isEnabled: false,
  86. installedPath: 'weseek/growi-plugin-example1',
  87. organizationName: 'weseek',
  88. origin: {
  89. url: 'https://github.com/weseek/growi-plugin-example1',
  90. },
  91. meta: {
  92. name: '@growi/growi-plugin-example1',
  93. types: [GrowiPluginType.Script],
  94. },
  95. },
  96. ]);
  97. });
  98. afterAll(async() => {
  99. await GrowiPlugin.deleteMany({});
  100. });
  101. describe('.activatePlugin', () => {
  102. it('shoud update the property "isEnabled" to true', async() => {
  103. // setup
  104. const plugin = await GrowiPlugin.findOne({});
  105. assert(plugin != null);
  106. expect(plugin.isEnabled).toBeFalsy(); // isEnabled: false
  107. // when
  108. const result = await GrowiPlugin.activatePlugin(plugin._id);
  109. const pluginAfterActivated = await GrowiPlugin.findOne({ _id: plugin._id });
  110. // then
  111. expect(result).toEqual('@growi/growi-plugin-example1'); // equals to meta.name
  112. expect(pluginAfterActivated).not.toBeNull();
  113. assert(pluginAfterActivated != null);
  114. expect(pluginAfterActivated.isEnabled).toBeTruthy(); // isEnabled: true
  115. });
  116. });
  117. describe('.deactivatePlugin', () => {
  118. it('shoud update the property "isEnabled" to true', async() => {
  119. // setup
  120. const plugin = await GrowiPlugin.findOne({});
  121. assert(plugin != null);
  122. expect(plugin.isEnabled).toBeTruthy(); // isEnabled: true
  123. // when
  124. const result = await GrowiPlugin.deactivatePlugin(plugin._id);
  125. const pluginAfterActivated = await GrowiPlugin.findOne({ _id: plugin._id });
  126. // then
  127. expect(result).toEqual('@growi/growi-plugin-example1'); // equals to meta.name
  128. expect(pluginAfterActivated).not.toBeNull();
  129. assert(pluginAfterActivated != null);
  130. expect(pluginAfterActivated.isEnabled).toBeFalsy(); // isEnabled: false
  131. });
  132. });
  133. });