Skip to content

Instantly share code, notes, and snippets.

View theKashey's full-sized avatar

Anton Korzunov theKashey

View GitHub Profile
@theKashey
theKashey / Title.js
Created December 26, 2018 05:56
Set document title from react node
class Title extends React.PureComponent {
domNode = document.createElement('div');
// ^ it's not added to the document
componentDidMount() {
this.componentDidUpdate();
}
componentDidUpdate() {
document.title = this.domNode.textContent || ':(';
@theKashey
theKashey / .babelrc
Last active August 1, 2020 09:46
storybook rewiremock
...
"plugins": [
"rewiremock/babel", // babel plugin is required to use "Jest-style" mocking
]
...
@theKashey
theKashey / storybook-actions.js
Created November 3, 2018 06:54
storybook-remock
import { remock, createElement } from 'react-remock';
import { action } from '@storybook/addon-actions';
remock(
'button', // for all "buttons"
(type, props, children) => (
createElement(type, {
...props,
// override onclick handler
onClick: e => { action(e); props.onClick && props.onClick(e); }
@theKashey
theKashey / webpack-conf.js
Created November 3, 2018 06:46
storybook-webpack
{
// ...
resolve: {
alias: {
// just replace any "API" you may use, by a "storybook-friendly" one
'/src/api': '/storybook/api',
'react-redux': ? // probably? mock it out!
}
}
const state = faste()
.withPhases(['red', 'yellow', 'green'])
.withMessages(['tick','next'])
.on('tick',['green'], ({transit}) => transit('yellow'))
.on('tick',['yellow'], ({transit}) => transit('red'))
.on('tick',['red'], ({transit}) => transit('green'))
// just rethrow next as tick
.on('next',[], ({trigger}) => trigger('tick'))
@theKashey
theKashey / node.js
Created October 19, 2018 02:14
injecting-mock-window
// this package empowers babel-register
const addHook = require('pirates').addHook;
function matcher(filename) {
// do not apply to mocks
return fileName.indexOf('src/mocks') < 0;
}
const PREPEND_TEXT = `
const window = require('src/mocks/window.js');
import window from 'current-window';
window.alert('Not alert you are looking for');
import rewiremock from 'rewiremock';
// somewhere in "setup"
rewiremock('reselect').callThrough(); // mock `reselect` but use original file
// "mocking cycle"
rewiremock.enable(); // rewiremock will wipe all required on `enable`
const App = require('App');
rewiremock.disable();// and clean on disable
const wipe = require('wipe-node-cache') or require('wipe-webpack-cache');
const resolver = (stubs, fileName, module) {
// any logic about should one with some file, or not
return !!stubs[fileName];
};
// wipe reselect, and everything depends on it, and everything up to the top
wipe({
'reselect/lib/index.js': true,
// we will "cache" hello using the current language
// of course it will never change :P
let helloMessage = '';
export const sayHello = (inLng) => {
if (!helloMessage) {
helloMessage = require('hello-message-generator')(inLng);
}
return helloMessage
}