Skip to content

Instantly share code, notes, and snippets.

View paulsturgess's full-sized avatar

Paul Sturgess paulsturgess

View GitHub Profile
@paulsturgess
paulsturgess / service_spec.js
Created February 8, 2017 20:51
Test Axios promise with jasmine-ajax
import Service from 'path/to/service';
import 'jasmine-ajax'
describe('Service', () => {
let request, promise;
let instance = Service;
let payload = {foo:'bar'};
let path = '/path';
let callback = jasmine.createSpy('callback');
@paulsturgess
paulsturgess / actions_spec.js
Last active November 20, 2017 22:30
Test a redux-thunk action
describe('saveResource', () => {
let resource = { id: 5, name: 'foo' };
describe('when the response is a 200', () => {
let dispatch = jasmine.createSpy('dispatch');
beforeEach(() => {
spyOn(Service, 'post').and.callFake(function(url, payload, callback) {
callback(200, {foo: 'bar'})
})
});
@paulsturgess
paulsturgess / actions.js
Last active February 8, 2017 20:40
Example redux actions using redux-thunk middleware
import Constants from '../path/to/constants';
import Service from '../path/to/service';
const Actions = {
toggleSomething: (id) => ({
type: Constants.TOGGLE_SOMETHING,
id
}),
resourceSuccessfullySaved: (data) => ({
type: Constants.RESOURCE_SUCCESSFULLY_SAVED,
@paulsturgess
paulsturgess / componentContainer.js
Created February 8, 2017 20:09
An example mapDispatchToProps when using redux-thunk middleware
const mapDispatchToProps = (dispatch, ownProps) => ({
saveResource: (resource, event) => {
Actions.saveResource(resource)(dispatch);
},
toggleSomething: (id, event) => {
dispatch(Actions.toggleSomething(id));
}
});
@paulsturgess
paulsturgess / service.js
Last active August 8, 2024 04:25
An example Service class wrapper for Axios
import axios from 'axios';
class Service {
constructor() {
let service = axios.create({
headers: {csrf: 'token'}
});
service.interceptors.response.use(this.handleSuccess, this.handleError);
this.service = service;
}
yum install -y libpng
yum install -y libjpeg
yum install -y openssl
yum install -y icu
yum install -y libX11
yum install -y libXext
yum install -y libXrender
yum install -y xorg-x11-fonts-Type1
yum install -y xorg-x11-fonts-75dpi
@paulsturgess
paulsturgess / test-redux-container-component.js
Last active April 6, 2017 20:03
Test redux container component
import React from 'react';
import TestUtils from 'react/lib/ReactTestUtils';
import actions from '../actions'
import ConnectedAddTodo from './AddTodo.js'
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
const store = configureMockStore()({});
describe('AddTodo', () => {
let instance, container;
@paulsturgess
paulsturgess / redux-container-component.js
Created November 6, 2016 22:13
Redux Container Component
import { connect } from 'react-redux';
import AddTodoForm from '../components/AddTodoForm'
import actions from '../actions';
// mapDispatchToProps receives the dispatch() method and returns
// callback props that can be used to inject into the presentational component
// This means addTodo can be set as a prop when testing AddTodoForm which makes
// it easy to spy and check the correct actions are triggered
export const mapDispatchToProps = (dispatch) => ({
addTodo: (value) => dispatch(actions.addTodo(value))
@paulsturgess
paulsturgess / Todo.test.js
Last active October 8, 2018 11:22
Testing a stateless component
import React from 'react';
import TestUtils from 'react/lib/ReactTestUtils';
import Todo from './Todo';
describe('Todo', () => {
let instance, li;
let Wrapper = React.createClass({
render: function() {
return this.props.children;
}
@paulsturgess
paulsturgess / Todo.js
Last active June 17, 2017 11:46
Example stateless React Component
import React, { PropTypes } from 'react'
const Todo = ({ onClick, completed, text }) => (
<li
onClick={onClick}
style={{
textDecoration: completed ? 'line-through' : 'none'
}}
>
{text}