Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit e3d9837

Browse files
author
Chau Tran
committed
fix(automapper.module.ts): fix test and core
1 parent a3e30c9 commit e3d9837

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

src/automapper.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class AutomapperModule {
2222
const mapper = new AutoMapper();
2323

2424
options && options.config && mapper.initialize(options.config);
25-
const providers = forRootProviders(options);
25+
const providers = forRootProviders(mapper, options);
2626

2727
return {
2828
module: AutomapperModule,
@@ -38,7 +38,7 @@ export class AutomapperModule {
3838
* @param {AutomapperModuleFeatureOptions} options
3939
*/
4040
static forFeature(options: AutomapperModuleFeatureOptions): DynamicModule {
41-
if (!options || (options && !options.profiles)) {
41+
if (!options || (options && !options.profiles.length)) {
4242
const message = 'AutomapperModuleFeatureOptions.profiles is empty';
4343
this.logger.error(message);
4444
throw new Error(message);

src/automapper.provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { getMapperToken } from './utils/getMapperToken';
88
import { MAPPER_MAP, MapperMap } from './utils/mapperMap';
99

1010
export const forRootProviders = (
11+
mapper: AutoMapper,
1112
options?: AutomapperModuleRootOptions
1213
): Provider[] => {
13-
const mapper = new AutoMapper();
1414
const token = getMapperToken(options ? options.name : '');
1515
!MapperMap.has(token) && MapperMap.set(token, mapper);
1616

test/automapper.test.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,69 @@ class MockProfile extends MappingProfileBase {
3434
})
3535
class MockModule {}
3636

37+
@Module({
38+
imports: [
39+
AutomapperModule.forRoot({
40+
config: cfg => {
41+
cfg.addProfile(new MockProfile());
42+
},
43+
}),
44+
],
45+
})
46+
class MockModuleWithConfig {}
47+
3748
@Module({
3849
imports: [AutomapperModule.forFeature({ profiles: [new MockProfile()] })],
3950
})
4051
class MockSubModule {}
4152

42-
describe('AutoMapper test', () => {
53+
describe('AutomapperModule - with config', () => {
54+
let moduleFixture: TestingModule;
55+
let mapper: AutoMapper;
56+
let mapperMap: Map<string, AutoMapper>;
57+
58+
beforeAll(async () => {
59+
moduleFixture = await Test.createTestingModule({
60+
imports: [MockModuleWithConfig],
61+
}).compile();
62+
mapperMap = moduleFixture.get<Map<string, AutoMapper>>(MAPPER_MAP);
63+
mapper = moduleFixture.get<AutoMapper>(getMapperToken());
64+
});
65+
66+
afterAll(() => {
67+
mapper.dispose();
68+
});
69+
70+
it('AutomapperModule has been initialized with config', () => {
71+
expect(mapperMap.size).toBeGreaterThan(0);
72+
expect(mapper).toBeTruthy();
73+
expect(JSON.stringify(mapper)).toEqual(
74+
JSON.stringify(mapperMap.get(getMapperToken()))
75+
);
76+
});
77+
78+
it('AutomapperModule - map with config', () => {
79+
const _mock = new Mock();
80+
_mock.foo = 'baz';
81+
82+
const vm = mapper.map(_mock, MockVm);
83+
expect(vm).toBeTruthy();
84+
expect(vm.bar).toEqual(_mock.foo);
85+
expect(vm).toBeInstanceOf(MockVm);
86+
});
87+
88+
it('AutomapperModule - reverseMap with config', () => {
89+
const _mockVm = new MockVm();
90+
_mockVm.bar = 'should be foo';
91+
92+
const _mock = mapper.map(_mockVm, Mock);
93+
expect(_mock).toBeTruthy();
94+
expect(_mock).toBeInstanceOf(Mock);
95+
expect(_mock.foo).toEqual(_mockVm.bar);
96+
});
97+
});
98+
99+
describe('AutoMapperModuke', () => {
43100
let moduleFixture: TestingModule;
44101
let mapper: AutoMapper;
45102
let mapperMap: Map<string, AutoMapper>;

0 commit comments

Comments
 (0)