Skip to content

Instantly share code, notes, and snippets.

import React, { createContext } from "react";
const AuthContext = createContext({});
const { Consumer, Provider } = AuthContext;
export class AuthContextProvider extends React.Component {
constructor(props) {
super(props);
@tfiechowski
tfiechowski / cluster-script.js
Created January 17, 2019 09:27
NodeJS script running in cluster
const { spawn } = require("child_process");
const fs = require("fs");
const cluster = require("cluster");
const numCPUs = require("os").cpus().length;
const scriptFile = './script.sh';
let tasks = [];
function runScript(arg) {
import { mount } from 'enzyme';
import { PhotosContextInner } from './PhotosContext';
describe('PhotosContext', () => {
test('fetchPhotos', async () => {
const api = {
fetchPhotos: () => ({ data: [{ caption: 'photo caption', src: 'src' }] }),
};
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as actions from '../../actions/TodoActions'
import * as types from '../../constants/ActionTypes'
import fetchMock from 'fetch-mock'
import expect from 'expect' // You can use any testing library
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
import api from './api';
export function addPhotos() { /* ... */ }
export function fetchPhotos() {
return async function(dispatch) {
const { data: photos } = await api.fetchPhotos();
dispatch(addPhotos(photos));
};
}
import 'regenerator-runtime/runtime';
import { PhotosProvider } from './contexts';
import Container from 'components/Container';
import ContextPhotoListContainer from './ContextPhotoListContainer';
import { render } from 'react-dom';
render(
<PhotosProvider>
import 'regenerator-runtime/runtime';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import Container from 'components/Container';
import ReduxPhotoListContainer from './ReduxPhotoListContainer';
import store from './store';
render(
import { combineReducers, createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import photos from './modules/Photos/reducer';
const rootReducer = combineReducers({ photos });
export default createStore(rootReducer, applyMiddleware(thunk));
import { PhotosConsumer } from './contexts';
import PhotoList from 'components/PhotoList';
export default function ContextPhotoListContainer() {
return (
<PhotosConsumer>
{({ photos, fetchPhotos }) => <PhotoList photos={photos} fetchPhotos={fetchPhotos} />}
</PhotosConsumer>
);
}
import { Component, createContext } from 'react';
import PropTypes from 'prop-types';
import api from './api';
const { Consumer, Provider } = createContext({
photos: [],
});
export { Consumer };