Skip to content

Instantly share code, notes, and snippets.

@seamusleahy
seamusleahy / Jest Enzym Error
Created June 28, 2017 15:04
How to fix the Enzym warning when mounting to component to the body
$ npm test
> example@1.0.0 test /home/user/project
> jest
PASS src/__tests__/WithErrorExample.test.jsx
Example
✓ Test (108ms)
Test Suites: 1 passed, 1 total
@seamusleahy
seamusleahy / test.js
Created June 27, 2017 03:27
How to test the content inside of a react-bootstrap <Modal>
import { mount, ReactWrapper } from 'enzyme';
import { Modal } from 'react-bootstrap';
// The Enzyme selector query will not work because the actual inside HTML of the modal lives else where in the DOM
// via portals. See: https://react-bootstrap.github.io/react-overlays/#portals
function getWrapperForModalPortal(parentWrapper) {
// The Modal instance provides a reference to HTML content:
// [1] https://github.com/react-bootstrap/react-bootstrap/blob/dddb9b966c7f3e79495be5c5730f7cce04813aaf/src/Modal.js#L228
// [2] https://github.com/react-bootstrap/react-bootstrap/blob/419051127d913bbb149e81ed221500108ba0d7f3/test/ModalSpec.js#L32
const modalInstance = parentWrapper.find(Modal).getNode()._modal.getDialogElement();
@seamusleahy
seamusleahy / fetch-response.js
Last active May 4, 2019 19:17
Using the Fetch HTML5 API to POST a form and get back JSON data.
// 3. Use the response
// ================================
responsePromise
// 3.1 Convert the response into JSON-JS object.
.then(function(response) {
return response.json();
})
// 3.2 Do something with the JSON data
.then(function(jsonData) {
console.log(jsonData);
@seamusleahy
seamusleahy / Angular.$q.all().js
Last active September 2, 2016 03:48
How to deal with multiple promises with native code and several popular libraries. https://seamusleahy.com/how-to-resolve-multiple-promises/
module.controller('MyCtrl', function($scope, $q) {
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all the them using $q.all which follows Promise.all of taking an array of promises
const consolidatedPromise = $q.all([promise1, promise2, promise3]);
consolidatedPromise.then(function (promiseValues) {
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
<LocalizationProvider messages={myLocalizationMessages}>
<Foo>
<h1><LocalizationText key="welcomeMessage" /></h1>
</Foo>
</LocalizationProvider>
@seamusleahy
seamusleahy / twig-indentless.php
Last active April 16, 2018 14:05
Twig tag to remove indentation at the start of each line: {% indentless %}...{%endindetless %}
<?php
/**
* Remove whitespace at the start of each line.
*
* Based on Twig's Twig_TokenParser_Spaceless class
*/
class TwigIndentlessTokenParser extends Twig_TokenParser
{
/**
@seamusleahy
seamusleahy / gist:5621434
Created May 21, 2013 17:06
Remove ability to create new posts of a custom post type in WordPress.
<?php
register_post_type( 'custom_post_type_name', array(
'capabilities' => array(
'create_posts' => false,
)
));
@seamusleahy
seamusleahy / gist:5388747
Last active December 16, 2015 05:59
Open external links in a new window using jQuery.
// Open external links in new windows
jQuery(document).ready(function($) {
var re = new RegExp( '^([^/]+:)?//([^/]+\\.)?' + window.location.host.replace(/\./g, '\\.') + '/');
$('body').on('click', 'a', function(event) {
if(!re.test(this.href)) {
window.open(this.href, '_blank');
return false;
}
});
});
@seamusleahy
seamusleahy / gist:4956037
Last active December 13, 2015 18:29
A function to retrieve the bare structure of a WordPress menu.
<?php
/**
* Retrieve the bare structure of a nav menu instead of the formatted output.
*
* @param unknown $args - the same arguments you pass into wp_nav_menu (the fallback and output arguments do not apply)
*/
function prefix_get_nav_menu( $args=array() ) {
$args = wp_parse_args( $args, array() );
$args = apply_filters( 'wp_nav_menu_args', $args );
$args = (object) $args;
@seamusleahy
seamusleahy / gist:4758617
Created February 11, 2013 23:29
Send additional header info for inserted media.
<?php
function send_extra_media_info( $html, $send_id, $attachment ) {
@header( "X-Attachment-ID: " . intval( $send_id ) );
foreach( array('post_title', 'image-size' ) as $key ) {
if( array_key_exists($key, $attachment) ) {
@header( "X-".$key.": " . str_replace( array( "\r", "\n"), ' ', $attachment[$key] ) );
}
}