Skip to content

Instantly share code, notes, and snippets.

const path = require('path');
const glob = require('glob');
const express = require('express');
const Bottle = require('bottlejs');
const bottle = new Bottle();
// traverse the file system, passing this bottle to every javascript file we find
glob.sync(path.join(path.resolve(__dirname), 'app/**/*.js')).forEach((match) => {
require(match)(bottle);
const chai = require('chai');
chai.use(require('sinon-chai'));
const sinon = require('sinon');
describe('controller.Pdfs', () => {
var bottle, generate, res;
beforeEach(() => {
res = {
// Factory
function createPdfsController(PdfService) {
return {
create: function(req, res, next) {
const content_id = req.params.content_id;
const pdf_id = PdfService.generate(content_id);
res.json({ pdf_id: pdf_id });
}
};
}
function fetchSomething(thingId) {
return (dispatch) => {
// update call metadata
dispatch(startRequest(thingId))
return fetch(`https://api.example.com/things/${thingId}`)
.then((response) => response.json())
// send the result of the call, probably updating the request metadata as well
.then((json) => dispatch(receiveAThing(json)))
import { takeLatest } from 'redux-saga'
import { call, put } from 'redux-saga/effects'
function* fetchThing(action) {
try {
const thing = yield call(fetch, `https://api.example.com/things/${action.payload.thingId}`);
yield put({ type: "THING_RECEIVED", thing });
} catch(err) {
yield put({ type: "THING_GETTING_FAILED", message: err.message});
}
// Make sure not to bind this function before passing it as onUpdate
// to Router below since it will be called with this set to the Router
// instance.
function onLocationChange() {
doSomethingInterestingWithPath(this.state.location.pathname);
}
...
<Router routes={routes} onUpdate={onLocationChange} />
@emilong
emilong / conf.js
Created April 15, 2016 22:37
Simple protractor framework for running files by require'ing them
exports.config = {
framework: 'custom',
frameworkPath: './lib/protractor-require-framework.js',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['automation/some-protractor-script.js'],
};
/**
* Ruby has these nice case expressions, but switches are statements in JS.
* Here's a little convention to make switch statements look a little more like expressions.
*
* This uses ES6, but of course you could do it with ES5 as well. This pattern is especially
* nice in ES6, however, especially when you want to use const instead of let or var.
*/
const output = (() => {
switch(input) {
const assignLater = (a) => {
const x
if (a === 1) {
x = 1 // XXX nope, must assign at declaration time
} else {
x = 2 // XXX nope, must assign at declaration time
}
}