Skip to content

Instantly share code, notes, and snippets.

import react from 'react';
class Person extends React.Component {
constructor(props) {
super(props);
this.state = {
name: ''
};
//there is no need to bind event handlers here :)
}
import react from 'react';
class Person extends React.Component {
constructor(props) {
super(props);
this.state = {
name: ''
};
this.handleChange = this.handleChange.bind(this);
}
@SherryH
SherryH / jestMock1.js
Last active July 25, 2018 19:07
A Jest Mock Example
import React from 'react';
import renderer from 'react-test-renderer';
//children mock need to be defined before parent module import
jest.mock('react-bootstrap-table', () => {
return {
BootstrapTable: 'BootstrapTable',
TableHeaderColumn: 'TableHeaderColumn',
};
});
var player = function (type) {
return function(song){
console.log('I can play '+song+' on '+ type);
}
}
var guitarPlayer = player('guitar');
guitarPlayer('Country Road');
var pianoPlayer = player('piano');
@SherryH
SherryH / subclass.js
Last active May 6, 2018 10:21
Functional and Pseudoclassical subclasses
//==================================
// Functional classes
// Define methods inside the object constructing function
var makeAnimal = function(name){
var obj = {};
obj.name = name;
var privateVar = ' you can see me';
obj.speak = function () {
@SherryH
SherryH / classes.js
Last active January 4, 2017 03:31
Functional and Pseudoclassical classes
//==================================
// Functional classes
// Define methods inside the object constructing function
var makeAnimal = function(name){
var obj = {};
obj.name = name;
var privateVar = 'you can see me';
obj.speak = function () {
@SherryH
SherryH / example.js
Created December 31, 2016 04:24
Make a Spy
// a highly simplified version of a spy which counts total call
var spy = function(object, property){
var origFn = object[property];
var spyFn = function(){
spyFn.callCount++;
return origin.apply(object, arguments);
}
spyFn.callCount = 0;
object[property] = spyFn;