Skip to content

Instantly share code, notes, and snippets.

View felippenardi's full-sized avatar

Felippe Nardi felippenardi

View GitHub Profile
const twoFactorMachine = {
two_factor: {
id: 'two_factor',
onEntry: ['checkTwoFactorSettings'],
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{ target: 'setup', cond: 'needsToSetupTwoFactor' },
const twoFactorMachine = {
two_factor: {
id: 'two_factor',
onEntry: ['checkTwoFactorSettings'],
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{ target: 'setup', cond: 'needsToSetupTwoFactor' },
const twoFactorMachine = {
two_factor: {
id: 'two_factor',
onEntry: ['checkTwoFactorSettings'],
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{ target: 'setup', cond: 'needsToSetupTwoFactor' },
@felippenardi
felippenardi / run_eslint.sh
Last active February 14, 2022 12:02
Shell script to Run Eslint only on modified files
#!/bin/bash
CHANGED_FILES=`git diff origin/master...HEAD --name-only`
node_modules/eslint/bin/eslint.js -f json $CHANGED_FILES --quiet | node_modules/eslines/index.js --quiet
if [[ $? != 0 ]] ; then
echo "Fix style violations"
exit 1
fi
echo "All changed files are good"
@felippenardi
felippenardi / guide.md
Last active July 25, 2018 17:35 — forked from swalkinshaw/tutorial.md
GraphQL API Design Guide

Guide: GraphQL API Design

This guide was created by VTEX for internal purposes inspired by Shopify's GraphQL API Tutorial.

Error Handling

There is no official GraphQL guidelines for handling errors, so we are stabilishing our own.

Testing an Angular $resource factory

Learn from live example how to test a $resource factory. What to test? What /not/ to test? When to use $resource? These questions will be answered here.

DEMO LINK

Let's start with a brief review.

What is $resource and when to use it?

The $resource is an Angular Service that makes it really easy to setup CRUD operations with RESTful APIs. If you have a /api/posts/ , this is all the code you need to setup http operations for POST, PUT, DELETE and GET:

# Testing an Angular $resource factory
#angular #medium
Learn from live example how to test a $resource factory. What to test? What /not/ to test? When to use $resource? These questions will be answered here.
[DEMO LINK](https://gist.run/?id=38949509eed11e6c1527218385579f80)
Let's start with a brief review.
## What is $resource and when to use it?

Testing an Angular $resource factory

This is an example of how to test an Angular Resource created with $resource.

Live Demo

Important take aways:

  • Do not mock $resource. Instead, you can use the resource's methods and assert that it is making the right API call. By doing that you can later change the implementation (for example for replace $resource for Angular Cached Resource) without needing toi go change all previous tests.
  • When intercepting the response of a custom method, do not transform the response string into json yourself. When you use the transformResponse action property of a $resource, you replace the default angular parser that is transforming backend response from string to JSON. It has some logic to ensure that the response is a JSON and can be parsed securely. You can use it by injecting $http into your factory and using `$http.defaults.transformRespon
@felippenardi
felippenardi / 0_reuse_code.js
Created June 19, 2016 14:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console