Skip to content

Commit 0787932

Browse files
build: update tests
1 parent a27b0df commit 0787932

31 files changed

+2947
-3250
lines changed

package-lock.json

Lines changed: 39 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
"devDependencies": {
5353
"@types/express": "^4.17.6",
5454
"@types/express-session": "^1.17.0",
55+
"@types/form-data": "^2.5.0",
5556
"@types/jest": "^26.0.9",
5657
"@types/koa": "^2.11.3",
58+
"@types/multer": "^1.4.3",
5759
"@types/node": "^14.0.11",
5860
"@typescript-eslint/eslint-plugin": "^3.8.0",
5961
"@typescript-eslint/parser": "^3.8.0",
@@ -66,7 +68,9 @@
6668
"eslint": "^7.6.0",
6769
"eslint-config-prettier": "^6.11.0",
6870
"eslint-plugin-jest": "^23.20.0",
71+
"form-data": "^3.0.0",
6972
"handlebars": "^4.7.6",
73+
"http-status-codes": "^1.4.0",
7074
"husky": "^4.2.5",
7175
"jest": "^26.2.2",
7276
"kcors": "^2.2.2",

src/util/container.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Container options.
3+
*/
4+
export interface UseContainerOptions {
5+
6+
/**
7+
* If set to true, then default container will be used in the case if given container haven't returned anything.
8+
*/
9+
fallback?: boolean;
10+
11+
/**
12+
* If set to true, then default container will be used in the case if given container thrown an exception.
13+
*/
14+
fallbackOnErrors?: boolean;
15+
16+
}
17+
18+
/**
19+
* Container to be used by this library for inversion control. If container was not implicitly set then by default
20+
* container simply creates a new instance of the given class.
21+
*/
22+
const defaultContainer: { get<T>(someClass: { new (...args: any[]): T }|Function): T } = new (class {
23+
private instances: { type: Function, object: any }[] = [];
24+
get<T>(someClass: { new (...args: any[]): T }): T {
25+
let instance = this.instances.find(instance => instance.type === someClass);
26+
if (!instance) {
27+
instance = { type: someClass, object: new someClass() };
28+
this.instances.push(instance);
29+
}
30+
31+
return instance.object;
32+
}
33+
})();
34+
35+
let userContainer: { get<T>(someClass: { new (...args: any[]): T }|Function): T };
36+
let userContainerOptions: UseContainerOptions;
37+
38+
/**
39+
* Sets container to be used by this library.
40+
*/
41+
export function useContainer(iocContainer: { get(someClass: any): any }, options?: UseContainerOptions): void {
42+
userContainer = iocContainer;
43+
userContainerOptions = options;
44+
}
45+
46+
/**
47+
* Gets the IOC container used by this library.
48+
*/
49+
export function getFromContainer<T>(someClass: { new (...args: any[]): T }|Function): T {
50+
if (userContainer) {
51+
try {
52+
const instance = userContainer.get(someClass);
53+
if (instance)
54+
return instance;
55+
56+
if (!userContainerOptions || !userContainerOptions.fallback)
57+
return instance;
58+
59+
} catch (error) {
60+
if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)
61+
throw error;
62+
}
63+
}
64+
return defaultContainer.get<T>(someClass);
65+
}

test/fakes/global-options/SessionMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class SessionMiddleware implements ExpressMiddlewareInterface {
1616
}
1717
}
1818

19-
private expSession = session({
19+
private expSession = (session as any)({
2020
secret: '19majkel94_helps_pleerock',
2121
resave: false,
2222
saveUninitialized: true,

test/functional/action-options.spec.ts

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import { JsonController } from '../../src/decorator/JsonController';
55
import { Post } from '../../src/decorator/Post';
66
import { Body } from '../../src/decorator/Body';
77
import { createExpressServer, createKoaServer, getMetadataArgsStorage } from '../../src/index';
8-
import { assertRequest } from './test-utils';
9-
const expect = require('chakram').expect;
8+
import { axios } from '../utilities/axios';
9+
import { AxiosError, AxiosResponse } from 'axios';
10+
1011

1112
describe('action options', () => {
1213
let initializedUser: any;
14+
let expressApp: any;
1315
let User: any;
1416

15-
after(() => {
17+
afterAll((done) => {
1618
defaultMetadataStorage.clear();
19+
expressApp.close(done)
1720
});
1821

1922
beforeEach(() => {
2023
initializedUser = undefined;
2124
});
2225

23-
before(() => {
26+
beforeAll((done) => {
2427
// reset metadata args storage
2528
getMetadataArgsStorage().reset();
2629

@@ -58,50 +61,37 @@ describe('action options', () => {
5861
return handler(user);
5962
}
6063
}
61-
});
6264

63-
let expressApp: any, koaApp: any;
64-
before(done => (expressApp = createExpressServer().listen(3001, done)));
65-
after(done => expressApp.close(done));
66-
before(done => (koaApp = createKoaServer().listen(3002, done)));
67-
after(done => koaApp.close(done));
65+
expressApp = createExpressServer().listen(3001, done)
66+
});
6867

6968
it('should use controller options when action transform options are not set', () => {
70-
assertRequest([3001, 3002], 'post', 'default', { firstName: 'Umed', lastName: 'Khudoiberdiev' }, response => {
71-
expect(initializedUser).to.be.instanceOf(User);
72-
expect(initializedUser.lastName).to.be.undefined;
73-
expect(response).to.have.status(200);
74-
expect(response.body.lastName).to.equal('default');
69+
expect.assertions(4);
70+
return axios.post('/default', { firstName: 'Umed', lastName: 'Khudoiberdiev' }).then((response: AxiosResponse) => {
71+
expect(initializedUser).toBeInstanceOf(User);
72+
expect(initializedUser.lastName).toBeUndefined();
73+
expect(response.status).toBe(200);
74+
expect(response.data.lastName).toBe('default');
7575
});
7676
});
7777

7878
it('should override controller options with action transformRequest option', () => {
79-
assertRequest(
80-
[3001, 3002],
81-
'post',
82-
'transformRequestOnly',
83-
{ firstName: 'Umed', lastName: 'Khudoiberdiev' },
84-
response => {
85-
expect(initializedUser).to.be.instanceOf(User);
86-
expect(initializedUser.lastName).to.be.undefined;
87-
expect(response).to.have.status(200);
88-
expect(response.body.lastName).to.equal('default');
89-
}
90-
);
79+
expect.assertions(4);
80+
return axios.post('/transformRequestOnly', { firstName: 'Umed', lastName: 'Khudoiberdiev' }).then((response: AxiosResponse) => {
81+
expect(initializedUser).toBeInstanceOf(User);
82+
expect(initializedUser.lastName).toBeUndefined();
83+
expect(response.status).toBe(200);
84+
expect(response.data.lastName).toBe('default');
85+
});
9186
});
9287

9388
it('should override controller options with action transformResponse option', () => {
94-
assertRequest(
95-
[3001, 3002],
96-
'post',
97-
'transformResponseOnly',
98-
{ firstName: 'Umed', lastName: 'Khudoiberdiev' },
99-
response => {
100-
expect(initializedUser).not.to.be.instanceOf(User);
101-
expect(initializedUser.lastName).to.exist;
102-
expect(response).to.have.status(200);
103-
expect(response.body.lastName).to.be.undefined;
104-
}
105-
);
89+
expect.assertions(4);
90+
return axios.post('/transformResponseOnly', { firstName: 'Umed', lastName: 'Khudoiberdiev' }).then((response: AxiosResponse) => {
91+
expect(initializedUser).not.toBeInstanceOf(User);
92+
expect(initializedUser.lastName).not.toBeUndefined();
93+
expect(response.status).toBe(200);
94+
expect(response.data.lastName).toBeUndefined();
95+
});
10696
});
10797
});

0 commit comments

Comments
 (0)