pluginsStore.spec.jsx 4.9 KB

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