Skip to content

Instantly share code, notes, and snippets.

@axelnormand
axelnormand / select.js
Last active January 5, 2018 16:30
Select util function for redux - saves milliseconds in your typing :)
// @flow
export const select = (selectors: Object) => (state: Object): Object => {
const keys = Object.keys(selectors);
const ret = {};
keys.forEach(key => {
if (!selectors[key]){
throw new Error(
`
Could not find key '${key}' in selectors.
@axelnormand
axelnormand / mockChannel.ts
Created July 30, 2018 10:30
React Native Storybook
import Channel from '@storybook/channels';
/** create mock channel so don't need websocket in jest tests */
export const createMockChannel = () => {
const transport = {
setHandler: () => null,
send: () => null,
};
return new Channel({ transport });
@axelnormand
axelnormand / storybookStore.ts
Created July 30, 2018 10:32
React Native Storybook
import { action } from '@storybook/addon-actions';
import { Store } from 'redux';
import { getInitialState } from '../state';
/**
* Ready only mock store with addon actions upon dispatch
*
* Defaults to having a valid App initial state as imported via state.ts
*/
export const getStorybookStore = (): Store<any> => {
@axelnormand
axelnormand / testStory.tsx
Created July 30, 2018 10:32
React Native Storybook
import { storiesOf } from '@storybook/react-native';
import React from 'react';
import { Text, View } from 'react-native';
const TestComponent: React.SFC<{}> = () => <Text>Hello world!</Text>;
/**
* A test story for testTemplate to use
*
* Test Decorator should not be in snapshot be design
@axelnormand
axelnormand / initStorybookUI.ts
Created July 30, 2018 10:33
React Native Stroybook
import { configure, getStorybookUI } from '@storybook/react-native';
import React from 'react';
import { getConfig } from 'src/config';
import {
allStories,
loadStories,
} from 'src/test/storybook/storyshots/allStories';
/**
* init storybook UI on app startup and returns component ready, but dont display yet
@axelnormand
axelnormand / createTests.ts
Last active July 30, 2018 10:52
React Native Storyshot Generation
import chalk from 'chalk';
import * as fs from 'graceful-fs';
import * as path from 'path';
const SRC_DIR = path.join(__dirname, '../../../../src');
const ALL_STORIES_DIR = path.join(__dirname, 'allStories');
const ALL_STORIES_FILE = path.join(ALL_STORIES_DIR, 'index.ts'); // commit this file to git
const STORY_EXTENSION = '.story.tsx';
const STORY_TEST_OUTPUT_EXTENSION = '.story.test.ts';
@axelnormand
axelnormand / package.json
Last active July 30, 2018 11:01
React Native Storybook
{
"scripts": {
"prestorybookBuild": "tsc --lib ES7 --skipLibCheck --outDir ./src/test/storybook/storyshots ./src/test/storybook/storyshots/createTests.ts",
"prestorybook": "yarn prestorybookBuild && node src/test/storybook/storyshots/createTests.js",
"storybook": "storybook start -p 7007 -c \"src/test/storybook\"",
...
},
"dependencies": {
"@storybook/addon-actions": "^3.4.3",
"@storybook/addon-links": "^3.4.3",
@axelnormand
axelnormand / jest.setup.js
Created July 30, 2018 11:13
React Native Jest
// Setup for use with enzyme
// Patching console.error so test will fail
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
Enzyme.configure({ adapter: new Adapter() });
//fix redux saga test plan which needs setTimeout on window
global.window.setTimeout = global.setTimeout;
@axelnormand
axelnormand / login.js
Last active April 22, 2019 03:57
sagas
// @flow
import { put, call, takeLatest } from 'redux-saga/effects';
import { login as loginApi } from '../api/login';
import {
loginActionTypes,
loginLoading,
loginComplete,
loginInvalid
} from '../reducers/login/loginActions';
import type { UserLogin } from '../reducers/login/loginActions';
@axelnormand
axelnormand / machine.js
Last active October 7, 2020 14:01
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)