Skip to content

Instantly share code, notes, and snippets.

@ivawzh
ivawzh / asyncAwait.js
Last active January 30, 2016 06:24
async/await of ES7 101
// Conclusion:
// Each synchronous call blocks Node's event loop. All concurrent tasks are blocked, and the event loop sits idle, until the call completes.
// Another important note, async/await runs at 79% of the speed of bare callbacks.
// Full documentation: https://github.com/yortus/asyncawait
import { describe, it } from 'mocha';
import { expect } from 'chai';
describe('async/await', ()=> {
@ivawzh
ivawzh / promise-trigger.js
Last active April 26, 2016 14:20
when is a promise triggered?
// Promise is triggered by `()`, not `.then()`
describe('promise trigger', () => {
it('flat promise is triggered by `()` even without .then', done => {
let result = []
result.push(1)
function aFlatPromise(val) {
return new Promise(resolve => {
result.push(val)
resolve(val)
@ivawzh
ivawzh / generator function
Created May 9, 2016 10:13
basic generator function
it('generator function works', () => {
function* anotherGenerator(i) {
yield i + 1
yield i + 2
yield i + 3
}
function* generator(i){
yield i
yield* anotherGenerator(i)
@ivawzh
ivawzh / sinon-faker-server-ajax-delay-test.js
Created May 25, 2016 23:33
Neither sinon faker server respond nor ajax delays
describe('neither sinon faker server respond nor ajax delays', () => {
let server
beforeEach('set up fake server', () => {
server = sinon.fakeServer.create()
server.respondWith("GET", 'hey/you', [200, { 'Content-Type': 'application/json' }, JSON.stringify({})])
})
afterEach(() => server.restore())
context('when ajax is called', () => {
let result
@ivawzh
ivawzh / firebase-faker.js
Last active July 13, 2016 08:04
Firebase test helper as a set up util for npm lib 'firebase-server'.
import proxyquire from 'proxyquire'
import originalWebsocket from 'faye-websocket'
import FirebaseServer from 'firebase-server'
const localhostWS = {
'faye-websocket': {
'@global': true,
Client: url => {
const localhostUrl = url.replace(/dummy\d+\.firebaseio\.test/i, 'localhost')
return new originalWebsocket.Client(localhostUrl)
@ivawzh
ivawzh / firebase-faker.spec.js
Last active July 12, 2016 16:40
Spec for firebase-faker test helper
import { expect } from 'chai'
import { setupDatabase, destroyDatabase } from './firebase-faker'
describe('Firebase fake server', () => {
let server, data, client
beforeEach(() => {
const port = 45001
data = {
states: {
CA: 'California',
@ivawzh
ivawzh / redux-middleware-block-reducer.js
Last active August 8, 2016 14:27
Redux middleware blocks Saga actions' way to reducer
describe.only('middleware 101', () => {
let store, stateHistory, dispatched
beforeEach(()=>{
const defaultState = { text: 'initial state' }
function reducer(state = defaultState, action) {
switch (action.type) {
case 'SIDE_EFFECT_USER_FETCH':
return { text: 'THIS HAS FAILED TO BE ESCAPED!!!!'}
case 'say':
@ivawzh
ivawzh / redux-backend.js
Last active August 12, 2016 10:50
Immutable unidirectional back-end architechure
// coordinator => saga => action => reducer => coordinator => ...
describe.only('Unidirectional back-end architechure', () => {
let store, stateHistory, dispatched
beforeEach(()=>{
const defaultState = { text: 'initial state' }
function reducer(state = defaultState, action) {
switch (action.type) {
case 'UPDATE_NUMBER':
@ivawzh
ivawzh / jest-test.jest.js
Last active October 13, 2016 06:11
Jest test 101
// @ package.json
{
"scripts": {
"jest": "NODE_ENV=test DB_USER=ivan-wang ./node_modules/jest/bin/jest.js",
"jest-watch": "NODE_ENV=test DB_USER=ivan-wang ./node_modules/jest/bin/jest.js --watch --onlyChanged"
},
"jest": {
"testEnvironment": "node",
"automock": true,
"rootDir": "",
@ivawzh
ivawzh / babel-and-jest-setup.md
Last active October 25, 2016 07:47
Babel & Jest setup 2016/10/21

Babel setup with Jest test

@ package.json

"devDependencies": {
    "babel-cli": "^6.16.0",                        // for production build
    "babel-jest": "^16.0.0",                       // for Jest test with ES6
    "babel-plugin-transform-runtime": "^6.15.0",   // for Babel Register. Because babel only handle syntax translation, but not provide                                                    // missing APIs from old javascript like `Array.includes`.
                                                   // `Promise`, `Set`, `Map`, or instance methods like `String.repeat` or