Skip to content

Instantly share code, notes, and snippets.

View nackjicholson's full-sized avatar

Will Vaughn nackjicholson

View GitHub Profile
@nackjicholson
nackjicholson / gist:67cdaff4ae83c7d54bab
Last active August 29, 2015 14:16
Forward a most stream
'use strict';
var most = require('most');
var Q = require('q');
var forwardFromPromisedStream = function(promise) {
return most.create(function(add, end, error) {
promise.then(function(stream) {
stream.observe(add).then(end, error);
});
@nackjicholson
nackjicholson / abusiveTest.js
Last active August 29, 2015 14:19
Javascript Unit vs Behavior Testing blog gist.
'use strict';
var proxyquire = require('proxyquire')
var should = require('should')
var sinon = require('sinon')
describe('ec2/doesAutoScalingGroupExist', function () {
var _
var constantFunc;
var ec2
@nackjicholson
nackjicholson / injection-spec.js
Created April 26, 2015 02:27
Better tests, proxyquire vs injection vs mockless
var assert = require('assert')
var sinon = require('sinon')
var dependency = require('dependency')
var sut = require('./injection-sut')
describe('sut', function () {
var methodStub
beforeEach(function () {
methodStub = sinon.stub(dependency, 'method')
@nackjicholson
nackjicholson / serviceLookup.js
Last active August 29, 2015 14:20
Node.js service lookup with consul
import got from 'got-promise';
import sample from 'lodash/collection/sample';
function serviceLookup(serviceName, routePath) {
let serviceHealthUrl = `http://your.consul.host.com/v1/health/service/${serviceName}?passing`; // jshint ignore:line
function composeServiceUrl(response) {
let service = sample(response.body).Service;
return `http://${service.Address}:${service.Port}${routePath}}}`;
}
@nackjicholson
nackjicholson / "npm run dev" with wr instead of watch.
Last active December 11, 2015 01:18
Using the `wr` is a lot faster than using `watch` due to this issue: https://github.com/mikeal/watch/issues/46
{
"scripts": {
"lint": "eslint --ignore-pattern=src/public/js/dist/*.js src && jscs src",
"test": "mocha --compilers js:babel-register src/**/*.test.js",
"dev": "wr --exec 'clear && npm run -s lint && npm run -s test' src"
}
}
@nackjicholson
nackjicholson / ClientAppSelect.js
Last active December 18, 2015 15:59
React Tests Dream Team - Cheerio and Teaspoon
import React from 'react';
import history from '../history';
function ClientAppSelect({ currentPathname }) {
function handleClientSelection(event) {
history.replaceState(null, event.target.value);
}
return (
<div>
@nackjicholson
nackjicholson / statelessRadio.js
Last active January 14, 2016 19:13
TDD and React Components
function statelessRadio(React) {
function StatelessRadio(props) {
const {
baseId,
defaultValue,
inputs = [],
titleText,
onSelection = () => {}
} = props;
describe('StatelessRadio', () => {
it('should return a function');
it('should render a wrapper div with id equal to baseId prop');
it('should render a p tag with the text from the titleText prop');
it('should render radio inputs and their labels from the inputs prop');
it('should check a default input from the defaultValue prop');
it('should take an onSelection callback prop');
});
<div id="favorite-color">
<p id="favorite-color__title">Favorite Color</p>
<input id="favorite-color-input1" name="favorite-color" type="radio" value="blue" />
<label for="favorite-color-input1">Blue</label>
<input id="favorite-color-input2" name="favorite-color" type="radio" value="red" />
<label for="favorite-color-input2">Red</label>
</div>
import assert from 'assert';
import React from 'react';
import statelessRadio from './statelessRadio';
// ...
it('should return a function', () => {
// "Arrange" Phase: where you do any setup or building of objects your test needs. This test is so simple it doesn't include this phase.
// "Act" Phase: Perform whatever action your test tests. In this case we call our module.