Skip to content

Commit

Permalink
Add mockObject.reset (#293)
Browse files Browse the repository at this point in the history
* feat(mocks): Allow MockObjects to be resetted

Fixes #292

* test: Fix test naming

* Export resetMockObject and update implementation

* Add changeset

* Change api to mockObject.reset

---------

Co-authored-by: Piotr Szlachciak <[email protected]>
  • Loading branch information
dhardtke and sz-piotr authored Apr 21, 2024
1 parent ce60bac commit e5ce81a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-queens-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"earl": minor
---

Add mockObject.reset which resets all functions on MockObject
8 changes: 8 additions & 0 deletions packages/earl/src/mocks/mockObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ describe(mockObject.name, () => {
'Cannot access .run - no mock value provided.',
)
})

it('can be reset using mockObject.reset', () => {
const instance = mockObject<Jogger>({ run: () => 10 })
instance.run.returnsOnce(20)
mockObject.reset(instance)

expect(instance.run()).to.equal(10)
})
})
13 changes: 13 additions & 0 deletions packages/earl/src/mocks/mockObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ function replaceFunctionsWithMocks<T extends object>(object: T) {
}
return clone
}

/**
* Resets every `mockFn` of the given `MockObject`.
*
* @param mockObject - The `MockObject` to reset
*/
mockObject.reset = function reset(mockObject: MockObject<unknown>): void {
for (const value of Object.values(mockObject)) {
if (isMockFn(value)) {
value.reset()
}
}
}

0 comments on commit e5ce81a

Please sign in to comment.