-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reset function to MockTransport
- Loading branch information
Showing
2 changed files
with
18 additions
and
6 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 |
---|---|---|
@@ -1,18 +1,29 @@ | ||
import { Transport } from "../types" | ||
|
||
export class MockTransport implements Transport { | ||
private response: string | ||
private responses: string[] = [] | ||
|
||
constructor(response: any) { | ||
this.response = | ||
constructor(response?: string) { | ||
if (response) { | ||
this.addMock(response) | ||
} | ||
} | ||
|
||
reset():void { | ||
this.responses = [] | ||
} | ||
|
||
addMock(response: string): void { | ||
const stringResponse = | ||
typeof response === "string" ? response : JSON.stringify(response) | ||
this.responses.push(stringResponse) | ||
} | ||
|
||
async get(): Promise<Response> { | ||
return new Response(this.response) | ||
return new Response(this.responses.shift()) | ||
} | ||
|
||
async post(): Promise<Response> { | ||
return new Response(this.response) | ||
return new Response(this.responses.shift()) | ||
} | ||
} |