Files
Verso/services/web/test/unit/src/SplitTests/SplitTestMiddlewareTests.js
T
Alexandre Bourdin 1b954fa720 Merge pull request #6332 from overleaf/ab-split-test-param-overrides
Split tests query param overrides

GitOrigin-RevId: 8112710d057ddc22cebf37a619dfc969be57b6cc
2022-01-25 09:03:04 +00:00

90 lines
2.5 KiB
JavaScript

const SandboxedModule = require('sandboxed-module')
const path = require('path')
const modulePath = path.join(
__dirname,
'../../../../app/src/Features/SplitTests/SplitTestMiddleware'
)
const sinon = require('sinon')
const MockResponse = require('../helpers/MockResponse')
const MockRequest = require('../helpers/MockRequest')
describe('SplitTestMiddleware', function () {
beforeEach(function () {
this.SplitTestMiddleware = SandboxedModule.require(modulePath, {
requires: {
'./SplitTestHandler': (this.SplitTestHandler = {
promises: {
assignInLocalsContext: sinon.stub().resolves(),
},
}),
},
})
this.req = new MockRequest()
this.res = new MockResponse()
this.next = sinon.stub()
})
it('assign multiple split test variants in locals', async function () {
this.SplitTestHandler.promises.assignInLocalsContext
.withArgs(this.req, 'ui-overhaul')
.resolves({
variant: 'default',
})
this.SplitTestHandler.promises.assignInLocalsContext
.withArgs(this.req, 'other-test')
.resolves({
variant: 'foobar',
})
const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([
'ui-overhaul',
'other-test',
])
await middleware(this.req, this.res, this.next)
sinon.assert.calledWith(
this.SplitTestHandler.promises.assignInLocalsContext,
this.req,
this.res,
'ui-overhaul'
)
sinon.assert.calledWith(
this.SplitTestHandler.promises.assignInLocalsContext,
this.req,
this.res,
'other-test'
)
sinon.assert.calledOnce(this.next)
})
it('assign no split test variant in locals', async function () {
const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([])
await middleware(this.req, this.res, this.next)
sinon.assert.notCalled(this.SplitTestHandler.promises.assignInLocalsContext)
sinon.assert.calledOnce(this.next)
})
it('exception thrown by assignment does not fail the request', async function () {
this.SplitTestHandler.promises.assignInLocalsContext
.withArgs(this.req, this.res, 'some-test')
.throws(new Error('failure'))
const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([
'some-test',
])
await middleware(this.req, this.res, this.next)
sinon.assert.calledWith(
this.SplitTestHandler.promises.assignInLocalsContext,
this.req,
this.res,
'some-test'
)
sinon.assert.calledOnce(this.next)
})
})