Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evaldosantos/f20ee5725c460244c6f2f2f7ff7ef946 to your computer and use it in GitHub Desktop.
Save evaldosantos/f20ee5725c460244c6f2f2f7ff7ef946 to your computer and use it in GitHub Desktop.
import { increaseCount, reducer, store } from './counter';
describe('Counter', () => {
it('should have a default state', () => {
const result = reducer();
expect(result.count).toEqual(0);
});
it('should increase the count', () => {
const action = increaseCount();
const result = reducer(undefined, action);
expect(result.count).toEqual(1);
});
it('should increase the count with a custom count value', () => {
const action = increaseCount(5);
const result = reducer(undefined, action);
expect(result.count).toEqual(5);
})
});
describe('sheep count store', () => {
it('should return the state', () => {
const state = store.getState();
expect(state.count).toEqual(0);
});
it('should dispatch action and update the state', () => {
const action = increaseCount();
store.dispatch(action);
expect(store.getState().count).toEqual(1);
});
it('should call the subscribers when the store data changes', () => {
const listener = jest.fn();
store.subscribe(listener);
const action = increaseCount();
store.dispatch(action);
expect(listener).toHaveBeenCalled();
});
it('should call the subscribers when the store data changes', () => {
const listener = jest.fn();
const unsubscribe = store.subscribe(listener);
const action = increaseCount();
unsubscribe();
store.dispatch(action);
expect(listener).not.toHaveBeenCalled();
});
});
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { shallow } from 'enzyme';
describe('App', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(
<App />
);
});
it('should have the `th` "Items"', () => {
expect(
wrapper.contains(<th>Items</th>)
).toBe(true);
});
it('should have a `button` element', () => {
expect(
wrapper.containsMatchingElement(
<button>Add item</button>
)
).toBe(true);
});
it('`button` should be disabled', () => {
const button = wrapper.find('button').first();
expect(
button.props().disabled
).toBe(true);
});
it('should have a `input` element', () => {
expect(
wrapper.containsMatchingElement(
<input />
)
).toBe(true);
});
describe('the user populates the input', () => {
const item = 'Vancouver';
beforeEach(() => {
const input = wrapper.find('input').first();
input.simulate('change', {
target: { value: item}
});
});
it('should update the state property `item`', () => {
expect(
wrapper.state().item
).toEqual(item);
});
it('should enable `button`', () => {
const button = wrapper.find('button').first();
expect(
button.props().disabled
).toBe(false);
});
describe('and then clears the input', () => {
beforeEach(() => {
const input = wrapper.find('input').first();
input.simulate('change', {
target: { value: ''}
});
});
it('should disable `button`', () => {
const button = wrapper.find('button').first();
expect(
button.props().disabled
).toBe(true);
});
});
describe('and then submits the form', () => {
beforeEach(() => {
const form = wrapper.find('form').first();
form.simulate('submit', {
preventDefault: () => {},
})
});
it('should add the item to state', () => {
expect(
wrapper.state().items
).toContain(item);
});
it('should render the item in the table', () => {
expect(
wrapper.containsMatchingElement(
<td>{item}</td>
)
).toBe(true);
});
it('should clear the input field', () => {
const input = wrapper.find('input').first();
expect(
input.props().value
).toEqual('');
});
it('should disable `button`', () => {
const button = wrapper.find('button').first();
expect(
button.props().disabled
).toBe(true);
});
})
});
});
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
@evaldosantos
Copy link
Author

evaldosantos commented Jul 12, 2018

function createAsyncAction (type, fn) {
  return (...args) => (dispatch) => {
    dispatch({
      type: `${type}_STARTED`,
      payload: args
    });
    let result;
    try {
      result = await fn(...args);
    } catch (error) {
      dispatch({
        type: `${type}_FAILED`,
        error: true,
        payload: error
      });
      throw error;
    }
    dispatch({
      type: `${type}_ENDED`,
      payload: result
    });
    return result;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment