-
Notifications
You must be signed in to change notification settings - Fork 0
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
ef3ff38
commit 4de059a
Showing
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as AsynchronousCaller } from "./src/AsynchronousCaller"; | ||
export { default as SynchronousCaller } from "./src/SynchronousCaller"; |
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,10 @@ | ||
import Callable from "./Callable"; | ||
import Caller from "./Caller"; | ||
|
||
export default class AsynchronousCaller implements Caller { | ||
public call(thisArg: any, callable: Callable, ...args: any[]): Promise<any> { | ||
return new Promise(resolve => setTimeout(resolve)).then(() => | ||
callable.call(thisArg, ...args) | ||
); | ||
} | ||
} |
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,37 @@ | ||
import AsynchronousCaller from "../AsynchronousCaller"; | ||
import SynchronousCaller from "../SynchronousCaller"; | ||
|
||
describe("AsynchronousCaller", () => { | ||
const caller = new AsynchronousCaller(); | ||
|
||
it("calls the provided callable", async () => { | ||
const callable = jest.fn(); | ||
await caller.call(null, callable); | ||
expect(callable).toHaveBeenCalled(); | ||
}); | ||
|
||
it("passes arguments to the provided callable", async () => { | ||
const callable = jest.fn(); | ||
await caller.call(null, callable, 1, 2, 3); | ||
expect(callable).toHaveBeenCalledWith(1, 2, 3); | ||
}); | ||
|
||
it("sets the `this` context to the provided value", async () => { | ||
const context = { foo: "bar" }; | ||
await caller.call(context, function(this: { foo: string }) { | ||
expect(this).toBe(context); | ||
}); | ||
}); | ||
|
||
it("asynchronously calls the callable", async () => { | ||
const callable = jest.fn(); | ||
|
||
await Promise.all([ | ||
caller.call(null, callable, "b"), | ||
new SynchronousCaller().call(null, callable, "a") | ||
]); | ||
|
||
expect(callable).toHaveBeenNthCalledWith(1, "a"); | ||
expect(callable).toHaveBeenNthCalledWith(2, "b"); | ||
}); | ||
}); |