Skip to content

Commit

Permalink
feat(facade): add spy method
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Oct 18, 2023
1 parent ddd68c8 commit c28414d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/ioc",
"version": "4.7.0",
"version": "4.8.0",
"description": "Global Ioc helper for Athenna ecosystem. Built on top of awilix.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -59,7 +59,7 @@
},
"devDependencies": {
"@athenna/common": "^4.18.0",
"@athenna/test": "^4.12.0",
"@athenna/test": "^4.13.0",
"@athenna/tsconfig": "^4.9.1",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
Expand Down
61 changes: 32 additions & 29 deletions src/facades/FacadeProxyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
* file that was distributed with this source code.
*/

import type {
SpyInstance,
MockBuilder,
StubInstance,
Mock as MockType
} from '@athenna/test'
import { debug } from '#src/debug'
import { Is, Module } from '@athenna/common'
import type { StubInstance, MockBuilder, Mock as MockType } from '@athenna/test'
import { PROTECTED_FACADE_METHODS } from '#src/constants/ProtectedFacadeMethods'

const athennaTest = await Module.safeImport('@athenna/test')
Expand All @@ -28,11 +33,6 @@ export class FacadeProxyHandler<T = any> {
*/
private provider: T = null

/**
* The stubbed service instance.
*/
private stubbed: StubInstance<T> = null

/**
* Creates a new instance of FacadeProxyHandler.
*/
Expand Down Expand Up @@ -74,35 +74,48 @@ export class FacadeProxyHandler<T = any> {

/**
* Resolves a service instance of the
* facade and save it to be used instead of the
* original one.
* facade and save it to be used as stub.
*
* The stub will be used instead of resolving
* the service.
*/
public stub(): StubInstance<T> {
this.provider = this.getProvider()
this.stubbed = athennaTest.Mock.stub(this.provider)
this.freeze()

return this.stubbed
return Mock.stub(this.provider)
}

/**
* Resolve a service instance of the facade
* and save it to be
*/
public spy(): SpyInstance<T> {
this.freeze()

return Mock.spy(this.provider)
}

/**
* Create a mock builder instance for the given method
* of the facade.
*/
public when(method: keyof T): MockBuilder {
this.provider = this.getProvider()

const stub = Mock.when<T>(this.provider, method)
this.freeze()

return stub
return Mock.when<T>(this.provider, method)
}

/**
* Restore the facade to the original state
* by removing the stubbed instance.
* Restore the mocked facade to the original state.
*/
public restore(): void {
this.stubbed = null
this.provider = null
if (!this.provider) {
return
}

Mock.restore(this.provider)

this.unfreeze()
}

/**
Expand All @@ -112,12 +125,6 @@ export class FacadeProxyHandler<T = any> {
* the same instance when a Facade method returns this.
*/
public set(_, key: string, value: any): boolean {
if (this.stubbed) {
this.stubbed[key] = value

return true
}

if (this.provider) {
this.provider[key] = value

Expand Down Expand Up @@ -150,10 +157,6 @@ export class FacadeProxyHandler<T = any> {
return this[key].bind(this)
}

if (this.stubbed) {
return this.stubbed[key]
}

if (this.provider) {
return this.provider[key]
}
Expand Down

0 comments on commit c28414d

Please sign in to comment.