Skip to content

Instantly share code, notes, and snippets.

@mars
mars / rewire-module.js
Last active September 15, 2016 08:51
Manually mock React components; Jasmine helper to stub & teardown a bunch of a module's dependencies with Rewire. How-to: http://substantial.com/blog/2014/11/11/test-driven-react-how-to-manually-mock-components/
/*
Jasmine helper to stub & teardown a bunch of a module's dependencies.
describe("SomeModule", function() {
var SomeModule = rewire("some-module");
rewireModule(SomeComponent, {
ItsComponent: React.createFactory('div'),
AnotherComponent: React.createFactory('div')
@mars
mars / heroku-buildpack-tips.markdown
Created August 9, 2016 05:42
Tips for authoring Heroku buildpacks
@mars
mars / json_api_base_serializer.rb
Last active April 8, 2016 19:31
ActiveModel Serializers for JSON-API (with association sideloading)
# Ancestor for JSON-API serializers, implements
# "application/vnd.api+json" media type conventions.
#
# See: http://jsonapi.org
#
class JsonApiSerializer < ActiveModel::Serializer
# Always make pluralized root JSON property.
#
def self.inherited(subclass)
#!/usr/bin/env bash
# Fail immediately on non-zero exit code.
set -e
# Fail immediately on non-zero exit code within a pipeline.
set -o pipefail
# Fail on undeclared variables.
set -u
# Debug, echo every command
#set -x
@mars
mars / foo-app-test.js
Last active December 20, 2015 06:29
ember-testing: multiple app compatibility
// give each tested app its own DOM element
document.write('<div class="ember-testing-container"><div id="foo-app-testing"></div></div>');
FooApp.rootElement = '#foo-app-testing';
// defer readiness
FooApp.setupForTesting();
module("Foo App", {
setup: function() {
FooApp.injectTestHelpers();
// last step, before each test begins, clears the container and advances readiness
@mars
mars / gist:6086194
Last active December 20, 2015 06:29
DEPRECATED see comments: ember-testing: select an option (helper)
Ember.Test.registerHelper('selectFrom',
function(app, selector, value, description) {
// choose an option
find(selector).val(value);
// trigger the change
find(selector).change();
// assert the selected option
equal(find(selector+" option:selected").val(), value, description||"makes the selection");
// promise
return wait();
@mars
mars / ember-data-global-ajax-params.js
Last active December 16, 2015 06:09
Add params to Ember Data's RESTAdapter AJAX requests.
App.accountId = 'asdf';
App.RESTAdapter = DS.RESTAdapter.extend({
// Scope all ajax calls.
ajax: function(url, type, hash) {
if (Ember.isEmpty(hash)) hash = {};
if (Ember.isEmpty(hash.data)) hash.data = {};
hash.data.account_id = App.accountId;
this._super(url, type, hash);
}
@mars
mars / bulk_email.rb
Created July 25, 2012 20:07
Bulk email via gmail
require "rubygems"
# gem install ruby-gmail
require 'gmail'
# gather email list
addresses = %w{
email1@domain.com
email2@domain.com
@mars
mars / wrap-promise.js
Last active August 29, 2015 14:18
wrap a Promise with Ruby-esque block behavior; ES6/ES2015
/*
Insert a Promise into a pre-defined Promise chain.
@param promiseBlock [Function] takes a Promise
parameter & returns the continuation of that parameter.
@return [Promise] the fullfilled Promise chain
*/
function wrapPromise(promiseBlock) {
if (typeof promiseBlock !== 'function') {
throw new Error('promiseBlock must be a Function');
@mars
mars / karma-ci.config.js
Last active August 29, 2015 14:13
example Webpack configs: React runtime & Karma tests
var webpackModule = require('./webpack.module.js');
var webpackResolve = require('./webpack.resolve.js');
var Webpack = require('webpack');
var RewirePlugin = require("rewire-webpack");
module.exports = function(config) {
config.set({
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter