pluginsStore.spec.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import PluginActions from 'sentry/actions/pluginActions';
  2. import PluginsStore from 'sentry/stores/pluginsStore';
  3. describe('PluginsStore', function () {
  4. beforeAll(function () {
  5. jest.spyOn(PluginsStore, 'trigger');
  6. });
  7. beforeEach(function () {
  8. PluginsStore.trigger.mockReset();
  9. });
  10. afterEach(function () {});
  11. it('has correct initial state', function () {
  12. PluginsStore.reset();
  13. expect(PluginsStore.getState()).toEqual({
  14. loading: true,
  15. error: null,
  16. pageLinks: null,
  17. plugins: [],
  18. });
  19. });
  20. describe('fetchAll', function () {
  21. beforeEach(function () {
  22. PluginsStore.reset();
  23. });
  24. it('has correct state when all plugins fetched successfully', function () {
  25. PluginActions.fetchAll.trigger();
  26. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  27. loading: true,
  28. error: null,
  29. pageLinks: null,
  30. plugins: [],
  31. });
  32. PluginActions.fetchAllSuccess.trigger(TestStubs.Plugins(), {pageLinks: null});
  33. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  34. loading: false,
  35. error: null,
  36. pageLinks: null,
  37. plugins: TestStubs.Plugins(),
  38. });
  39. });
  40. it('has correct state when error in fetching all plugins', function () {
  41. PluginActions.fetchAll.trigger();
  42. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  43. loading: true,
  44. error: null,
  45. pageLinks: null,
  46. plugins: [],
  47. });
  48. PluginActions.fetchAllError.trigger({responseJSON: {message: 'Error'}});
  49. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  50. loading: false,
  51. error: {responseJSON: {message: 'Error'}},
  52. pageLinks: null,
  53. plugins: [],
  54. });
  55. });
  56. it('does not reset loading state on consecutive fetches', function () {
  57. PluginActions.fetchAll.trigger();
  58. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  59. loading: true,
  60. error: null,
  61. pageLinks: null,
  62. plugins: [],
  63. });
  64. PluginActions.fetchAllSuccess.trigger(TestStubs.Plugins(), {pageLinks: null});
  65. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  66. loading: false,
  67. error: null,
  68. pageLinks: null,
  69. plugins: TestStubs.Plugins(),
  70. });
  71. PluginActions.fetchAll.trigger();
  72. expect(PluginsStore.trigger).toHaveBeenCalledWith({
  73. loading: false,
  74. error: null,
  75. pageLinks: null,
  76. plugins: TestStubs.Plugins(),
  77. });
  78. });
  79. });
  80. describe('update', function () {
  81. const plugin = TestStubs.Plugin();
  82. beforeEach(function () {
  83. PluginsStore.reset();
  84. PluginsStore.plugins = new Map(TestStubs.Plugins().map(p => [p.id, p]));
  85. });
  86. it('has optimistic state when updating', function () {
  87. PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
  88. const state = PluginsStore.getState();
  89. expect(state).toMatchObject({
  90. error: null,
  91. pageLinks: null,
  92. });
  93. expect(state.plugins[0]).toMatchObject({
  94. ...plugin,
  95. id: 'amazon-sqs',
  96. name: 'Amazon Sqs',
  97. });
  98. // Doesn't update other plugins plz
  99. expect(state.plugins[1]).toMatchObject({
  100. id: 'github',
  101. name: 'GitHub',
  102. });
  103. });
  104. it('saves old plugin state', function () {
  105. PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
  106. const state = PluginsStore.getState();
  107. expect(state).toMatchObject({
  108. error: null,
  109. pageLinks: null,
  110. });
  111. expect(PluginsStore.updating.get('amazon-sqs')).toMatchObject({
  112. ...plugin,
  113. id: 'amazon-sqs',
  114. name: 'Amazon SQS',
  115. });
  116. });
  117. it('removes old plugin state on successful update', function () {
  118. PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
  119. expect(PluginsStore.updating.get('amazon-sqs')).toMatchObject({
  120. ...plugin,
  121. id: 'amazon-sqs',
  122. name: 'Amazon SQS',
  123. });
  124. PluginActions.updateSuccess.trigger('amazon-sqs');
  125. expect(PluginsStore.getState().plugins[0]).toMatchObject({
  126. id: 'amazon-sqs',
  127. name: 'Amazon Sqs',
  128. });
  129. expect(PluginsStore.updating.get('amazon-sqs')).toEqual(undefined);
  130. });
  131. it('restores old plugin state when update has an error', function () {
  132. PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
  133. expect(PluginsStore.getState().plugins[0]).toMatchObject({
  134. id: 'amazon-sqs',
  135. name: 'Amazon Sqs',
  136. });
  137. PluginActions.updateError.trigger('amazon-sqs');
  138. expect(PluginsStore.getState().plugins[0]).toMatchObject({
  139. id: 'amazon-sqs',
  140. name: 'Amazon SQS',
  141. });
  142. expect(PluginsStore.updating.get('amazon-sqs')).toEqual(undefined);
  143. });
  144. });
  145. });