pluginsStore.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {Plugin} from 'sentry-fixture/plugin';
  2. import {Plugins} from 'sentry-fixture/plugins';
  3. import PluginsStore from 'sentry/stores/pluginsStore';
  4. describe('PluginsStore', function () {
  5. beforeEach(function () {
  6. jest.resetAllMocks();
  7. });
  8. it('has correct initial state', function () {
  9. PluginsStore.reset();
  10. expect(PluginsStore.getState()).toEqual({
  11. loading: true,
  12. error: null,
  13. pageLinks: null,
  14. plugins: [],
  15. });
  16. });
  17. describe('fetchAll', function () {
  18. beforeEach(function () {
  19. PluginsStore.reset();
  20. });
  21. it('has correct state when all plugins fetched successfully', function () {
  22. const triggerSpy = jest.spyOn(PluginsStore, 'trigger');
  23. PluginsStore.onFetchAll();
  24. expect(triggerSpy).toHaveBeenCalledWith({
  25. loading: true,
  26. error: null,
  27. pageLinks: null,
  28. plugins: [],
  29. });
  30. PluginsStore.onFetchAllSuccess(Plugins(), {pageLinks: undefined});
  31. expect(triggerSpy).toHaveBeenCalledWith({
  32. loading: false,
  33. error: null,
  34. pageLinks: null,
  35. plugins: Plugins(),
  36. });
  37. });
  38. it('has correct state when error in fetching all plugins', function () {
  39. const triggerSpy = jest.spyOn(PluginsStore, 'trigger');
  40. PluginsStore.onFetchAll();
  41. expect(triggerSpy).toHaveBeenCalledWith({
  42. loading: true,
  43. error: null,
  44. pageLinks: null,
  45. plugins: [],
  46. });
  47. PluginsStore.onFetchAllError({responseJSON: {message: 'Error'}});
  48. expect(triggerSpy).toHaveBeenCalledWith({
  49. loading: false,
  50. error: {responseJSON: {message: 'Error'}},
  51. pageLinks: null,
  52. plugins: [],
  53. });
  54. });
  55. it('does not reset loading state on consecutive fetches', function () {
  56. const triggerSpy = jest.spyOn(PluginsStore, 'trigger');
  57. PluginsStore.onFetchAll();
  58. expect(triggerSpy).toHaveBeenCalledWith({
  59. loading: true,
  60. error: null,
  61. pageLinks: null,
  62. plugins: [],
  63. });
  64. PluginsStore.onFetchAllSuccess(Plugins(), {pageLinks: undefined});
  65. expect(triggerSpy).toHaveBeenCalledWith({
  66. loading: false,
  67. error: null,
  68. pageLinks: null,
  69. plugins: Plugins(),
  70. });
  71. PluginsStore.onFetchAll();
  72. expect(triggerSpy).toHaveBeenCalledWith({
  73. loading: false,
  74. error: null,
  75. pageLinks: null,
  76. plugins: Plugins(),
  77. });
  78. });
  79. });
  80. describe('update', function () {
  81. const plugin = Plugin();
  82. beforeEach(function () {
  83. PluginsStore.reset();
  84. PluginsStore.plugins = new Map(Plugins().map(p => [p.id, p]));
  85. });
  86. it('has optimistic state when updating', function () {
  87. PluginsStore.onUpdate('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. PluginsStore.onUpdate('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. PluginsStore.onUpdate('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. PluginsStore.onUpdateSuccess('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. PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
  133. expect(PluginsStore.getState().plugins[0]).toMatchObject({
  134. id: 'amazon-sqs',
  135. name: 'Amazon Sqs',
  136. });
  137. PluginsStore.onUpdateError('amazon-sqs', new Error('error'));
  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. });