growi-plugin.integ.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: 'growilabs/growi-plugin-unenabled1',
  9. organizationName: 'growilabs',
  10. origin: {
  11. url: 'https://github.com/growilabs/growi-plugin-unenabled1',
  12. },
  13. meta: {
  14. name: '@growi/growi-plugin-unenabled1',
  15. types: [GrowiPluginType.Script],
  16. },
  17. },
  18. {
  19. isEnabled: false,
  20. installedPath: 'growilabs/growi-plugin-unenabled2',
  21. organizationName: 'growilabs',
  22. origin: {
  23. url: 'https://github.com/growilabs/growi-plugin-unenabled2',
  24. },
  25. meta: {
  26. name: '@growi/growi-plugin-unenabled2',
  27. types: [GrowiPluginType.Template],
  28. },
  29. },
  30. {
  31. isEnabled: true,
  32. installedPath: 'growilabs/growi-plugin-example1',
  33. organizationName: 'growilabs',
  34. origin: {
  35. url: 'https://github.com/growilabs/growi-plugin-example1',
  36. },
  37. meta: {
  38. name: '@growi/growi-plugin-example1',
  39. types: [GrowiPluginType.Script],
  40. },
  41. },
  42. {
  43. isEnabled: true,
  44. installedPath: 'growilabs/growi-plugin-example2',
  45. organizationName: 'growilabs',
  46. origin: {
  47. url: 'https://github.com/growilabs/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(
  74. GrowiPluginType.Template,
  75. );
  76. const pluginNames = results.map((p) => p.meta.name);
  77. // then
  78. expect(results.length === 1).toBeTruthy();
  79. expect(pluginNames.includes('@growi/growi-plugin-example2')).toBeTruthy();
  80. });
  81. });
  82. });
  83. describe('GrowiPlugin activate/deactivate', () => {
  84. beforeAll(async () => {
  85. await GrowiPlugin.insertMany([
  86. {
  87. isEnabled: false,
  88. installedPath: 'growilabs/growi-plugin-example1',
  89. organizationName: 'growilabs',
  90. origin: {
  91. url: 'https://github.com/growilabs/growi-plugin-example1',
  92. },
  93. meta: {
  94. name: '@growi/growi-plugin-example1',
  95. types: [GrowiPluginType.Script],
  96. },
  97. },
  98. ]);
  99. });
  100. afterAll(async () => {
  101. await GrowiPlugin.deleteMany({});
  102. });
  103. describe('.activatePlugin', () => {
  104. it('shoud update the property "isEnabled" to true', async () => {
  105. // setup
  106. const plugin = await GrowiPlugin.findOne({});
  107. assert(plugin != null);
  108. expect(plugin.isEnabled).toBeFalsy(); // isEnabled: false
  109. // when
  110. const result = await GrowiPlugin.activatePlugin(plugin._id);
  111. const pluginAfterActivated = await GrowiPlugin.findOne({
  112. _id: plugin._id,
  113. });
  114. // then
  115. expect(result).toEqual('@growi/growi-plugin-example1'); // equals to meta.name
  116. expect(pluginAfterActivated).not.toBeNull();
  117. assert(pluginAfterActivated != null);
  118. expect(pluginAfterActivated.isEnabled).toBeTruthy(); // isEnabled: true
  119. });
  120. });
  121. describe('.deactivatePlugin', () => {
  122. it('shoud update the property "isEnabled" to true', async () => {
  123. // setup
  124. const plugin = await GrowiPlugin.findOne({});
  125. assert(plugin != null);
  126. expect(plugin.isEnabled).toBeTruthy(); // isEnabled: true
  127. // when
  128. const result = await GrowiPlugin.deactivatePlugin(plugin._id);
  129. const pluginAfterActivated = await GrowiPlugin.findOne({
  130. _id: plugin._id,
  131. });
  132. // then
  133. expect(result).toEqual('@growi/growi-plugin-example1'); // equals to meta.name
  134. expect(pluginAfterActivated).not.toBeNull();
  135. assert(pluginAfterActivated != null);
  136. expect(pluginAfterActivated.isEnabled).toBeFalsy(); // isEnabled: false
  137. });
  138. });
  139. });