SplitTestMiddlewareTests.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const modulePath = path.join(
  4. __dirname,
  5. '../../../../app/src/Features/SplitTests/SplitTestMiddleware'
  6. )
  7. const sinon = require('sinon')
  8. const MockResponse = require('../helpers/MockResponse')
  9. const MockRequest = require('../helpers/MockRequest')
  10. describe('SplitTestMiddleware', function () {
  11. beforeEach(function () {
  12. this.SplitTestMiddleware = SandboxedModule.require(modulePath, {
  13. requires: {
  14. './SplitTestHandler': (this.SplitTestHandler = {
  15. promises: {
  16. getAssignment: sinon.stub().resolves(),
  17. },
  18. }),
  19. },
  20. })
  21. this.req = new MockRequest()
  22. this.res = new MockResponse()
  23. this.next = sinon.stub()
  24. })
  25. it('assign multiple split test variants in locals', async function () {
  26. this.SplitTestHandler.promises.getAssignment
  27. .withArgs(this.req, 'ui-overhaul')
  28. .resolves({
  29. variant: 'default',
  30. })
  31. this.SplitTestHandler.promises.getAssignment
  32. .withArgs(this.req, 'other-test')
  33. .resolves({
  34. variant: 'foobar',
  35. })
  36. const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([
  37. 'ui-overhaul',
  38. 'other-test',
  39. ])
  40. await middleware(this.req, this.res, this.next)
  41. sinon.assert.calledWith(
  42. this.SplitTestHandler.promises.getAssignment,
  43. this.req,
  44. this.res,
  45. 'ui-overhaul'
  46. )
  47. sinon.assert.calledWith(
  48. this.SplitTestHandler.promises.getAssignment,
  49. this.req,
  50. this.res,
  51. 'other-test'
  52. )
  53. sinon.assert.calledOnce(this.next)
  54. })
  55. it('assign no split test variant in locals', async function () {
  56. const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([])
  57. await middleware(this.req, this.res, this.next)
  58. sinon.assert.notCalled(this.SplitTestHandler.promises.getAssignment)
  59. sinon.assert.calledOnce(this.next)
  60. })
  61. it('exception thrown by assignment does not fail the request', async function () {
  62. this.SplitTestHandler.promises.getAssignment
  63. .withArgs(this.req, this.res, 'some-test')
  64. .throws(new Error('failure'))
  65. const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([
  66. 'some-test',
  67. ])
  68. await middleware(this.req, this.res, this.next)
  69. sinon.assert.calledWith(
  70. this.SplitTestHandler.promises.getAssignment,
  71. this.req,
  72. this.res,
  73. 'some-test'
  74. )
  75. sinon.assert.calledOnce(this.next)
  76. })
  77. })