Last active
March 21, 2018 02:49
-
-
Save gabsprates/f10c28281edc500e54518890c0df09da to your computer and use it in GitHub Desktop.
POST14
This file contains hidden or 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
// enzyme.adapter.tsx | |
import { configure } from 'enzyme'; | |
import * as Adapter from 'enzyme-adapter-react-16'; | |
configure({ adapter: new Adapter() }); | |
// package.json | |
"jest": { | |
// ... | |
"setupFiles": [ "./enzyme.adapter.tsx" ] | |
} |
This file contains hidden or 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
import * as React from "react"; | |
import * as ReactDOM from "react-dom"; | |
import TodoApp from "./components/TodoApp"; | |
ReactDOM.render( | |
<TodoApp />, | |
document.getElementById("example") | |
); |
This file contains hidden or 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
import * as React from "react"; | |
import TodoList from "./TodoList"; | |
class TodoApp extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { items: [], text: '' }; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
} | |
render() { | |
return ( | |
<div> | |
<h3>TODO</h3> | |
<TodoList items={this.state.items} /> | |
<form onSubmit={this.handleSubmit}> | |
<input | |
onChange={this.handleChange} | |
value={this.state.text} | |
/> | |
<button> | |
Add #{this.state.items.length + 1} | |
</button> | |
</form> | |
</div> | |
); | |
} | |
handleChange(e) { | |
this.setState({ text: e.target.value }); | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
if (!this.state.text.length) { | |
return; | |
} | |
const newItem = { | |
text: this.state.text, | |
id: Date.now() | |
}; | |
this.setState(prevState => ({ | |
items: prevState.items.concat(newItem), | |
text: '' | |
})); | |
} | |
} | |
export default TodoApp; |
This file contains hidden or 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
import * as React from "react"; | |
class TodoList extends React.Component { | |
render() { | |
return ( | |
<ul> | |
{this.props.items.map(item => ( | |
<li key={item.id}>{item.text}</li> | |
))} | |
</ul> | |
); | |
} | |
} | |
export default TodoList; |
This file contains hidden or 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
// vamos alterar a chave "test" para: | |
"test": "./node_modules/.bin/jest", |
This file contains hidden or 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
class TodoApp extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { items: [], text: '' }; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
} | |
render() { | |
return ( | |
<div> | |
<h3>TODO</h3> | |
<TodoList items={this.state.items} /> | |
<form onSubmit={this.handleSubmit}> | |
<input | |
onChange={this.handleChange} | |
value={this.state.text} | |
/> | |
<button> | |
Add #{this.state.items.length + 1} | |
</button> | |
</form> | |
</div> | |
); | |
} | |
handleChange(e) { | |
this.setState({ text: e.target.value }); | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
if (!this.state.text.length) { | |
return; | |
} | |
const newItem = { | |
text: this.state.text, | |
id: Date.now() | |
}; | |
this.setState(prevState => ({ | |
items: prevState.items.concat(newItem), | |
text: '' | |
})); | |
} | |
} | |
class TodoList extends React.Component { | |
render() { | |
return ( | |
<ul> | |
{this.props.items.map(item => ( | |
<li key={item.id}>{item.text}</li> | |
))} | |
</ul> | |
); | |
} | |
} | |
ReactDOM.render(<TodoApp />, mountNode); |
This file contains hidden or 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
constructor(props: {}) { | |
// ... | |
this.handleReset = this.handleReset.bind(this); | |
} | |
render() { | |
// ... | |
<button type="button" onClick={this.handleReset}> | |
Reset list | |
</button> | |
</form> | |
// ... | |
} | |
handleReset() { | |
this.setState(TodoApp.defaultState); | |
} |
This file contains hidden or 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
import * as React from "react"; | |
import { render, shallow } from "enzyme"; | |
import TodoApp from "../../src/components/TodoApp"; | |
describe("Testando <TodoApp />", () => { | |
it("deve renderizar o componente", () => { | |
const wrapper = render(<TodoApp />); | |
expect(wrapper.length).toEqual(1); | |
}); | |
describe("tentando handlers", () => { | |
it("deve chamar handleChange e alterar state.text quando mudar o valor do input", () => { | |
const handle = jest.spyOn(TodoApp.prototype, "handleChange"); | |
const wrapper = shallow(<TodoApp />); | |
const input = wrapper.find("form input"); | |
expect(wrapper.state("text")).toBe(""); | |
input.simulate("change", { target: { value: "Assistir Star Wars" } }); | |
expect(handle).toHaveBeenCalled(); | |
expect(wrapper.state("text")).toBe("Assistir Star Wars"); | |
}); | |
it("deve chamar handleSubmit e atualzar state.items quando submeter o form", () => { | |
const handle = jest.spyOn(TodoApp.prototype, "handleSubmit"); | |
const wrapper = shallow(<TodoApp />); | |
const instance = wrapper.instance() as TodoApp; | |
const form = wrapper.find("form"); | |
instance.setState({ text: "Assistir Star Wars" }); | |
expect(wrapper.state("items").length).toEqual(0); | |
form.simulate("submit", { preventDefault: () => { } }); | |
expect(handle).toHaveBeenCalled(); | |
expect(wrapper.state("items").length).toEqual(1); | |
expect(wrapper.state("items")[0].text).toBe("Assistir Star Wars"); | |
}); | |
it("deve chamar handleReset e atualizar o state quando clicar no botão reset", () => { | |
const handle = jest.spyOn(TodoApp.prototype, "handleReset"); | |
const wrapper = shallow(<TodoApp />); | |
const instance = wrapper.instance() as TodoApp; | |
const button = wrapper.find("form button[type='button']"); | |
instance.setState({ text: "Assistir The Clone Wars" }); | |
instance.setState({ items: [{ id: 0, text: "Assistir Rogue One" }] }); | |
expect(wrapper.state("text")).toBe("Assistir The Clone Wars"); | |
expect(wrapper.state("items").length).toEqual(1); | |
button.simulate("click"); | |
expect(handle).toHaveBeenCalled(); | |
expect(wrapper.state("text")).toBe(""); | |
expect(wrapper.state("items").length).toEqual(0); | |
}); | |
}); | |
}); |
This file contains hidden or 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
import * as React from "react"; | |
import { render } from "enzyme"; | |
import TodoList, { ItemType } from "../../src/components/TodoList"; | |
const mock: Array<ItemType> = [ | |
{ id: 0, text: "Assistir Tarzan" }, | |
{ id: 1, text: "Assistir Toy Story" }, | |
{ id: 2, text: "Assistir O Rei Leão" }, | |
]; | |
describe("Testando <TodoList />", () => { | |
it("deve renderizar uma <ul> com 1 item", () => { | |
const movie = mock.slice(0, 1); | |
const wrapper = render(<TodoList items={movie} />); | |
const li = wrapper.find("ul > li"); | |
expect(li.length).toEqual(1); | |
expect(li.eq(0).text()).toBe(movie[0].text); | |
}); | |
it("deve renderizar uma <ul> com 3 itens", () => { | |
const wrapper = render(<TodoList items={mock} />); | |
const li = wrapper.find("ul > li"); | |
expect(li.length).toEqual(3); | |
mock.forEach((item, index) => { | |
expect(li.eq(index).text()).toBe(item.text); | |
}); | |
}); | |
}); |
This file contains hidden or 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
import * as React from "react"; | |
export type ItemType = { | |
id: number, | |
text: string | |
}; | |
type PropsType = { | |
items: Array<ItemType> | |
}; | |
const TodoList = (props: PropsType) => ( | |
<ul> | |
{props.items.map(item => ( | |
<li key={item.id}>{item.text}</li> | |
))} | |
</ul> | |
); | |
export default TodoList; |
This file contains hidden or 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
const TodoList = (props) => ( | |
<ul> | |
{props.items.map(item => ( | |
<li key={item.id}>{item.text}</li> | |
))} | |
</ul> | |
); |
This file contains hidden or 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
// @@ 1,10 @@ | |
import * as React from "react"; | |
import TodoList, { ItemType } from "./TodoList"; | |
type StateType = { | |
items: Array<ItemType>, | |
text: string | |
}; | |
class TodoApp extends React.Component<{}, StateType> { | |
constructor(props: {}) { |
This file contains hidden or 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
// @@ 9,20 @@ | |
class TodoApp extends React.Component<{}, StateType> { | |
static defaultState: StateType = { | |
items:[], | |
text: "" | |
}; | |
constructor(props: {}) { | |
super(props); | |
this.state = TodoApp.defaultState; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
} |
This file contains hidden or 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
// @@ 40,44 @@ | |
handleChange(e: React.ChangeEvent<HTMLInputElement>) { | |
this.setState({ text: e.target.value }); | |
} | |
handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment