Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
simenbrekken / README.md
Last active September 5, 2024 07:13
Hide fetch/XHR in Cypress command log

This novel workaround simply hides any command log entries that originate from fetch/XHR requests.

While I've updated this receipe for Cypress 10 and converted it to TypeScript you should be able to use it in a JavaScript project by ignoring the cypress.d.ts file and placing the snippet from e2e.ts in e2e.js instead.

@simenbrekken
simenbrekken / analytics.js
Created February 12, 2018 07:31
KISS analytics abstraction
const googleAnalytics = event => {
switch (event.type) {
default:
window.dataLayer.push(event)
}
}
const facebook = event => {
switch (event.type) {
case 'pageview':
@simenbrekken
simenbrekken / README.md
Last active August 7, 2019 15:01
Apollo Client 2.0 withFragments

This HoC gives you a way to work with co-located fragments that is similar to Relay's FragmentContainer

It supports both fragment maps extrapolation of prop names from fragment definitions.

Fragment map

export default withFragments({
  price: gql`
 fragment ProductDetails_price on Product {
@simenbrekken
simenbrekken / index.html
Created November 22, 2017 06:54
Field-error-handling
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<!-- Styles -->
<link rel="stylesheet" href="style.css">
@simenbrekken
simenbrekken / GraphQL.js
Last active November 7, 2017 05:20
react-apollo graphql de-enhancer
import { Component, createElement } from 'react'
import { graphql as withGraphQL } from 'react-apollo'
class GraphQL extends Component {
constructor(props, context) {
super(props, context)
this.client = props.client || context.client
this.renderer = this.createRenderer(this.props)
}
@simenbrekken
simenbrekken / index.js
Created October 4, 2017 07:19
Signal handling for node Express apps in Docker
const PORT = process.env.PORT || 8081
const HOST = process.env.HOST || 'localhost'
const app = express()
const server = app.listen(PORT, err => {
if (err) {
console.error(err)
process.exit(1)
}
@simenbrekken
simenbrekken / README.md
Last active October 11, 2017 16:23
Express.js brute-force ☕️-reloading

This neat little script let's you develop Express apps without using something like nodemon to reload your server between changes.

Usage:

  1. Put dev.js outside your project source folder, scripts/dev.js is what I'm using.
  2. Make sure your actual app in src/index.js exports the Express instance itself without listening:
import express from 'express'
@simenbrekken
simenbrekken / MyComponent.js
Created September 13, 2017 04:54
Server-side rendering React applications with request resolver
componentWillMount() {
registerRequestPromises(() => {
const queries = createQueries(mapPropsToQueries, this.props)
return this.dispatchQueries(queries)
})
}
@simenbrekken
simenbrekken / Component.js
Last active April 28, 2017 06:49
Simplified redux-query reducer with error handling
import { getQueryByUrl } from './reducers/queries'
import { getPageByPath } from './reducers/pages'
const Page = ({ loading = true, error, page }) => {
if (loading) {
return <Loader />
}
if (error) {
return <Error error={error} />
@simenbrekken
simenbrekken / request-resolver.js
Last active April 10, 2017 11:02
Universal Rendering with Redux Query and React Router v4
let pendingPromises
let resolving = false
export const registerRequestPromises = createRequestPromises => {
if (resolving) {
const promises = createRequestPromises()
if (promises) {
pendingPromises.push(...promises)
}