-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a837e9c
commit 3a3c742
Showing
3 changed files
with
203 additions
and
48 deletions.
There are no files selected for viewing
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
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
164 changes: 164 additions & 0 deletions
164
test/test-helpers/simplified-account/controllers/ExperimentalTestBuilder.class.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
const sinon = require('sinon') | ||
const _ = require('lodash') | ||
const proxyquire = require('proxyquire') | ||
|
||
class TestRequest { | ||
withUser (user) { | ||
this.user = user | ||
return this | ||
} | ||
|
||
withAccount (account) { | ||
this.account = account | ||
return this | ||
} | ||
|
||
withService (service) { | ||
this.service = service | ||
return this | ||
} | ||
|
||
withBody (body) { | ||
this.body = body | ||
return this | ||
} | ||
|
||
static copy (sourceRequest) { | ||
const newRequest = new TestRequest() | ||
newRequest | ||
.withAccount(sourceRequest.account) | ||
.withUser(sourceRequest.user) | ||
.withService(sourceRequest.service) | ||
return newRequest | ||
} | ||
} | ||
|
||
class TestResponse { | ||
withRedirectSpy () { | ||
this.redirect = sinon.spy() | ||
return this | ||
} | ||
} | ||
|
||
module.exports = class ExperimentalTestBuilder { | ||
constructor (controllerPath) { | ||
this.controllerPath = controllerPath | ||
this.next = sinon.spy() | ||
this.req = new TestRequest() | ||
this.res = new TestResponse() | ||
.withRedirectSpy() | ||
} | ||
|
||
withAccountType (type) { | ||
this.req.withAccount({ type }) | ||
return this | ||
} | ||
|
||
withAccount (account) { | ||
this.req.withAccount(account) | ||
return this | ||
} | ||
|
||
withRequestBody (body) { | ||
this.req.withBody(body) | ||
return this | ||
} | ||
|
||
withServiceExternalId (serviceExternalId) { | ||
this.req.service.externalId = serviceExternalId | ||
return this | ||
} | ||
|
||
withService (service) { | ||
this.req.service = service | ||
return this | ||
} | ||
|
||
withStubs (stubs) { | ||
this.stubs = stubs | ||
return this | ||
} | ||
|
||
withReq (req) { | ||
this.req = new TestRequest() | ||
.withService(req.service) | ||
.withBody(req.body) | ||
.withAccount(req.account) | ||
.withUser(req.user) | ||
return this | ||
} | ||
|
||
withRes (res) { | ||
this.res = { | ||
...res | ||
} | ||
return this | ||
} | ||
|
||
withNext (next) { | ||
this.next = next | ||
return this | ||
} | ||
|
||
build () { | ||
return new ControllerTest( | ||
this.controllerPath, | ||
this.req, | ||
this.res, | ||
this.next, | ||
this.stubs | ||
) | ||
} | ||
|
||
static copy (sourceTest) { | ||
return new ExperimentalTestBuilder(sourceTest.controllerPath) | ||
.withReq(sourceTest.req) | ||
.withRes(sourceTest.res) | ||
.withNext(sourceTest.next) | ||
.withStubs(sourceTest.stubs) | ||
// .build() | ||
} | ||
} | ||
|
||
class ControllerTest { | ||
constructor (controllerPath, req, res, next, stubs) { | ||
this.controllerPath = controllerPath | ||
this.controller = proxyquire(this.controllerPath, { | ||
...stubs | ||
}) | ||
|
||
this.req = req | ||
this.res = res | ||
this.next = next | ||
this.stubs = stubs | ||
} | ||
|
||
callMethod (method, recursive = false) { | ||
sinon.resetHistory() | ||
const controllerMethod = this.controller[method] | ||
return this.#call(controllerMethod, recursive) | ||
} | ||
|
||
call (recursive = false) { | ||
sinon.resetHistory() | ||
return this.#call(this.controller, recursive) | ||
} | ||
|
||
#call (method, recursive = false) { | ||
if (method instanceof Array && recursive) { | ||
return method.forEach(async subMethod => { | ||
await this._call(subMethod, recursive) | ||
}) | ||
} else if (typeof method !== 'function') { | ||
throw new Error(`Method [${method}] is not a function`) | ||
} | ||
|
||
return method(this.req, this.res, this.next) | ||
} | ||
|
||
callMethodAtIndex (method, index, recursive = false) { | ||
sinon.resetHistory() | ||
const controllerMethod = this.controller[method][index] | ||
return this.#call(controllerMethod) | ||
} | ||
} |