Skip to content

Commit d70e8a2

Browse files
TheWillGParallelTask
authored andcommitted
When baseUri is null or undefined throw error on app.bind() (#24)
1 parent 3020407 commit d70e8a2

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/api/dino.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ export class Dino implements IDino {
112112
// since container is already created and bound to express instance
113113
/**
114114
* Binds the dino instance to express app
115-
* @Throws Error(Errors.routerNotRegistered)
115+
* @Throws Error(Errors.routerNotRegistered)
116116
* @Throws Error(Errors.dinoAlreadyBinded)
117+
* @Throws Error(Errors.baseUriInvalid)
117118
*/
118119
bind(): void {
119120
if (this.isBinded) throw new Error(Errors.dinoAlreadyBinded);
121+
if (DataUtility.isUndefinedOrNull(this.appContainer.baseUri)) throw new Error(Errors.baseUriInvalid);
120122
if (DataUtility.isUndefinedOrNull(this.appContainer.useRouter)) {
121123
throw new Error(Errors.routerNotRegistered);
122124
}

src/modules/constants/errors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const Errors = {
22
dinoAlreadyBinded: 'dino.bind(): Already invoked',
3-
routerNotRegistered: 'Express router is not registered with dino'
3+
routerNotRegistered: 'Express router is not registered with dino',
4+
baseUriInvalid: 'Invalid baseUri to mount the dino app'
45
};

tests/unit/api/dino.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('api.dino.spec', () => {
2020
app.registerController(Boolean);
2121
expect(obj.controllers[0]).toBe(Function);
2222
expect(obj.controllers[1]).toBe(Boolean);
23-
// Following expects are common for most of the tests,
23+
// Following expects are common for most of the tests,
2424
// should not be deleted easily
2525
expect(express).toEqual({ expressInstance: true });
2626
expect(obj.baseUri).toBe('/test');
@@ -137,4 +137,22 @@ describe('api.dino.spec', () => {
137137
app.bind();
138138
expect(invoked).toBeTruthy();
139139
});
140+
it('bind.throws_error_when_null_baseUri_provided', () => {
141+
let obj: IAppContainer = {} as IAppContainer;
142+
obj.useRouter = () => null;
143+
obj.build = () => null;
144+
spyOn(AppContainer, 'create').and.callFake(() => obj);
145+
let app = new Dino({ expressInstance: true } as any, null);
146+
expect(() => app.bind())
147+
.toThrow(new Error(Errors.baseUriInvalid));
148+
});
149+
it('bind.throws_error_when_undefined_baseUri_provided', () => {
150+
let obj: IAppContainer = {} as IAppContainer;
151+
obj.useRouter = () => null;
152+
obj.build = () => null;
153+
spyOn(AppContainer, 'create').and.callFake(() => obj);
154+
let app = new Dino({ expressInstance: true } as any, undefined);
155+
expect(() => app.bind())
156+
.toThrow(new Error(Errors.baseUriInvalid));
157+
});
140158
});

0 commit comments

Comments
 (0)