Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Sturgess paulsturgess

View GitHub Profile
@paulsturgess
paulsturgess / service_spec.js
Created Feb 8, 2017
Test Axios promise with jasmine-ajax
View service_spec.js
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 Nov 20, 2017
Test a redux-thunk action
View actions_spec.js
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 Feb 8, 2017
Example redux actions using redux-thunk middleware
View actions.js
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 Feb 8, 2017
An example mapDispatchToProps when using redux-thunk middleware
View componentContainer.js
const mapDispatchToProps = (dispatch, ownProps) => ({
saveResource: (resource, event) => {
Actions.saveResource(resource)(dispatch);
},
toggleSomething: (id, event) => {
dispatch(Actions.toggleSomething(id));
}
});
@paulsturgess
paulsturgess / service.js
Last active Dec 14, 2021
An example Service class wrapper for Axios
View service.js
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;
}
View Install wkhtmltopdf on CentOS7
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 Apr 6, 2017
Test redux container component
View test-redux-container-component.js
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;
View redux-container-component.js
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 Oct 8, 2018
Testing a stateless component
View Todo.test.js
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 Jun 17, 2017
Example stateless React Component
View Todo.js
import React, { PropTypes } from 'react'
const Todo = ({ onClick, completed, text }) => (
<li
onClick={onClick}
style={{
textDecoration: completed ? 'line-through' : 'none'
}}
>
{text}