Skip to content

Instantly share code, notes, and snippets.

View whtswrng's full-sized avatar

Tomáš Gold whtswrng

  • Brno
View GitHub Profile
@whtswrng
whtswrng / .js
Last active November 27, 2018 18:23
react-testing-1
describe("when input changed", () => {
it("should change property name in the state", () => {
const componentWrapper = shallow(<Login />);
componentWrapper.find('#email').simulate('change', {target: {name: 'email', value: 'blah@gmail.com'}});
expect(componentWrapper.state('email')).toEqual('blah@gmail.com');
})
});
@whtswrng
whtswrng / react-testing-2.js
Last active October 21, 2018 10:41
react-testing-2
describe('when valid email was filled', () => {
beforeEach(() => {
subscribe = jest.fn();
wrapper = mount(<Subscription subscribe={subscribe}/>);
});
describe('and when form was submitted', () => {
beforeEach(() => {
...
wrapper.find('#email').simulate('change', {target: {name: 'email', value: 'blah@gmail.com'}});
@whtswrng
whtswrng / react-testing-3.js
Last active October 15, 2018 20:02
react-testing-3.js
export class Subscription extends Component {
state = {
email: ''
};
render() {
return (
<input
id="email"
type="email"
@whtswrng
whtswrng / react-testing-4.js
Last active October 20, 2018 20:21
react-testing-4.js
export class Subscription extends Component {
state = {
email: ''
};
render() {
return (
...
<MarketingContent logo={logo}/>
<EmailInput onChange={(email) => this.onEmailChange(email)}/>
@whtswrng
whtswrng / react-testing-5.js
Created September 25, 2018 08:23
react-testing-5.js
it('should render marketing content with correct logo', () => {
expect(wrapper.find('MarketingContent').length).toBe(1);
expect(wrapper.find('MarketingContent').props("logo")).toEqual(logo);
});
it('should render email input and pass it onChange method', () => {
expect(wrapper.find('EmailInput').length).toBe(1);
expect(wrapper.find('EmailInput').props("onChange")).toEqual(this.onChange);
});
@whtswrng
whtswrng / solid-1.jsx
Created October 6, 2018 08:24
solid-1-bad
class App extends Component {
state = {
users: []
};
async fetchUsers() {
const users = await fetch('http://totallyhardcodedurl.com/stupid');
this.setState({users});
}
class App extends Component {
state = {
users: [{name: 'Jim', surname: 'Smith', age: 33}]
};
componentDidMount() {
this.fetchUsers();
}
class App extends Component {
render() {
return (
<div className="App">
<Header/>
<UserList/>
</div>
);
}
class UserList extends Component {
static propTypes = {
fetchUsers: PropTypes.func.isRequired,
saveUsers: PropTypes.func.isRequired
};
state = {
users: [{name: 'Jim', surname: 'Smith', age: 33}]
};
class App extends Component {
...
async fetchUsers() {
const users = await fetch('http://totallyhardcodedurl.com/stupid');
this.setState({users});
}
...