globalSelectionStore.spec.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import GlobalSelectionStore from 'app/stores/globalSelectionStore';
  2. import {
  3. updateProjects,
  4. updateDateTime,
  5. updateEnvironments,
  6. } from 'app/actionCreators/globalSelection';
  7. jest.mock('app/utils/localStorage', () => {
  8. return {
  9. getItem: () => JSON.stringify({projects: [5], environments: ['staging']}),
  10. setItem: jest.fn(),
  11. };
  12. });
  13. describe('GlobalSelectionStore', function() {
  14. const organization = TestStubs.Organization({
  15. features: ['global-views'],
  16. projects: [TestStubs.Project({id: '5'})],
  17. });
  18. afterEach(function() {
  19. GlobalSelectionStore.reset();
  20. });
  21. it('get()', function() {
  22. expect(GlobalSelectionStore.get()).toEqual({
  23. projects: [],
  24. environments: [],
  25. datetime: {period: null, start: null, end: null, utc: null},
  26. });
  27. });
  28. it('updateProjects()', async function() {
  29. expect(GlobalSelectionStore.get().projects).toEqual([]);
  30. updateProjects([1]);
  31. await tick();
  32. expect(GlobalSelectionStore.get().projects).toEqual([1]);
  33. });
  34. it('updateDateTime()', async function() {
  35. expect(GlobalSelectionStore.get().datetime).toEqual({
  36. period: null,
  37. start: null,
  38. end: null,
  39. utc: null,
  40. });
  41. updateDateTime({period: '2h', start: null, end: null});
  42. await tick();
  43. expect(GlobalSelectionStore.get().datetime).toEqual({
  44. period: '2h',
  45. start: null,
  46. end: null,
  47. });
  48. updateDateTime({
  49. period: null,
  50. start: '2018-08-08T00:00:00',
  51. end: '2018-09-08T00:00:00',
  52. utc: true,
  53. });
  54. await tick();
  55. expect(GlobalSelectionStore.get().datetime).toEqual({
  56. period: null,
  57. start: '2018-08-08T00:00:00',
  58. end: '2018-09-08T00:00:00',
  59. utc: true,
  60. });
  61. updateDateTime({
  62. period: null,
  63. start: null,
  64. end: null,
  65. utc: null,
  66. });
  67. await tick();
  68. expect(GlobalSelectionStore.get().datetime).toEqual({
  69. period: null,
  70. start: null,
  71. end: null,
  72. utc: null,
  73. });
  74. });
  75. it('updateEnvironments()', async function() {
  76. expect(GlobalSelectionStore.get().environments).toEqual([]);
  77. updateEnvironments(['alpha']);
  78. await tick();
  79. expect(GlobalSelectionStore.get().environments).toEqual(['alpha']);
  80. });
  81. it('loadInitialData() - queryParams', async function() {
  82. GlobalSelectionStore.loadInitialData(organization, {
  83. project: '5',
  84. environment: ['staging'],
  85. });
  86. await tick();
  87. expect(GlobalSelectionStore.get().projects).toEqual([5]);
  88. expect(GlobalSelectionStore.get().environments).toEqual(['staging']);
  89. });
  90. it('loadInitialData() - localStorage', async function() {
  91. GlobalSelectionStore.loadInitialData(organization, {});
  92. await tick();
  93. expect(GlobalSelectionStore.get().projects).toEqual([5]);
  94. expect(GlobalSelectionStore.get().environments).toEqual(['staging']);
  95. });
  96. it('loadInitialData() - defaults used if invalid', async function() {
  97. GlobalSelectionStore.loadInitialData(organization, {project: [2]});
  98. await tick();
  99. expect(GlobalSelectionStore.get().projects).toEqual([]);
  100. expect(GlobalSelectionStore.get().environments).toEqual([]);
  101. });
  102. });