Skip to content

Instantly share code, notes, and snippets.

@sbatson5
sbatson5 / mock-firebase-example.js
Created November 4, 2019 16:56
mockFirebase example
const { mockFirebase } = require('firestore-jest-mock');
mockFirebase({
database: {
users: [
{ id: 'abc123', first: 'Bob', last: 'builder', born: 1998 },
{ id: '123abc', first: 'Blues', last: 'builder', born: 1996 }
],
cities: [
{ id: 'LA', name: 'Los Angeles', state: 'CA', country: 'USA' },
@sbatson5
sbatson5 / mocked-firestore.js
Last active November 4, 2019 21:08
Simple fake firestore
class FakeFirestore {
collection(name) {
this.currentCollection = name;
return this;
}
where() {
return this;
}
@sbatson5
sbatson5 / multiple-firestore-queries.js
Last active February 26, 2020 09:35
Multiple firestore queries
const firebase = require('firebase');
function getCurrentEventsByStateForAdmins(state) {
const db = firebase.firestore();
db.collection('events')
.orderBy('expirationDate', 'asc')
.where('expirationDate', '>', new Date().toISOString())
.where('state', '==', state)
.where('isAdmin', '==', true)
@sbatson5
sbatson5 / firebase-add-example.js
Created November 4, 2019 16:21
Example of add to firebase
const firebase = require('firebase');
const db = firebase.firestore();
db.collection("users").add({
first: "Ada",
last: "Lovelace",
born: 1815
})
.then(function(docRef) {
@sbatson5
sbatson5 / firebase-mock-simple.js
Created November 4, 2019 16:20
Simple firebase mock
jest.mock('firebase', () => ({
firestore: jest.fn().mockReturnValue({
collection: jest.fn().mockReturnValue({
add: jest.fn().mockResolvedValue({
id: 'abc123'
})
})
})
}));
@sbatson5
sbatson5 / throttled-actions.js
Created September 27, 2019 15:16
Throttled Vuex Actions
import { api, throttle } from '@/utils';
const actions = {
setEvents({ commit }, payload) {
throttle(function* () {
const { zip, radius } = payload;
yield api.get('/events/general')
.then(response => commit('SET_GENERAL_EVENTS', response.data)
@sbatson5
sbatson5 / concurrency.js
Created September 27, 2019 15:09
Throttling Vuex actions
import store from '@/store';
/**
* A simple class we create just for tracking our running functions
*/
class Concurrency {
get(key) {
return this[key] || {};
}
function* genFunction() {
yield 'Hi there!';
yield 'Is this working?';
yield 'Bye 👋';
};
const ourMethod = genFunction();
ourMethod.next();
// {value: "Hi there!", done: false}
@sbatson5
sbatson5 / actions.js
Last active September 27, 2019 15:15
actions.js
const actions = {
setEvents({ commit }, payload) {
const { zip, radius } = payload;
api.get('/events/general')
.then(response => commit('SET_GENERAL_EVENTS', response.data)
.catch(catchError);
api.get(`/events/zip/${zip}`, { params: { radius } })
@sbatson5
sbatson5 / Install.php
Last active September 6, 2019 20:11
Install script for CraftCMS plugin
<?php
namespace upstatement\jobsboard\migrations;
use upstatement\jobsboard\Jobsboard;
use Craft;
use craft\config\DbConfig;
use craft\db\Migration;