|
| 1 | +import React from "react"; |
| 2 | +import { createStore } from "redux"; |
| 3 | +import { Provider } from "react-redux"; |
| 4 | +import format from "date-fns/format"; |
| 5 | +import uuid from "uuid/v4"; |
| 6 | +import { |
| 7 | + render, |
| 8 | + screen, |
| 9 | + fireEvent, |
| 10 | + getAllByAltText, |
| 11 | + queryAllByRole, |
| 12 | +} from "@testing-library/react"; |
| 13 | +import HistoryContainer from "../src/client/components/containers/HistoryContainer"; |
| 14 | +import * as actions from "../src/client/actions/actions.js"; |
| 15 | +import reducers from "../src/client/reducers/index.js"; |
| 16 | +import "@testing-library/jest-dom/extend-expect"; |
| 17 | +// import store from "../src/client/store"; |
| 18 | + |
| 19 | +//generate date for today |
| 20 | +const today = new Date(); |
| 21 | +//generate date for yesterday |
| 22 | +const yesterday = new Date(); |
| 23 | +yesterday.setDate(yesterday.getDate() - 1); |
| 24 | +//generate date for two days prior |
| 25 | +const twoDaysAgo = new Date(); |
| 26 | +twoDaysAgo.setDate(twoDaysAgo.getDate() - 2); |
| 27 | + |
| 28 | +describe("history container test", () => { |
| 29 | + const initialState = { |
| 30 | + business: { |
| 31 | + history: [ |
| 32 | + { |
| 33 | + date: format(today, "MM/DD/YYYY"), |
| 34 | + history: [ |
| 35 | + { |
| 36 | + id: 1, |
| 37 | + created_at: today, |
| 38 | + url: "http://google.com", |
| 39 | + request: { |
| 40 | + method: "GET", |
| 41 | + }, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + date: format(yesterday, "MM/DD/YYYY"), |
| 47 | + history: [ |
| 48 | + { |
| 49 | + id: 2, |
| 50 | + created_at: yesterday, |
| 51 | + url: "http://facebook.com", |
| 52 | + request: { |
| 53 | + method: "GET", |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + date: format(twoDaysAgo, "MM/DD/YYYY"), |
| 60 | + history: [ |
| 61 | + { |
| 62 | + id: 3, |
| 63 | + created_at: twoDaysAgo, |
| 64 | + url: "http://instagram.com", |
| 65 | + request: { |
| 66 | + method: "GET", |
| 67 | + }, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + }; |
| 74 | + const renderWithRedux = ( |
| 75 | + component, |
| 76 | + { store = createStore(reducers, initialState) } = {} |
| 77 | + ) => { |
| 78 | + return { |
| 79 | + ...render(<Provider store={store}>{component}</Provider>), |
| 80 | + store, |
| 81 | + }; |
| 82 | + }; |
| 83 | + |
| 84 | + beforeEach(() => { |
| 85 | + renderWithRedux(<HistoryContainer />); |
| 86 | + screen.debug(); |
| 87 | + }); |
| 88 | + test("renders all items in history array from store", () => { |
| 89 | + expect(screen.queryAllByLabelText("queryDate").length).toBe(3); |
| 90 | + }); |
| 91 | + test("correctly renders headers in history container", () => { |
| 92 | + expect(screen.queryByText("Today")).toBeTruthy(); |
| 93 | + expect(screen.queryByText("Yesterday")).toBeTruthy(); |
| 94 | + expect( |
| 95 | + screen.queryByText(format(twoDaysAgo, "ddd, MMM D, YYYY")) |
| 96 | + ).toBeTruthy(); |
| 97 | + }); |
| 98 | + test("correctly renders url for each header in history", () => { |
| 99 | + expect(screen.queryByText("http://google.com")).toBeTruthy(); |
| 100 | + expect(screen.queryByText("http://facebook.com")).toBeTruthy(); |
| 101 | + expect(screen.queryByText("http://instagram.com")).toBeTruthy(); |
| 102 | + }); |
| 103 | + test("correctly renders request method for each header in history", () => { |
| 104 | + expect(screen.queryAllByText("GET").length).toBe(3); |
| 105 | + }); |
| 106 | + test("clear history button renders", () => { |
| 107 | + expect(screen.queryByText("Clear History")).toBeTruthy(); |
| 108 | + }); |
| 109 | + /* Doesn't work in RTL at present */ |
| 110 | + // test("clear history pop-up clears history", () => { |
| 111 | + // store.dispatch(actions.clearHistory()); |
| 112 | + // expect(screen.queryAllByLabelText("queryDate").length).toBe(0); |
| 113 | + // }); |
| 114 | +}); |
| 115 | + |
| 116 | +/* FOR EACH COMPONENT - UNIT TESTING */ |
| 117 | +//renders |
| 118 | +//buttons work (clear history, etc.) |
| 119 | +//forms function (accepts input) |
| 120 | +//state updates appropriately |
0 commit comments