This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract UnmockFaker from Backend. (#355)
* Add UnmockFaker. * Remove some duplication. * Simplify nockify. * Move nock logic to ServiceStore. * Clean-up. * Add faker tests. * Add type for NockAPI. * Add docs.
- Loading branch information
Kimmo Sääskilahti
authored and
Carolyn Stransky
committed
Dec 12, 2019
1 parent
b805d8b
commit c8ba193
Showing
6 changed files
with
291 additions
and
68 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { u } from ".."; | ||
import UnmockFaker from "../faker"; | ||
import { ServiceStore } from "../service/serviceStore"; | ||
|
||
const serviceStore = new ServiceStore([]); | ||
const faker = new UnmockFaker({ serviceStore }); | ||
|
||
const expectNServices = (expectedLength: number) => | ||
expect(Object.keys(faker.services).length).toEqual(expectedLength); | ||
|
||
describe("UnmockFaker", () => { | ||
beforeEach(() => serviceStore.removeAll()); | ||
|
||
describe("adding service with nock syntax", () => { | ||
it("adds a service", () => { | ||
faker | ||
.nock("https://foo.com") | ||
.get("/foo") | ||
.reply(200, { foo: u.string() }); | ||
expectNServices(1); | ||
}); | ||
it("adds a service and allows faking it", () => { | ||
faker | ||
.nock("https://foo.com") | ||
.get("/foo") | ||
.reply(200, { foo: u.string() }); | ||
const res = faker.fake({ | ||
host: "foo.com", | ||
protocol: "https", | ||
method: "get", | ||
path: "/foo", | ||
pathname: "/foo", | ||
headers: {}, | ||
query: {}, | ||
}); | ||
expect(res).toHaveProperty("body"); | ||
expect(res.body).toBeDefined(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as _ from "lodash"; | ||
import { responseCreatorFactory } from "../generator"; | ||
import { | ||
CreateResponse, | ||
IListener, | ||
ISerializedRequest, | ||
ISerializedResponse, | ||
IUnmockOptions, | ||
ServiceStoreType, | ||
} from "../interfaces"; | ||
import { | ||
IRandomNumberGenerator, | ||
randomNumberGenerator, | ||
} from "../random-number-generator"; | ||
import { addFromNock, NockAPI, ServiceStore } from "../service/serviceStore"; | ||
|
||
export interface IFakerOptions { | ||
listeners?: IListener[]; | ||
serviceStore: ServiceStore; | ||
randomNumberGenerator?: IRandomNumberGenerator; | ||
} | ||
|
||
const DEFAULT_OPTIONS: IUnmockOptions = { | ||
useInProduction: () => true, | ||
isWhitelisted: (__: string) => false, | ||
randomize: () => true, | ||
log: (__: string) => {}, // tslint:disable-line:no-empty | ||
}; | ||
|
||
export default class UnmockFaker { | ||
public createResponse: CreateResponse; | ||
/** | ||
* Add a new service to the faker using `nock` syntax. | ||
*/ | ||
public readonly nock: NockAPI; | ||
private readonly serviceStore: ServiceStore; | ||
private readonly randomNumberGenerator: IRandomNumberGenerator; | ||
private readonly listeners: IListener[]; | ||
/** | ||
* Unmock faker. Creates fake responses to fake requests, using | ||
* the services contained in `serviceStore`. | ||
* Add new services with the `faker.nock` method. | ||
* @param options Options for creating the object. | ||
*/ | ||
public constructor({ | ||
listeners, | ||
randomNumberGenerator: rng, | ||
serviceStore, | ||
}: IFakerOptions) { | ||
this.listeners = listeners ? listeners : []; | ||
this.randomNumberGenerator = rng || randomNumberGenerator({}); | ||
this.serviceStore = serviceStore; | ||
this.createResponse = this.createResponseCreator(); | ||
this.nock = addFromNock(this.serviceStore); | ||
} | ||
|
||
/** | ||
* Create a new faker function from the given options. | ||
* @param options Options for faking responses. | ||
*/ | ||
public setOptions(options: IUnmockOptions) { | ||
this.createResponse = this.createResponseCreator(options); | ||
} | ||
|
||
/** | ||
* Fake a response to a request. | ||
* @param request Serialized request. | ||
* @throws Error if no matcher was found for the request. | ||
* @returns Serialized response. | ||
*/ | ||
public fake(request: ISerializedRequest): ISerializedResponse { | ||
return this.createResponse(request); | ||
} | ||
|
||
/** | ||
* Services dictionary mapping service name to `Service` object. | ||
*/ | ||
public get services(): ServiceStoreType { | ||
return (this.serviceStore && this.serviceStore.services) || {}; | ||
} | ||
|
||
/** | ||
* Reset the states of all services. | ||
*/ | ||
public reset() { | ||
this.serviceStore.resetServices(); | ||
} | ||
|
||
private createResponseCreator(options?: IUnmockOptions): CreateResponse { | ||
return responseCreatorFactory({ | ||
listeners: this.listeners, | ||
options: options || DEFAULT_OPTIONS, | ||
rng: this.randomNumberGenerator, | ||
store: this.serviceStore, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.