SplitTestHelper.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { assert } from 'chai'
  2. import { CacheFlow } from 'cache-flow'
  3. const sendStaffRequest = async function (
  4. staffUser,
  5. { method, path, payload, clearCache = true }
  6. ) {
  7. const response = await staffUser.doRequest(method, {
  8. uri: path,
  9. json: payload,
  10. })
  11. if (clearCache) {
  12. await CacheFlow.reset('split-test')
  13. }
  14. return response
  15. }
  16. const createTest = async function (staffUser, payload) {
  17. const response = await sendStaffRequest(staffUser, {
  18. method: 'POST',
  19. path: '/admin/api/split-test/create',
  20. payload,
  21. })
  22. return response.body
  23. }
  24. const updateTestConfig = async function (staffUser, payload) {
  25. const response = await sendStaffRequest(staffUser, {
  26. method: 'POST',
  27. path: '/admin/api/split-test/update-config',
  28. payload,
  29. })
  30. return response.body
  31. }
  32. const expectResponse = async function (
  33. staffUser,
  34. { method, path, payload },
  35. { status, body, excluding, excludingEvery }
  36. ) {
  37. const result = await sendStaffRequest(staffUser, { method, path, payload })
  38. assert.equal(result.response.statusCode, status)
  39. if (body) {
  40. if (excludingEvery) {
  41. assert.deepEqualExcludingEvery(result.body, body, excludingEvery)
  42. } else if (excluding) {
  43. assert.deepEqualExcludingEvery(result.body, body, excluding)
  44. } else {
  45. assert.deepEqual(result.body, body)
  46. }
  47. }
  48. }
  49. export default {
  50. sendStaffRequest,
  51. createTest,
  52. updateTestConfig,
  53. expectResponse,
  54. }