pluginsStore.spec.jsx 4.6 KB

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