App.test.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import "raf/polyfill";
  2. import React from "react";
  3. import { App, appName, Link, titleName } from "./App";
  4. import { configure, shallow, mount } from "enzyme";
  5. import Adapter from "enzyme-adapter-react-16";
  6. import toJson from "enzyme-to-json";
  7. // Configure Enzyme for the React version we are using.
  8. // Could be in a test setup file. Required for React 16, 15, 0.14, 0.13.
  9. configure({ adapter: new Adapter() });
  10. describe("<App />>", () => {
  11. const wrapper = shallow(<App />);
  12. it("should find its elements", () => {
  13. // There is one <p> in <App />: only one shoud be present in shallow rendering.
  14. expect(wrapper.find("p").length).toBe(1);
  15. expect(wrapper.find("p.App-intro").exists()).toBe(true);
  16. expect(wrapper.find("ul").hasClass("tyler")).toBe(true);
  17. expect(wrapper.find("ul").children().length).toBe(3);
  18. expect(wrapper.find("h1").text()).toBe("Welcome to React");
  19. });
  20. it("should find using props", () => {
  21. // "key" and "ref" could not be used that way.
  22. // CSS attribute syntax.
  23. expect(wrapper.find("[text='Some title']").length).toBe(1);
  24. // Object property selector.
  25. expect(wrapper.find({ text: "Some title" }).length).toBe(1);
  26. });
  27. // FIXME cannot make sense of video.
  28. // @see http://airbnb.io/enzyme/docs/api/ReactWrapper/find.html
  29. test.skip("should find using a constructor", () => {
  30. // expect(wrapper.find(function App() { return ... }).length).toBe(1);
  31. });
  32. it("should find using displayName", () => {
  33. const w = mount(<App />);
  34. expect(w.find(appName).length).toBe(1);
  35. expect(w.find(titleName).length).toBe(1);
  36. });
  37. it("matches the snapshot", () => {
  38. const tree = shallow(<App />);
  39. expect(toJson(tree)).toMatchSnapshot();
  40. });
  41. });
  42. describe("<Link>", () => {
  43. const expected = "www.google.com";
  44. it("link component accepts address prop", () => {
  45. const wrapper = shallow(<Link address={expected} />);
  46. // wrapper.instance() is the React component. "props" is a property.
  47. expect(wrapper.instance().props.address).toBe(expected);
  48. });
  49. // wrapper itself is the ShallowWrapper instance, not the React component.
  50. it("tag node renders href correctly", () => {
  51. const wrapper = shallow(<Link address={expected} />);
  52. // Check its rendering:
  53. // console.log(wrapper.html(), wrapper.props());
  54. expect(wrapper.prop("href")).toBe(expected);
  55. // Here "props" is a method, not a property.
  56. expect(wrapper.props().href).toBe(expected);
  57. });
  58. it("returns null with true hide prop", () => {
  59. const wrapper = shallow(<Link address={expected} hide={false} />);
  60. expect(wrapper.find("a").length).toBe(1);
  61. expect(wrapper.props().href).toBe(expected);
  62. // Causes a rerender() of the component.
  63. // - triggers componentWillReceiveProps()
  64. wrapper.setProps({ hide: true });
  65. expect(wrapper.find("a").length).toBe(0);
  66. // Dangerous: some versions of React might inject comments in HTML.
  67. // expect(wrapper.html()).toBeNull();
  68. // expect(wrapper.getNode()).toBeNull();
  69. // Obsolete: getNode() is deprecated.
  70. // Recommended way https://github.com/airbnb/enzyme/issues/52
  71. expect(wrapper.getElement()).toBe(null);
  72. expect(wrapper.getElement()).toBeNull();
  73. // Get() returns the node at the given index on the wrapper.
  74. expect(wrapper.get(0)).toBeNull();
  75. });
  76. });