Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
feat: test for loading loopback apps
Browse files Browse the repository at this point in the history
  • Loading branch information
JamilOmar committed Mar 19, 2020
1 parent e7d857e commit e8ecc60
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 11 deletions.
14 changes: 3 additions & 11 deletions lib/loopback-loader/loopback-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ export class LoopbackLoader {
this.apis = [];
this.config = config;
}

/**
* gets the Api settings from the config file
* @return {config}
*/
public getApiSettings(config: any, api: string) {
return _.get(config, api);
}
/**
* loads all the loopback apis from the package dependency
* @param {location}
Expand All @@ -48,7 +40,7 @@ export class LoopbackLoader {
this.apis = [];
const manifest = Utils.getPackageManifest(this.mainDir);
const lscSettings = Utils.getPackageLscSettings(manifest);
const dependecies = lscSettings?.getPackageDependencies || Utils.getPackageDependencies(manifest);
const dependecies = lscSettings?.packageDependencies || Utils.getPackageDependencies(manifest);
const lbApis:LoopbackApi[] = [];
const loopbackApiSettings: LoopbackApiSettings = lscSettings?.loopbackApi;
/* if the local project has an api */
Expand Down Expand Up @@ -81,8 +73,8 @@ export class LoopbackLoader {
public getApis(): LoopbackApi[] {
return this.apis;
}
private setApiFormat(api:LoopbackApi , module:any ){
const apiConfig = this.getApiSettings(this.config, api.configAlias || api.name);
public setApiFormat(api:LoopbackApi , module:any ){
const apiConfig = _.get(this.config, api.configAlias || api.name);
let basePath:string = api.basePath || apiConfig?.basePath || api.name;
basePath = basePath.startsWith('/')?basePath: `/${basePath}`;
return { name: api.name, app: new module.app(this.config),basePath };
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/loopback-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

class Helper{
constructor(){
}
}

module.exports={
app:Helper
}

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

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

20 changes: 20 additions & 0 deletions test/fixtures/loopback-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "loopback-app-package",
"version": "0.0.1",
"main": "index.js",
"lsc":{
"loopbackApi":{
"name":"test-api"
},
"packageDependencies": {
"loopback-api-package-1": {"name":"loopback-api",
"basePath":"/loopback" ,
"isLoopbackApi":true}
}
},

"dependencies": {
"uuid": "^3.3.2"
}
}

61 changes: 61 additions & 0 deletions test/lib/unit/lib/loopback-loader/loopback-loader_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {expect} from '@loopback/testlab';
import * as path from 'path';
const {LoopbackLoader} = require('../../../../../lib/loopback-loader');
describe('Loopback Loader', () => {

let config:any;
beforeEach(async () => {
config = {
services: {
main: `${process.cwd()}/test/fixtures/loopback-app`
},
test3:{
basePath:"/test3"
}
};
});

it('should create a loopback loader instance ', async () => {
const lbLoader = new LoopbackLoader(config);
expect(lbLoader).to.not.be.undefined();
});

it('should load all the apis ', async () => {
const lbLoader = new LoopbackLoader(config);
expect(lbLoader).to.not.be.undefined();
const apis =lbLoader.loadApis();
expect(apis.length).to.greaterThanOrEqual(2);
});
it('should load all the apis and test get all api ', async () => {
const lbLoader = new LoopbackLoader(config);
expect(lbLoader).to.not.be.undefined();
const apis =lbLoader.loadApis();
expect(apis.length).to.greaterThanOrEqual(2);
expect(apis.length).to.equal(lbLoader.getApis().length);
});

it('should set the api format setApiFormat ', async () => {
const lbLoader = new LoopbackLoader(config);
const result1 = lbLoader.setApiFormat( {name:"test" , basePath:"/basepath"}, require(path.join(config.services.main, './')) );
const result2 = lbLoader.setApiFormat( {name:"test"}, require(path.join(config.services.main, './')) );
const result3 = lbLoader.setApiFormat( {name:"test" , configAlias:"test3"}, require(path.join(config.services.main, './')) );
expect(result1).to.not.be.undefined();
expect(result1.name).to.be.equal("test");
expect(result1.basePath).to.be.equal("/basepath");
expect(result1.app).to.not.be.undefined();

expect(result2).to.not.be.undefined();
expect(result2.name).to.be.equal("test");
expect(result2.basePath).to.be.equal("/test");
expect(result2.app).to.not.be.undefined();

expect(result3).to.not.be.undefined();
expect(result3.name).to.be.equal("test");
expect(result3.basePath).to.be.equal("/test3");
expect(result3.app).to.not.be.undefined();
});



});

0 comments on commit e8ecc60

Please sign in to comment.