122 lines
3.9 KiB
TypeScript
122 lines
3.9 KiB
TypeScript
import { DefaultParams } from '@modules/ad/core/application/ports/default-params.type';
|
|
import { DefaultParamsProvider } from '@modules/ad/infrastructure/default-params-provider';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
const mockConfigServiceWithDefaults = {
|
|
get: jest.fn().mockImplementation((value: string) => {
|
|
switch (value) {
|
|
case 'DEPARTURE_TIME_MARGIN':
|
|
return 600;
|
|
case 'ROLE':
|
|
return 'passenger';
|
|
case 'SEATS_PROPOSED':
|
|
return 2;
|
|
case 'SEATS_REQUESTED':
|
|
return 1;
|
|
case 'STRICT_FREQUENCY':
|
|
return 'false';
|
|
case 'TIMEZONE':
|
|
return 'Europe/Paris';
|
|
case 'ALGORITHM_TYPE':
|
|
return 'PASSENGER_ORIENTED';
|
|
case 'REMOTENESS':
|
|
return 10000;
|
|
case 'USE_PROPORTION':
|
|
return 'true';
|
|
case 'PROPORTION':
|
|
return 0.4;
|
|
case 'USE_AZIMUTH':
|
|
return 'true';
|
|
case 'AZIMUTH_MARGIN':
|
|
return 15;
|
|
case 'MAX_DETOUR_DISTANCE_RATIO':
|
|
return 0.5;
|
|
case 'MAX_DETOUR_DURATION_RATIO':
|
|
return 0.6;
|
|
case 'PER_PAGE':
|
|
return 15;
|
|
default:
|
|
return 'some_default_value';
|
|
}
|
|
}),
|
|
};
|
|
|
|
const mockConfigServiceWithoutDefaults = {
|
|
get: jest.fn(),
|
|
};
|
|
|
|
describe('DefaultParamsProvider', () => {
|
|
let defaultParamsProviderWithDefaults: DefaultParamsProvider;
|
|
let defaultParamsProviderWithoutDefaults: DefaultParamsProvider;
|
|
|
|
beforeAll(async () => {
|
|
const moduleWithDefaults: TestingModule = await Test.createTestingModule({
|
|
imports: [],
|
|
providers: [
|
|
DefaultParamsProvider,
|
|
{
|
|
provide: ConfigService,
|
|
useValue: mockConfigServiceWithDefaults,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
defaultParamsProviderWithDefaults =
|
|
moduleWithDefaults.get<DefaultParamsProvider>(DefaultParamsProvider);
|
|
|
|
const moduleWithoutDefault: TestingModule = await Test.createTestingModule({
|
|
imports: [],
|
|
providers: [
|
|
DefaultParamsProvider,
|
|
{
|
|
provide: ConfigService,
|
|
useValue: mockConfigServiceWithoutDefaults,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
defaultParamsProviderWithoutDefaults =
|
|
moduleWithoutDefault.get<DefaultParamsProvider>(DefaultParamsProvider);
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(defaultParamsProviderWithDefaults).toBeDefined();
|
|
});
|
|
|
|
it('should provide default params if defaults are set', async () => {
|
|
const params: DefaultParams = defaultParamsProviderWithDefaults.getParams();
|
|
expect(params.DEPARTURE_TIME_MARGIN).toBe(600);
|
|
expect(params.PASSENGER).toBeTruthy();
|
|
expect(params.DRIVER).toBeFalsy();
|
|
expect(params.TIMEZONE).toBe('Europe/Paris');
|
|
expect(params.ALGORITHM_TYPE).toBe('PASSENGER_ORIENTED');
|
|
expect(params.REMOTENESS).toBe(10000);
|
|
expect(params.USE_PROPORTION).toBeTruthy();
|
|
expect(params.PROPORTION).toBe(0.4);
|
|
expect(params.USE_AZIMUTH).toBeTruthy();
|
|
expect(params.AZIMUTH_MARGIN).toBe(15);
|
|
expect(params.MAX_DETOUR_DISTANCE_RATIO).toBe(0.5);
|
|
expect(params.MAX_DETOUR_DURATION_RATIO).toBe(0.6);
|
|
expect(params.PER_PAGE).toBe(15);
|
|
});
|
|
|
|
it('should provide default params if defaults are not set', async () => {
|
|
const params: DefaultParams =
|
|
defaultParamsProviderWithoutDefaults.getParams();
|
|
expect(params.DEPARTURE_TIME_MARGIN).toBe(900);
|
|
expect(params.PASSENGER).toBeTruthy();
|
|
expect(params.DRIVER).toBeFalsy();
|
|
expect(params.TIMEZONE).toBe('Europe/Paris');
|
|
expect(params.ALGORITHM_TYPE).toBe('PASSENGER_ORIENTED');
|
|
expect(params.REMOTENESS).toBe(15000);
|
|
expect(params.USE_PROPORTION).toBeTruthy();
|
|
expect(params.PROPORTION).toBe(0.3);
|
|
expect(params.USE_AZIMUTH).toBeTruthy();
|
|
expect(params.AZIMUTH_MARGIN).toBe(10);
|
|
expect(params.MAX_DETOUR_DISTANCE_RATIO).toBe(0.3);
|
|
expect(params.MAX_DETOUR_DURATION_RATIO).toBe(0.3);
|
|
expect(params.PER_PAGE).toBe(10);
|
|
});
|
|
});
|