Skip to content

Instantly share code, notes, and snippets.

@TimNZ
Last active April 8, 2020 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimNZ/93c94888eef7e2bee67860238929d60d to your computer and use it in GitHub Desktop.
Save TimNZ/93c94888eef7e2bee67860238929d60d to your computer and use it in GitHub Desktop.
Testing simultaneous guest user cart management of CoCart, ensuring each session is separate
const axios = require("axios").default
const axiosCookieJarSupport = require("axios-cookiejar-support").default
const CookieJar = require("tough-cookie").CookieJar
const assert = require('assert')
let instanceId = 1
const createAxiosInstance = () => {
const axiosInstance = axios.create({
baseURL: "https://test.site/wp-json/cocart/v1",
withCredentials: true,
})
axiosCookieJarSupport(axiosInstance)
axiosInstance.defaults.jar = new CookieJar()
axiosInstance.instanceId = instanceId++
return axiosInstance
}
const getCart = async (axiosInstance) =>
axiosInstance.get("/get-cart").then((response) => {
console.log(axiosInstance.instanceId, "get-cart", response.data)
return response.data
})
const addItem = async (axiosInstance, productId) =>
axiosInstance
.post("/add-item", {
product_id: productId,
})
.then((response) => {
console.log(axiosInstance.instanceId, "add-item", response.data)
return response.data
})
const clearCart = async (axiosInstance) =>
axiosInstance.post("/clear").then((response) => {
console.log(axiosInstance.instanceId, "clear", response.data)
return response.data
})
async function run(axiosInstance) {
let cart = await getCart(axiosInstance)
assert(cart.length == 0)
await addItem(axiosInstance, 1)
cart = await getCart(axiosInstance)
assert(cart.totals.subtotal == '$10.00')
await addItem(axiosInstance, 2)
cart = await getCart(axiosInstance)
assert(cart.totals.subtotal == '$20.00')
}
run(createAxiosInstance())
run(createAxiosInstance())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment