diff --git a/README.md b/README.md index 90241d2..1a5b4f0 100644 --- a/README.md +++ b/README.md @@ -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) ``` @@ -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 diff --git a/src/transport/mock.ts b/src/transport/mock.ts index 7437c16..3d6dadf 100644 --- a/src/transport/mock.ts +++ b/src/transport/mock.ts @@ -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 { - return new Response(this.response) + return new Response(this.responses.shift()) } async post(): Promise { - return new Response(this.response) + return new Response(this.responses.shift()) } }