Skip to content

Commit

Permalink
Improve MockTransport
Browse files Browse the repository at this point in the history
add reset function to MockTransport
  • Loading branch information
ebisbe committed Oct 9, 2024
1 parent 6adbdbd commit 2c07ad6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const res = await flickr.test.login()
console.log(res.body)

// new
const body = await flickr('flickr.test.login')
const body = await flickr('flickr.test.login', {})
console.log(body)
```

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

// null auth does nothing
Expand Down
21 changes: 16 additions & 5 deletions src/transport/mock.ts
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())
}
}

0 comments on commit 2c07ad6

Please sign in to comment.