withEnvironmentInQueryString.spec.jsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from 'react';
  2. import {shallow} from 'enzyme';
  3. import {browserHistory} from 'react-router';
  4. import withEnvironmentInQueryString from 'app/utils/withEnvironmentInQueryString';
  5. import LatestContextStore from 'app/stores/latestContextStore';
  6. class BasicComponent extends React.Component {
  7. render() {
  8. return <div>test component</div>;
  9. }
  10. }
  11. const WrappedComponent = withEnvironmentInQueryString(BasicComponent);
  12. describe('withEnvironmentInQueryString', function() {
  13. beforeEach(function() {
  14. browserHistory.replace = jest.fn();
  15. LatestContextStore.onSetActiveOrganization({});
  16. });
  17. describe('updates environment', function() {
  18. let wrapper;
  19. beforeEach(function() {
  20. LatestContextStore.onSetActiveEnvironment({
  21. name: 'prod',
  22. });
  23. wrapper = shallow(
  24. <WrappedComponent location={{pathname: 'http://lol/', query: {}}} />,
  25. TestStubs.routerContext()
  26. );
  27. });
  28. it('passes environment prop to component', function() {
  29. expect(wrapper.prop('environment').name).toBe('prod');
  30. });
  31. it('replaces browser history', function() {
  32. expect(browserHistory.replace).toHaveBeenCalledWith('http://lol/?environment=prod');
  33. });
  34. });
  35. describe('handles correct environment value', function() {
  36. let wrapper;
  37. beforeEach(function() {
  38. LatestContextStore.onSetActiveEnvironment({
  39. name: 'prod',
  40. });
  41. wrapper = shallow(
  42. <WrappedComponent
  43. location={{pathname: 'http://lol', query: {environment: 'prod'}}}
  44. />,
  45. TestStubs.routerContext()
  46. );
  47. });
  48. it('passes environment prop to component', function() {
  49. expect(wrapper.prop('environment').name).toBe('prod');
  50. });
  51. it('does not replace browser history', function() {
  52. expect(browserHistory.replace).not.toHaveBeenCalled();
  53. });
  54. });
  55. describe('removes invalid environment value', function() {
  56. let wrapper;
  57. beforeEach(function() {
  58. LatestContextStore.onSetActiveEnvironment({name: 'staging'});
  59. wrapper = shallow(
  60. <WrappedComponent
  61. location={{pathname: 'http://lol/', query: {environment: 'prod'}}}
  62. />,
  63. TestStubs.routerContext()
  64. );
  65. });
  66. it('passes environment prop to component', function() {
  67. expect(wrapper.prop('environment').name).toBe('staging');
  68. });
  69. it('replaces browser history', function() {
  70. expect(browserHistory.replace).toHaveBeenCalledWith(
  71. 'http://lol/?environment=staging'
  72. );
  73. });
  74. });
  75. });