-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4 - Added the ability to add headers in the routes
- Loading branch information
Showing
8 changed files
with
299 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import uuid from "uuid/v4"; | ||
|
||
export default function({ index, data = {}, onBlur = () => {}, onRemove = () => {} } = {}) { | ||
const { id = uuid(), header: initialHeader, value: initialValue } = data; | ||
|
||
const [header, setHeader] = useState(initialHeader); | ||
const [value, setValue] = useState(initialValue); | ||
|
||
const update = (field, inputValue) => { | ||
field === "header" ? setHeader(inputValue) : setValue(inputValue); | ||
}; | ||
|
||
useEffect(() => { | ||
if (header && value) onBlur({ id, header, value }); | ||
}, [header, value]); | ||
|
||
return ( | ||
<div className="columns Header" aria-label="header"> | ||
<div className="control column"> | ||
<input className="input" placeholder="header" value={header} aria-label="header-key" onChange={e => update("header", e.target.value)} /> | ||
</div> | ||
<div className="control column"> | ||
<input className="input" placeholder="value" value={value} aria-label="header-value" onChange={e => update("value", e.target.value)} /> | ||
</div> | ||
<div className="control column is-1 Header__Remove" onClick={() => onRemove(id)}> | ||
<i className="far fa-times-circle" aria-label="remove-header" /> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// __tests__/fetch.test.js | ||
import React from "react"; | ||
import { render, fireEvent, cleanup } from "react-testing-library"; | ||
import "jest-dom/extend-expect"; | ||
import HeaderInput from "./"; | ||
|
||
afterEach(cleanup); | ||
|
||
describe("DoubleInput", () => { | ||
describe("renders", () => { | ||
it("two inputs one for the header and one for the value", () => { | ||
const { getByPlaceholderText } = render(<HeaderInput />); | ||
expect(getByPlaceholderText("header")).toBeVisible(); | ||
expect(getByPlaceholderText("value")).toBeVisible(); | ||
}); | ||
|
||
it('two inputs are rendered with the given "header" and "value" data when given to the component', () => { | ||
const { getByPlaceholderText } = render(<HeaderInput data={{ id: 1, header: "Content-Type", value: "application/json" }} />); | ||
expect(getByPlaceholderText("header").value).toEqual("Content-Type"); | ||
expect(getByPlaceholderText("value").value).toEqual("application/json"); | ||
}); | ||
}); | ||
|
||
describe("props: events", () => { | ||
it('onBlur is called when both "header" and "value" have been entered', () => { | ||
const spy = jest.fn(); | ||
const { getByPlaceholderText } = render(<HeaderInput onBlur={spy} />); | ||
fireEvent.change(getByPlaceholderText("header"), { target: { value: "Content-Type" } }); | ||
fireEvent.change(getByPlaceholderText("value"), { target: { value: "application/json" } }); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('onBlur is not called when "header" value is set but "value" is missing', () => { | ||
const spy = jest.fn(); | ||
const { getByPlaceholderText } = render(<HeaderInput onBlur={spy} />); | ||
fireEvent.change(getByPlaceholderText("header"), { target: { value: "Content-Type" } }); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('onBlur is not called when "value" is set but the "header" value is not', () => { | ||
const spy = jest.fn(); | ||
const { getByPlaceholderText } = render(<HeaderInput onBlur={spy} />); | ||
fireEvent.change(getByPlaceholderText("value"), { target: { value: "application/json" } }); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("onRemove is called with the headers id when the user clicks on the remove icon", () => { | ||
const spy = jest.fn(); | ||
const data = { id: 1, header: "Content-Type", value: "application/json" }; | ||
const { getByLabelText } = render(<HeaderInput data={data} onRemove={spy} />); | ||
fireEvent.click(getByLabelText("remove-header")); | ||
expect(spy).toHaveBeenCalledWith(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.