Skip to content

Instantly share code, notes, and snippets.

@sbatson5
sbatson5 / component.js
Last active February 24, 2020 21:39
Step 3
class CandidateImage extends HTMLElement {
connectedCallback() {
this.shadow = this.attachShadow({ mode: 'open' });
// Create an image
const image = new Image();
image.src = 'https://cdn-candidates.com/bernie.jpg';
// We can use appendChild just like we do the normal document
this.shadow.appendChild(image);
@sbatson5
sbatson5 / component.js
Last active February 24, 2020 21:22
Step 2
class CandidateImage extends HTMLElement {
connectedCallback() {
console.log('Adding a candidate!');
this.innerHTML = '<img src="https://cdn-candidates.com/bernie.jpg" />';
}
}
/**
* Register our custom element
*/
@sbatson5
sbatson5 / component.js
Last active February 24, 2020 21:22
Initial setup
class CandidateImage extends HTMLElement {
connectedCallback() {
console.log('Adding a candidate!');
this.innerHTML = '<img src="https://cdn-candidates.com/bernie.jpg" />';
}
}
@sbatson5
sbatson5 / query-example.js
Last active November 4, 2019 19:35
Testing firestore queries
// These are the mocked version of some firestore methods
// And we want to assert they are called correctly
const {
mockCollection,
mockGet,
mockWhere,
} = require('firestore-jest-mock/mocks/firestore');
const { mockFirebase } = require('firestore-jest-mock');
@sbatson5
sbatson5 / simple-state-firebase.js
Last active November 4, 2019 19:32
Simple firebase example of conditional query
const firebase = require('firebase');
function maybeGetEventsByState(state) {
const db = firebase.firestore();
const query = db.collection('events');
if (state) {
query = query.where('state', '==', state);
}
@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'
})
})
})
}));