projectsStore.spec.jsx 4.7 KB

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