// Configure Mocha, telling both it and chai to use BDD-style tests. mocha.setup("tdd"); var assert = chai.assert; let repository = { "200": { "offer-id": 200, "percentage": 15 }, "400": { "offer-id": 400, "percentage": 22 } }; class IResponseStategy { process() {} } class ValidationFailedResponse extends IResponseStategy { process() { return { "code": 400, "body": { "errors": ["The id you provided is invalid"] } } } } class NotFoundResponse extends IResponseStategy { process() { notify("Offer not found: " + this.id); return { "code": 404, "body": { "errors": ["The id you provided cannot be found"] } } } } class ValidResponse extends IResponseStategy { constructor(id) { super(); this.id = id; } process() { return { "code": 200, "body": repository[this.id] } } } function chooseStategy(id) { let strategy = null; if (typeof id !== 'string') strategy = new ValidationFailedResponse(); else if (!repository[id]) strategy = new NotFoundResponse(); else strategy = new ValidResponse(id); return strategy; } function getOfferById(requestId) { return JSON.stringify(chooseStategy(requestId) .process()); } describe('pattern endpoint should', function() { it('return an offer 200', function() { assert.equal("{\"code\":200,\"body\":{\"offer-id\":200,\"percentage\":15}}", getOfferById("200")); }); it('return a validation error 400 when not found', function() { assert.equal("{\"code\":400,\"body\":{\"errors\":[\"The id you provided is invalid\"]}}", getOfferById(400)); }); it('return a not found 404', function() { assert.equal("{\"code\":404,\"body\":{\"errors\":[\"The id you provided cannot be found\"]}}", getOfferById("404")); }); }); // Run all our test suites. Only necessary in the browser. mocha.run();