Skip to content

Instantly share code, notes, and snippets.

View agm1984's full-sized avatar

Adam Mackintosh agm1984

View GitHub Profile
@agm1984
agm1984 / reactScrollPositionY.js
Created February 23, 2018 06:45
How to get Y scroll position in React
import React, { Component } from 'react'
import UserDetails from './UserDetails'
/**
* This utility function allows function calls to be debounced.
* @param {Function} func Function that requires debouncing
* @param {Number} wait Wait time in milliseconds between successive invocations
*/
const debounce = (func, wait) => {
let timeout
@agm1984
agm1984 / reduce.js
Created July 9, 2019 22:27
How Array.prototype.reduce works
const poop = ['one', 'two', 'three', 'four', 'five']
// I always use `acc` and `item` by default
// acc is the thing being accumulated
// item is each item in `poop`
// basic idea: this will do nothing,
// add some console.logs in here and study the behaviour
// look at the example below that uses `i` and add it here with console.log
const ultraBasic = poop.reduce((acc, item) => {
@agm1984
agm1984 / case-insensitive-search
Created August 8, 2019 21:35
Maybe you have an autocomplete field and you want to filter a list to case insensitive matches.
@agm1984
agm1984 / vue-sfc-snippet.json
Last active April 30, 2020 20:16
VS Code snippet for a Vue component scaffold
{
"Vue component": {
"prefix": "vue",
"body": [
"<template>",
" <div></div>",
"</template>",
"",
"<script>",
"export default {",
@agm1984
agm1984 / phpunit.xml
Last active July 10, 2020 01:42
phpunit.xml for Laravel unit testing with database transactions. DB_CONNECTION and DB_DATABASE are omitted which implicitly instructs Laravel to use the database information in the currently-loaded .env file (note: we are testing against the current environment and the <php> section in this file overwrites specific env variables)
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
@agm1984
agm1984 / OAuthGitHubTest.php
Created July 10, 2020 01:48
This is not a full set of tests, but it should be enough to see one way to use Mockery. See this repo, where I originally found this code, for more information: https://github.com/cretueusebiu/laravel-vue-spa/blob/master/app/OAuthProvider.php
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\OauthProvider;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use Laravel\Socialite\Facades\Socialite;
@agm1984
agm1984 / TestCase.php
Created July 10, 2020 02:35
Example of how to use the `DatabaseTransactions` Trait in Laravel.
<?php
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseTransactions;
@agm1984
agm1984 / TestCase.php
Created July 10, 2020 02:51
Demonstrates how to use a "select or create" pattern for Laravel unit testing via `$this->actingAs($user);`
<?php
namespace Tests;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Spatie\Permission\Models\Role;
abstract class TestCase extends BaseTestCase
@agm1984
agm1984 / TestCase.php
Last active July 10, 2020 02:57
Demonstrates how to use a "select or create" pattern for Laravel unit testing via `$this->actingAs($user);`
<?php
namespace Tests;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Spatie\Permission\Models\Role;
abstract class TestCase extends BaseTestCase
@agm1984
agm1984 / TestCase.php
Last active July 10, 2020 03:14
Demonstration of how to use a `resetAuth` function to fully reset auth state in-between Laravel unit tests. For theory, see: https://stackoverflow.com/a/57941133/6141025
<?php
use Illuminate\Auth\SessionGuard;
// ...
/**
* Resets AuthManager state by logging-out the user from all auth guards.
* This is used between unit tests to wipe cached auth state.
*