Skip to content

Instantly share code, notes, and snippets.

@poetix
Created September 25, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poetix/3b7ad000488705240e2b19b5bb4d063e to your computer and use it in GitHub Desktop.
Save poetix/3b7ad000488705240e2b19b5bb4d063e to your computer and use it in GitHub Desktop.
Now without the interface...
import { expect } from 'chai';
import 'mocha';
class HelloEndpoint {
constructor(private service: HelloService) {}
async handle(evt: any): Promise<any> {
const greeting = await this.service.sayHello(evt.queryStringParameters.name);
return {
body: greeting
};
}
}
// Concrete implementation
class HelloService {
async sayHello(name: string): Promise<string> {
throw Error("Not implemented yet")
}
}
const helloService: HelloService & { receivedName?: string } = {
sayHello: async (name: string): Promise<string> => {
helloService.receivedName = name;
return `Hello ${name}!`;
}
}
const helloEndpoint = new HelloEndpoint(helloService);
describe("The Hello Endpoint", () => {
it("Should greet the person", async () => {
// Call the endpoint.
const result = await helloEndpoint.handle({
queryStringParameters: {
name: "Arthur Putey"
}
});
// Validate that the right data is passed into the service.
expect(helloService.receivedName).to.equal("Arthur Putey");
// Validate that the right response is returned to the client.
expect(result.body).to.equal("Hello Arthur Putey!");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment