Skip to content

Instantly share code, notes, and snippets.

View jacob-ruiz's full-sized avatar

Jacob Ruiz jacob-ruiz

View GitHub Profile
@jacob-ruiz
jacob-ruiz / machine.js
Created October 11, 2021 23:49
Generated by XState Viz: https://xstate.js.org/viz
const verifyAccount = assign({
accountVerified: (context, event) => true
});
const machine = Machine({
id: 'verification',
initial: 'softIDCheck',
context: {
retries: 0,
accountVerified: false,
@jacob-ruiz
jacob-ruiz / machine.js
Last active March 18, 2021 23:56
Generated by XState Viz: https://xstate.js.org/viz
const verifyAccount = assign({
accountVerified: (context, event) => true
});
const incrementRetries = assign({
retries: (context, event) => context.retries + 1
});
const machine = Machine({
id: 'verification',
@jacob-ruiz
jacob-ruiz / machine.js
Created March 17, 2021 00:28
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
import React, { Component } from "react";
import { Query } from "react-apollo";
import gql from "graphql-tag";
const CommentsQuery = gql`
query Comments($postId: ID!, $cursor: String) {
commentsForPost(postId: $postId, cursor: $cursor) {
# __typeName
edges {
node {
it("returns a user if user is logged in and downgrade was successful", async () => {
const credentials = { login: "jacob", password: "password" };
const response = await userApi.login(credentials);
const cookie = response.headers["set-cookie"][0];
const result = await userApi.downgradeToBasic(cookie);
const expectedResult = {
data: {
downgradeToBasic: {
it("throws an error if user is not logged in.", async () => {
const variables = { stripeToken: "tok_visa" };
const cookie = null;
const result = await userApi.downgradeToBasic(cookie);
const expectedError = "Please log in first.";
expect(result.data.errors[0].message).to.eql(expectedError);
});
export const downgradeToBasic = async (cookie) => {
return await axios.post(
API_URL,
{
query: `
mutation {
downgradeToBasic {
id
username
role
it("returns a user with role set to Premium if the Stripe token is valid and user is logged in.", async () => {
const credentials = { login: "jacob", password: "password" };
const response = await userApi.login(credentials);
const cookie = response.headers["set-cookie"][0];
const variables = { stripeToken: "tok_visa" };
const result = await userApi.upgradeToPremium(variables, cookie);
const expectedResult = {
data: {
{
downgradeToBasic: async (parent, args, { models, me, res }) => {
if (!me) throw new Error("Please log in first.");
if (me.role === "BASIC") {
throw new Error(
"You already have a Basic account. Upgrade to Premium to get the full experience."
);
}
{
upgradeToPremium: async (parent, { stripeToken }, { models, me, res }) => {
if (!me) throw new Error("Please log in first.");
if (me.role === "PREMIUM")
throw new Error("You already have a Premium account. Enjoy!");
try {
const stripeCustomer = await stripe.customers.create({
description: `Customer for ${me.email}`,
email: me.email,