projectsStore.spec.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import ProjectsStore from 'sentry/stores/projectsStore';
  2. import TeamStore from 'sentry/stores/teamStore';
  3. describe('ProjectsStore', function () {
  4. const teamFoo = TestStubs.Team({
  5. slug: 'team-foo',
  6. });
  7. const teamBar = TestStubs.Team({
  8. slug: 'team-bar',
  9. });
  10. const projectFoo = TestStubs.Project({
  11. id: '2',
  12. slug: 'foo',
  13. name: 'Foo',
  14. teams: [teamFoo],
  15. });
  16. const projectBar = TestStubs.Project({
  17. id: '10',
  18. slug: 'bar',
  19. name: 'Bar',
  20. teams: [teamFoo, teamBar],
  21. });
  22. describe('setting data', function () {
  23. beforeEach(function () {
  24. ProjectsStore.reset();
  25. });
  26. it('correctly manages loading state', function () {
  27. expect(ProjectsStore.getState()).toMatchObject({
  28. projects: [],
  29. loading: true,
  30. });
  31. ProjectsStore.loadInitialData([projectFoo, projectBar]);
  32. expect(ProjectsStore.getState()).toMatchObject({
  33. projects: [projectBar, projectFoo], // projects are returned sorted
  34. loading: false,
  35. });
  36. });
  37. });
  38. describe('updating data', function () {
  39. beforeEach(function () {
  40. ProjectsStore.reset();
  41. ProjectsStore.loadInitialData([projectFoo, projectBar]);
  42. });
  43. it('updates when slug changes', async function () {
  44. ProjectsStore.onChangeSlug('foo', 'new-project');
  45. await tick();
  46. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  47. slug: 'new-project',
  48. });
  49. expect(ProjectsStore.itemsById[projectBar.id]).toBeDefined();
  50. });
  51. it('adds project to store on "create success"', function () {
  52. const project = TestStubs.Project({id: '11', slug: 'created-project'});
  53. const reloadOrgRequest = MockApiClient.addMockResponse({
  54. url: '/organizations/my-org/',
  55. body: {},
  56. });
  57. MockApiClient.addMockResponse({
  58. url: '/organizations/my-org/projects/',
  59. body: [project, projectBar, projectFoo],
  60. });
  61. MockApiClient.addMockResponse({
  62. url: '/organizations/my-org/teams/',
  63. body: [],
  64. });
  65. ProjectsStore.onCreateSuccess(project, 'my-org');
  66. expect(ProjectsStore.itemsById[project.id]).toMatchObject({
  67. id: '11',
  68. slug: 'created-project',
  69. });
  70. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  71. id: '2',
  72. slug: 'foo',
  73. name: 'Foo',
  74. });
  75. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  76. id: '10',
  77. slug: 'bar',
  78. });
  79. expect(reloadOrgRequest).toHaveBeenCalled();
  80. });
  81. it('updates a project in store', function () {
  82. // Create a new project, but should have same id as `projectBar`
  83. const project = TestStubs.Project({id: '10', slug: 'bar', name: 'New Name'});
  84. ProjectsStore.onUpdateSuccess(project);
  85. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  86. id: '10',
  87. slug: 'bar',
  88. name: 'New Name',
  89. });
  90. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  91. id: '2',
  92. slug: 'foo',
  93. name: 'Foo',
  94. });
  95. });
  96. it('can remove a team from a single project', function () {
  97. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  98. teams: [
  99. expect.objectContaining({slug: 'team-foo'}),
  100. expect.objectContaining({slug: 'team-bar'}),
  101. ],
  102. });
  103. ProjectsStore.onRemoveTeam('team-foo', 'bar');
  104. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  105. teams: [expect.objectContaining({slug: 'team-bar'})],
  106. });
  107. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  108. teams: [expect.objectContaining({slug: 'team-foo'})],
  109. });
  110. });
  111. it('removes a team from all projects when team is deleted', function () {
  112. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  113. teams: [
  114. expect.objectContaining({slug: 'team-foo'}),
  115. expect.objectContaining({slug: 'team-bar'}),
  116. ],
  117. });
  118. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  119. teams: [expect.objectContaining({slug: 'team-foo'})],
  120. });
  121. TeamStore.onRemoveSuccess('team-foo');
  122. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  123. teams: [expect.objectContaining({slug: 'team-bar'})],
  124. });
  125. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  126. teams: [],
  127. });
  128. });
  129. it('can add a team to a project', function () {
  130. const team = TestStubs.Team({
  131. slug: 'new-team',
  132. });
  133. ProjectsStore.onAddTeam(team, 'foo');
  134. expect(ProjectsStore.itemsById[projectBar.id]).toMatchObject({
  135. teams: [
  136. expect.objectContaining({slug: 'team-foo'}),
  137. expect.objectContaining({slug: 'team-bar'}),
  138. ],
  139. });
  140. expect(ProjectsStore.itemsById[projectFoo.id]).toMatchObject({
  141. teams: [
  142. expect.objectContaining({slug: 'team-foo'}),
  143. expect.objectContaining({slug: 'new-team'}),
  144. ],
  145. });
  146. });
  147. });
  148. });