• Design
  • Shotty
  • Blog
  • Reading
  • Photos
Menu

Jacob Ruiz

Product Designer
  • Design
  • Shotty
  • Blog
  • Reading
  • Photos
graphql-notes@2x.png

GraphQL: Writing Tests for Canceling a Stripe Subscription

July 3, 2019

We’ve already written tests for creating a subscription in Stripe, now it’s time to write some basic tests for the other case: canceling the subscription in Stripe.

First, we can create a function that takes in a cookie string (for authentication), and makes a call to our GraphQL API using Axios.

export const downgradeToBasic = async (cookie) => {
return await axios.post(
API_URL,
{
query: `
mutation {
downgradeToBasic {
id
username
role
}
}
`
},
{
headers: { Cookie: cookie },
withCredentials: true
}
);
};
view raw api.js hosted with ❤ by GitHub

Now we can use this function to test a couple of cases:

1. It throws an error if the user is not logged in

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);
});
view raw user.spec.js hosted with ❤ by GitHub

2. It returns a user if user is logged in and downgrade was successful

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: {
id: "1",
username: "jacob",
role: "BASIC"
}
}
};
expect(result.data).to.eql(expectedResult);
});
view raw user.spec.js hosted with ❤ by GitHub
← Today's Design System Component: <Breadcrumb />GraphQL: Writing Tests For Our Stripe Subscription Resolvers →
shotty-skinny2x.jpg

Shotty - Faster Access To Your Screenshots on Mac

Shotty is an award-winning Mac app I created to give you instant access to all your recent screenshots, right from the menu bar. You can even add annotations on-the-fly. Stop wasting time digging through Finder for your screenshots. I promise it’ll change your workflow forever (just read the App Store reviews!).



Most popular

information-architecture

Information Architecture: The Most Important Part of Design You're Probably Overlooking

Follow @JacobRuizDesign