Skip to content

Commit d4a76bd

Browse files
authored
Improve MockTransport (#169)
add reset function to MockTransport
1 parent 6adbdbd commit d4a76bd

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const res = await flickr.test.login()
8787
console.log(res.body)
8888

8989
// new
90-
const body = await flickr('flickr.test.login')
90+
const body = await flickr('flickr.test.login', {})
9191
console.log(body)
9292
```
9393

@@ -116,6 +116,7 @@ import * as assert from 'node:assert'
116116
// mock transport returns the response you pass in the constructor
117117
const transport = new MockTransport({
118118
stat: 'ok',
119+
foo: 'bar'
119120
})
120121

121122
// null auth does nothing

src/transport/mock.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { Transport } from "../types"
22

33
export class MockTransport implements Transport {
4-
private response: string
4+
private responses: string[] = []
55

6-
constructor(response: any) {
7-
this.response =
6+
constructor(response?: string) {
7+
if (response) {
8+
this.addMock(response)
9+
}
10+
}
11+
12+
reset():void {
13+
this.responses = []
14+
}
15+
16+
addMock(response: string): void {
17+
const stringResponse =
818
typeof response === "string" ? response : JSON.stringify(response)
19+
this.responses.push(stringResponse)
920
}
1021

1122
async get(): Promise<Response> {
12-
return new Response(this.response)
23+
return new Response(this.responses.shift())
1324
}
1425

1526
async post(): Promise<Response> {
16-
return new Response(this.response)
27+
return new Response(this.responses.shift())
1728
}
1829
}

0 commit comments

Comments
 (0)