Skip to content

Instantly share code, notes, and snippets.

View sirbrillig's full-sized avatar

Payton Swick sirbrillig

View GitHub Profile
@sirbrillig
sirbrillig / get-from-dom.js
Created February 29, 2016 23:20
window object dependency injection
var getWindow = require( './window' ).getWindow;
function getSomethingFromTheDOM() {
return getWindow().document.querySelector( '.something' );
}
@sirbrillig
sirbrillig / jsdom-react-iframe-test.js
Created March 16, 2016 00:02
This file should run, but it throws an error
// Set up DOM
var jsdom = require( 'jsdom' ).jsdom;
global.document = jsdom( '' );
global.window = document.defaultView;
global.navigator = document.defaultView.navigator;
// Prepare React
var ReactTestUtils = require( 'react-addons-test-utils' );
var React = require( 'react' );
@sirbrillig
sirbrillig / SpiesExample.php
Last active March 26, 2016 21:35
An example of using Spies
<?php
class GreetingGenerator {
public function get_greeting( $name ) {
// Assumes determine_greeting is defined in another file
return determine_greeting( $name );
}
}
class Greeter {
@sirbrillig
sirbrillig / MyTimeTest.php
Last active July 30, 2016 14:15
Convert a string timezone offset to a formatted string timezone offset
<?php
use PHPUnit\Framework\TestCase;
class MyTime {
/**
* Convert a float string timezone offset to a DateTimeZone
*
* @param string $number The initial offset float, as a string.
* @return DateTimeZone The converted DateTimeZone
*/
@sirbrillig
sirbrillig / add-methods-to-object.js
Last active August 2, 2016 15:47
Two different ways to add a method to an object without mutating the object prototype
"use strict"
class OriginalObj {
sayHello() {
console.log( 'hello' );
}
}
const myOriginal = new OriginalObj();
myOriginal.sayHello();
@sirbrillig
sirbrillig / plug.js
Last active October 20, 2016 17:41
Babel plugin to convert simple React components to PHP
module.exports = function( babel ) {
const t = babel.types;
let insertedPhp = false;
const updateParamNameVisitor = {
Identifier( path ) {
if ( path.node.name.startsWith( '$' ) ) {
return;
}
// Transform VariableDeclarator Identifiers to PHP style
window.registerBlock('My Block', function() { return '<p>this is my block</p>' })
@sirbrillig
sirbrillig / renderer-test.js
Last active December 23, 2016 20:43
A naive `renderToString()` that mimics `React.renderToStaticMarkup()`
/* globals describe, it */
import React from 'react';
import { expect } from 'chai';
// Using React.createElement works too!
import { renderToString, createElement } from './renderer';
describe( 'renderToString()', function() {
it( 'returns a string with a tag wrapping plain text for a component with one text child', function() {
@sirbrillig
sirbrillig / simple-functional-component.js
Created January 2, 2017 18:27
An example of a simple stateless functional React component
import React from 'react';
export function Header( { title, subtitle, className } ) {
return (
<div className={ className }>
<h1>{ title }</h1>
<span>{ subtitle }</span>
</div>
);
}
@sirbrillig
sirbrillig / bootstrap.php
Created January 3, 2017 18:44
Example phpunit test bootstrap file for using Patchwork
<?php
$autoload = 'vendor/autoload.php';
$patchwork = 'vendor/antecedent/patchwork/Patchwork.php';
# require patchwork first
if ( file_exists( $patchwork ) ) {
require_once $patchwork;
}
if ( file_exists( $autoload ) ) {