-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up a scenario that makes use of ambient modules
- Loading branch information
Showing
5 changed files
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { mocked } from 'ts-jest/utils'; | ||
import isCallable from 'is-callable'; | ||
import callMeMaybe from './callMeMaybe'; | ||
|
||
jest.mock('is-callable'); | ||
|
||
const mockedIsCallable = mocked(isCallable, true); | ||
|
||
describe('callMeMaybe', () => { | ||
it('returns the result of the callable when isCallable returns true', () => { | ||
mockedIsCallable.mockReturnValue(true); | ||
const fn = () => 42; | ||
expect(callMeMaybe(fn)).toBe(42); | ||
}); | ||
|
||
it('returns null when isCallable returns false', () => { | ||
mockedIsCallable.mockReturnValue(false); | ||
const fn = () => 42; | ||
expect(callMeMaybe(fn)).toBeNull(); | ||
}); | ||
}); |
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,16 @@ | ||
import isCallable from 'is-callable'; | ||
|
||
/** | ||
* Returns whether the given argument is callable or not. | ||
* | ||
* @param callable - A function. | ||
* @returns Whether the given argument is callable or not. | ||
*/ | ||
function maybeCall(callable: any): any { | ||
if (isCallable(callable)) { | ||
return callable(); | ||
} | ||
return null; | ||
} | ||
|
||
export default maybeCall; |
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,5 @@ | ||
declare module 'is-callable' { | ||
export default function isCallable<T>( | ||
value: any, | ||
): value is (...args: any[]) => T; | ||
} |
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