schemes.jsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from "react"
  2. import { shallow } from "enzyme"
  3. import { fromJS } from "immutable"
  4. import Schemes from "components/schemes"
  5. describe("<Schemes/>", function(){
  6. it("calls props.specActions.setScheme() when no currentScheme is selected", function(){
  7. let setSchemeSpy = jest.fn()
  8. // Given
  9. let props = {
  10. specActions: {
  11. setScheme: setSchemeSpy
  12. },
  13. schemes: fromJS([
  14. "http",
  15. "https"
  16. ]),
  17. currentScheme: undefined,
  18. path: "/test",
  19. method: "get"
  20. }
  21. // When
  22. let wrapper = shallow(<Schemes {...props}/>)
  23. // Then currentScheme should default to first scheme in options list
  24. expect(props.specActions.setScheme).toHaveBeenCalledWith("http", "/test" , "get")
  25. // When the currentScheme is no longer in the list of options
  26. props.schemes = fromJS([
  27. "https"
  28. ])
  29. wrapper.setProps(props)
  30. // Then currentScheme should default to first scheme in options list, again
  31. expect(props.specActions.setScheme).toHaveBeenCalledWith("https", "/test", "get")
  32. })
  33. it("doesn't call props.specActions.setScheme() when schemes hasn't changed", function(){
  34. let setSchemeSpy = jest.fn()
  35. // Given
  36. let props = {
  37. specActions: {
  38. setScheme: setSchemeSpy
  39. },
  40. schemes: fromJS([
  41. "http",
  42. "https"
  43. ]),
  44. currentScheme: "https"
  45. }
  46. // When
  47. let wrapper = shallow(<Schemes {...props}/>)
  48. // Should be called initially, to set the global state
  49. expect(setSchemeSpy.mock.calls.length).toEqual(1)
  50. // After an update
  51. wrapper.instance().componentWillReceiveProps(props)
  52. // Should not be called again, since `currentScheme` is in schemes
  53. expect(setSchemeSpy.mock.calls.length).toEqual(1)
  54. })
  55. })