Skip to content

Commit

Permalink
fortjs release v - 1.7.2
Browse files Browse the repository at this point in the history
* added test for rest in es6
*  issue with singleton not working for es6 fixed
  • Loading branch information
ujjwalguptaofficial committed Aug 13, 2019
1 parent 76db286 commit 724e717
Show file tree
Hide file tree
Showing 15 changed files with 2,756 additions and 389 deletions.
4 changes: 2 additions & 2 deletions dist/fort.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/fort.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion example/rest/javascript/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"plugins": [
["@babel/plugin-proposal-decorators", {
"legacy": true
}]
}],
"babel-plugin-parameter-decorator-custom"
]
}
23 changes: 8 additions & 15 deletions example/rest/javascript/controllers/default_controller.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import {
Controller,
DefaultWorker,
htmlResult,
textResult,
renderView,
viewResult
} from "fortjs";
import { Controller, DefaultWorker, textResult, viewResult, Worker, Assign } from "fortjs";

export class DefaultController extends Controller {

@DefaultWorker()
async index() {
async index(@Assign('FortJs') title) {
try {
const model = {
title: 'FortJs'
}
const result = await viewResult('default/index.html', model);
const data = {
title: title
};
const result = await viewResult('default/index.html', data);
return result;
} catch (ex) {
console.log(ex);
// handle exception and show user a good message.
// save this ex in a file or db, so that you can know what's the issue and where
const result = await textResult(`Our server is busy right now. Please try later.`);
const result = await textResult("Our server is busy right now. Please try later.");
return result;
}
}
Expand Down
22 changes: 22 additions & 0 deletions example/rest/javascript/controllers/default_controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DefaultController } from "./default_controller";
import { viewResult, Fort } from "fortjs";
import { createApp } from "../index";

describe('DefaultController', () => {
let app;
beforeAll(async () => {
app = await createApp();
});

it('index', async () => {
const expectedResult = await viewResult('default/index.html', {
title: 'FortJs'
});
const indexMethodOutput = await new DefaultController().index('FortJs');
expect(indexMethodOutput).toEqual(expectedResult);
});

afterAll(() => {
return app.destroy();
});
});
27 changes: 14 additions & 13 deletions example/rest/javascript/controllers/user_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@ import {
Route,
HTTP_STATUS_CODE,
HTTP_METHOD,
Guards
Guards,
Singleton
} from 'fortjs';
import {
UserService
} from '../services/user_service';
import {
ModelUserGuard
} from '../guards/model_user_guard';
import {
User
} from '../models/user';

export class UserController extends Controller {

constructor(@Singleton(UserService) service) {
super();
console.log('service', service);
this.service = service;
}

@DefaultWorker()
async getUsers() {
const service = new UserService();
return jsonResult(service.getUsers());
return jsonResult(this.service.getUsers());
}

@Worker([HTTP_METHOD.Post])
@Route("/")
@Guards([ModelUserGuard])
async addUser() {
const user = this.data.user;
const service = new UserService();
const newUser = service.addUser(user);
const newUser = this.service.addUser(user);
return jsonResult(newUser, HTTP_STATUS_CODE.Created);
}

Expand All @@ -43,7 +45,7 @@ export class UserController extends Controller {
async updateUser() {

const user = this.data.user;
const userUpdated = new UserService().updateUser(user);
const userUpdated = this.service.updateUser(user);
if (userUpdated === true) {
return textResult("user updated");
} else {
Expand All @@ -57,7 +59,7 @@ export class UserController extends Controller {
async getUser() {

const userId = Number(this.param.id);
const user = new UserService().getUser(userId);
const user = this.service.getUser(userId);
if (user == null) {
return textResult("invalid id");
}
Expand All @@ -70,10 +72,9 @@ export class UserController extends Controller {
async removeUser() {

const userId = Number(this.param.id);
const service = new UserService();
const user = service.getUser(userId);
const user = this.service.getUser(userId);
if (user != null) {
service.removeUser(userId);
this.service.removeUser(userId);
return textResult("user deleted");
} else {
return textResult("invalid user");
Expand Down
23 changes: 23 additions & 0 deletions example/rest/javascript/controllers/user_controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { jsonResult } from "fortjs";
import { createApp } from "../index";
import { UserController } from "./user_controller";
import { UserService } from "../services/user_service";

describe('UserController', () => {
let app;
let controller;
beforeAll(async () => {
app = await createApp();
controller = new UserController(new UserService());
});

it('getUsers', async () => {
const expectedResult = await jsonResult(controller.service.getUsers());
const indexMethodOutput = await controller.getUsers();
expect(indexMethodOutput).toEqual(expectedResult);
});

afterAll(() => {
return app.destroy();
});
});
30 changes: 30 additions & 0 deletions example/rest/javascript/e2e/default.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import axios from "axios";
import { createApp } from "..";
const instance = axios.create({
baseURL: 'http://localhost:4000',
timeout: 1000
});

describe('/default', () => {

let app;
beforeAll(async () => {
app = await createApp();
});

it('index', async () => {
const response = await instance.get('/', {
headers: {
accept: 'text/html'
}
});
expect(response.status).toEqual(200);
expect(response.headers['content-type']).toEqual('text/html');
expect(response.data).toContain('<title>FortJs</title>');
});

afterAll(() => {
return app.destroy();
});

});
129 changes: 129 additions & 0 deletions example/rest/javascript/e2e/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import axios from "axios";
import { createApp } from "../index";

const instance = axios.create({
baseURL: 'http://localhost:4000/user',
timeout: 1000
});

describe('/user', () => {

let app;
beforeAll(async () => {
app = await createApp();
});

it('/get all users', async () => {
const response = await instance.get('/', {
headers: {
accept: 'application/json'
}
});
expect(response.status).toEqual(200);
expect(response.headers['content-type']).toEqual('application/json');
expect(response.data).toEqual([
{ "address": "bhubaneswar india", "emailId": "ujjwal@mg.com", "gender": "male", "id": 1, "name": "ujjwal", "password": "admin" }
]);
});

it('/get single user', async () => {
const response = await instance.get('/1', {
headers: {
accept: 'application/json'
}
});
expect(response.status).toEqual(200);
expect(response.headers['content-type']).toEqual('application/json');
expect(response.data).toEqual(
{ "address": "bhubaneswar india", "emailId": "ujjwal@mg.com", "gender": "male", "id": 1, "name": "ujjwal", "password": "admin" }
);
});

it('/add user', async () => {
const user = {
"name": "ujjwal",
"emailId": "ujjwal@m.com",
"password": "12345as",
"gender": "male",
"address": "sadfsgbhfgtbrg"
};
const response = await instance.post('/', user, {
headers: {
accept: 'application/json'
}
});
expect(response.status).toEqual(201);
expect(response.headers['content-type']).toEqual('application/json');

expect(response.data).toEqual({ id: 2, ...user });
});

it('/update user', async () => {
const user = {
"name": "ujjwal gupta",
"emailId": "ujjwal@m.com",
"password": "12345as",
"gender": "male",
"address": "sadfsgbhfgtbrg",
"id": 2
};
const response = await instance.put('/', user, {
headers: {
accept: '*/*'
}
});
expect(response.status).toEqual(200);
expect(response.data).toEqual("user updated");
});

it('/check updated user', async () => {
const user = {
"name": "ujjwal gupta",
"emailId": "ujjwal@m.com",
"password": "12345as",
"gender": "male",
"address": "sadfsgbhfgtbrg",
"id": 2
};
const response = await instance.get('/2', {
headers: {
accept: 'application/json'
}
});
expect(response.status).toEqual(200);
expect(response.headers['content-type']).toEqual('application/json');
expect(response.data).toEqual(
user
);
});

it('/remove user', async () => {

const response = await instance.delete('/2', {
headers: {
accept: '*/*'
}
});
expect(response.status).toEqual(200);
expect(response.data).toEqual("user deleted");
});

it('/get deleted user or not existing user', async () => {
try {
const response = await instance.get('/2', {
headers: {
accept: '*/*'
}
});
}
catch (ex) {
expect(ex.response.status).toEqual(404);
expect(ex.response.data).toEqual('invalid user id');
}
});

afterAll(() => {
return app.destroy();
});

});
29 changes: 18 additions & 11 deletions example/rest/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import {
App
} from "./app";

const app = new App();
app.create({
folders: [{
alias: "/",
path: path.join(__dirname, "../static")
}]
}).then(() => {
console.log("Your fort is located at address - localhost:4000");
}).catch(err => {
console.error(err);
})
export const createApp = async () => {
const app = new App();
await app.create({
folders: [{
alias: "/",
path: path.join(__dirname, "../static")
}]
});
return app;
};

if (process.env.NODE_ENV !== "test") {
createApp().then(() => {
console.log("Your fort is located at address - localhost:4000");
}).catch(err => {
console.error(err);
});
}
9 changes: 9 additions & 0 deletions example/rest/javascript/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
transform: {
'^.+\\.js?$': 'babel-jest'
},
testEnvironment: 'node',
testRegex: '.*\\.(test|spec)?\\.(ts|tsx|js)$',
moduleFileExtensions: ['ts', 'js'],

};
Loading

0 comments on commit 724e717

Please sign in to comment.