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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const downgradeToBasic = async (cookie) => { | |
return await axios.post( | |
API_URL, | |
{ | |
query: ` | |
mutation { | |
downgradeToBasic { | |
id | |
username | |
role | |
} | |
} | |
` | |
}, | |
{ | |
headers: { Cookie: cookie }, | |
withCredentials: true | |
} | |
); | |
}; |
Now we can use this function to test a couple of cases:
1. It throws an error if the user is not logged in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
2. It returns a user if user is logged in and downgrade was successful
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |