pluginsStore.spec.tsx 4.6 KB

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