Skip to content

Commit

Permalink
feat: test file for controller
Browse files Browse the repository at this point in the history
version
  • Loading branch information
farisT committed Jun 11, 2019
1 parent c8277b4 commit aeb3763
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@liftr/cli",
"version": "1.5.3",
"version": "1.6.0",
"description": "The Liftr CLI provides scaffolding for the Liftr framework",
"author": "Faris Tangastani",
"repository": "https://github.com/farisT/liftr",
Expand Down
17 changes: 9 additions & 8 deletions src/create/createController.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import fs from 'fs-extra';
import chalk from 'chalk';
import { checkExistence } from '../helpers';
import { createTestController } from './testing/createTestController';

export const createController = (ControllerName: string) => {
export const createController = async (ControllerName: string) => {
const fileContent: string = `
import { Request, Response } from 'express';
export let ${ControllerName}Controller = (req: Request, res: Response) => {
// Methods to respond to the request at the route e.g:
res.send('${ControllerName} route');
res.send('${ControllerName} controller');
};
`;
const filepath: string = process.cwd() + `/src/controllers/${ControllerName}.controller.ts`;
const check = checkExistence(`/src/controllers/${ControllerName}.controller.ts`)
if(!check) {
fs.writeFile(filepath, fileContent, (err) => {
const check = checkExistence(`/src/controllers/${ControllerName}.controller.ts`);
if (!check) {
await fs.writeFile(filepath, fileContent, (err) => {
if (err) throw err;
});
}
else {
console.error(chalk.red(`Controller named ${ControllerName} already exists!`));
await createTestController(ControllerName);
} else {
console.error(chalk.red(`Controller named ${ControllerName} already exists!`));
process.exit(1);
}
};
5 changes: 5 additions & 0 deletions src/create/createExampleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const createExampleApi = async (setupName: string) => {
const fileContent = null;
const RoutePath: string = process.cwd() + `/${setupName}/src/routes/liftr.route.ts`;
const ControllerPath: string = process.cwd() + `/${setupName}/src/controllers/liftr.controller.ts`;
const ControllerSpecPath: string = process.cwd() + `/${setupName}/src/controllers/liftr.controller.spec.ts`;
const MiddleWarePath: string = process.cwd() + `/${setupName}/src/middleware/liftr.middleware.ts`;
const RoutingPath: string = process.cwd() + `/${setupName}/src/routes/LiftrRoutingModule.ts`;

Expand All @@ -13,6 +14,7 @@ export const createExampleApi = async (setupName: string) => {
ControllerPath,
RoutingPath,
MiddleWarePath,
ControllerSpecPath,
];
await creationArray.forEach(async (createPath) => {
await fs.writeFile(createPath, fileContent, (err) => {
Expand All @@ -27,6 +29,9 @@ export const createExampleApi = async (setupName: string) => {
if (createPath.includes('liftr.controller.ts')) {
await fs.copySync(path.resolve(__dirname, '../templates/controller.ts'), createPath);
}
if (createPath.includes('liftr.controller.spec.ts')) {
await fs.copySync(path.resolve(__dirname, '../templates/controller.spec.ts'), createPath);
}
if (createPath.includes('liftr.middleware.ts')) {
await fs.copySync(path.resolve(__dirname, '../templates/middleware.ts'), createPath);
}
Expand Down
9 changes: 4 additions & 5 deletions src/create/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ export const ${MiddlewareName}Middleware = (req: Request, res: Response, next: N
};
`;
const filepath = process.cwd() + `/src/middleware/${MiddlewareName}.middleware.ts`;
const check = checkExistence(`/src/middleware/${MiddlewareName}.middleware.ts`)
if(!check) {
const check = checkExistence(`/src/middleware/${MiddlewareName}.middleware.ts`);
if (!check) {
fs.writeFile(filepath, fileContent, (err) => {
if (err) throw err;
});
}
else {
console.error(chalk.red(`Middleware named ${MiddlewareName} already exists!`));
} else {
console.error(chalk.red(`Middleware named ${MiddlewareName} already exists!`));
process.exit(1);
}
};
9 changes: 4 additions & 5 deletions src/create/createRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ export const ${RouteName}Route: Router = Router()
.get('/', ${RouteName}Controller);
`;
const filepath = process.cwd() + `/src/routes/${RouteName}.route.ts`;
const check = checkExistence(`/src/routes/${RouteName}.route.ts`)
if(!check) {
const check = checkExistence(`/src/routes/${RouteName}.route.ts`);
if (!check) {
fs.writeFileSync(filepath, fileContent);
createController(RouteName);
}
else {
console.error(chalk.red(`Route named ${RouteName} already exists!`));
} else {
console.error(chalk.red(`Route named ${RouteName} already exists!`));
process.exit(1);
}
};
7 changes: 6 additions & 1 deletion src/create/createTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ export const createTesting = async (setupName: string) => {
const fileContent = null;
const mochaSetup: string = process.cwd() + `/${setupName}/test/mocha.opts`;
const nycSetup: string = process.cwd() + `/${setupName}/.nycrc`;
const mochaRequire: string = process.cwd() + `/${setupName}/test/mocha.require.ts`;

const testingFiles: string[] = [
mochaSetup,
nycSetup,
mochaRequire,
];
await testingFiles.forEach(async (createPath) => {
await fs.writeFile(createPath, fileContent, (err) => {
// if (err) throw err;
// if (err) console.error('Cant create files');
});
if (createPath.includes('mocha.opts')) {
await fs.copySync(path.resolve(__dirname, '../templates/mocha.opts'), createPath);
}
if (createPath.includes('.nycrc')) {
await fs.copySync(path.resolve(__dirname, '../templates/.nycrc'), createPath);
}
if (createPath.includes('mocha.require.ts')) {
await fs.copySync(path.resolve(__dirname, '../templates/mocha.require.ts'), createPath);
}
});
};
31 changes: 31 additions & 0 deletions src/create/testing/createTestController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from 'fs-extra';

export const createTestController = (controllerName: string) => {
const filepath: string = process.cwd() + `/src/controllers/${controllerName}.controller.spec.ts`;
const fileContent = `import * as sinon from 'sinon';
import { expect } from 'chai';
import { Request, Response } from 'express';
import { ${controllerName}Controller } from './${controllerName}.controller';
describe('src/controllers/${controllerName}.controller.ts', () => {
let sandbox: sinon.SinonSandbox;
let req: any = {};
let responseStub: Partial<Response>;
beforeEach(() => {
sandbox = sinon.createSandbox();
responseStub = {
send: sandbox.stub(),
}
});
it('should send a message' , () => {
${controllerName}Controller(req as Request, responseStub as Response);
expect(responseStub.send).to.be.calledWith('${controllerName} controller');
});
});
`;
fs.writeFile(filepath, fileContent, (err) => {
if (err) throw err;
});
};
12 changes: 0 additions & 12 deletions templates/app.ts

This file was deleted.

22 changes: 22 additions & 0 deletions templates/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as sinon from 'sinon';
import { expect } from 'chai';
import { Request, Response } from 'express';
import { liftrController } from './liftr.controller';

describe('src/controllers/liftr.controller.ts', () => {
let sandbox: sinon.SinonSandbox;
let req: any = {};
let responseStub: Partial<Response>;

beforeEach(() => {
sandbox = sinon.createSandbox();
responseStub = {
send: sandbox.stub(),
}
});

it('should send a message' , () => {
liftrController(req as Request, responseStub as Response);
expect(responseStub.send).to.be.calledWith('Lift off!');
});
});
2 changes: 1 addition & 1 deletion templates/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';

export let LiftrController = (req: Request, res: Response) => {
export let liftrController = (req: Request, res: Response) => {
res.send('Lift off!');
};
2 changes: 1 addition & 1 deletion templates/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from 'express';

export const LiftrMiddleware = (req: Request, res: Response, next: NextFunction) => {
export const liftrMiddleware = (req: Request, res: Response, next: NextFunction) => {
return next();
};
1 change: 1 addition & 0 deletions templates/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r ts-node/register test/mocha.require.ts
-r tsconfig-paths/register
-R spec
--recursive
Expand Down
1 change: 1 addition & 0 deletions templates/mocha.require.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('chai').use(require('sinon-chai'));
6 changes: 3 additions & 3 deletions templates/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router, Request, Response, NextFunction} from "express"
import { LiftrController } from "@controllers/liftr.controller"
import { liftrController } from "@controllers/liftr.controller"

export const LiftrRoute: Router = Router()
.get('/', LiftrController);
export const liftrRoute: Router = Router()
.get('/', liftrController);
4 changes: 2 additions & 2 deletions templates/routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AppRouter } from '@liftr/core'
import { LiftrRoute } from '@routes/liftr.route'
import { liftrRoute } from '@routes/liftr.route'


export const routes: AppRouter[] = [
{
handler: LiftrRoute,
handler: liftrRoute,
middleware: [],
path: '/',
},
Expand Down

0 comments on commit aeb3763

Please sign in to comment.