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

Commit

Permalink
fix(automapper.module.ts): fix test and core
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Nov 6, 2019
1 parent a3e30c9 commit e3d9837
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/automapper.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class AutomapperModule {
const mapper = new AutoMapper();

options && options.config && mapper.initialize(options.config);
const providers = forRootProviders(options);
const providers = forRootProviders(mapper, options);

return {
module: AutomapperModule,
Expand All @@ -38,7 +38,7 @@ export class AutomapperModule {
* @param {AutomapperModuleFeatureOptions} options
*/
static forFeature(options: AutomapperModuleFeatureOptions): DynamicModule {
if (!options || (options && !options.profiles)) {
if (!options || (options && !options.profiles.length)) {
const message = 'AutomapperModuleFeatureOptions.profiles is empty';
this.logger.error(message);
throw new Error(message);
Expand Down
2 changes: 1 addition & 1 deletion src/automapper.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { getMapperToken } from './utils/getMapperToken';
import { MAPPER_MAP, MapperMap } from './utils/mapperMap';

export const forRootProviders = (
mapper: AutoMapper,
options?: AutomapperModuleRootOptions
): Provider[] => {
const mapper = new AutoMapper();
const token = getMapperToken(options ? options.name : '');
!MapperMap.has(token) && MapperMap.set(token, mapper);

Expand Down
59 changes: 58 additions & 1 deletion test/automapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,69 @@ class MockProfile extends MappingProfileBase {
})
class MockModule {}

@Module({
imports: [
AutomapperModule.forRoot({
config: cfg => {
cfg.addProfile(new MockProfile());
},
}),
],
})
class MockModuleWithConfig {}

@Module({
imports: [AutomapperModule.forFeature({ profiles: [new MockProfile()] })],
})
class MockSubModule {}

describe('AutoMapper test', () => {
describe('AutomapperModule - with config', () => {
let moduleFixture: TestingModule;
let mapper: AutoMapper;
let mapperMap: Map<string, AutoMapper>;

beforeAll(async () => {
moduleFixture = await Test.createTestingModule({
imports: [MockModuleWithConfig],
}).compile();
mapperMap = moduleFixture.get<Map<string, AutoMapper>>(MAPPER_MAP);
mapper = moduleFixture.get<AutoMapper>(getMapperToken());
});

afterAll(() => {
mapper.dispose();
});

it('AutomapperModule has been initialized with config', () => {
expect(mapperMap.size).toBeGreaterThan(0);
expect(mapper).toBeTruthy();
expect(JSON.stringify(mapper)).toEqual(
JSON.stringify(mapperMap.get(getMapperToken()))
);
});

it('AutomapperModule - map with config', () => {
const _mock = new Mock();
_mock.foo = 'baz';

const vm = mapper.map(_mock, MockVm);
expect(vm).toBeTruthy();
expect(vm.bar).toEqual(_mock.foo);
expect(vm).toBeInstanceOf(MockVm);
});

it('AutomapperModule - reverseMap with config', () => {
const _mockVm = new MockVm();
_mockVm.bar = 'should be foo';

const _mock = mapper.map(_mockVm, Mock);
expect(_mock).toBeTruthy();
expect(_mock).toBeInstanceOf(Mock);
expect(_mock.foo).toEqual(_mockVm.bar);
});
});

describe('AutoMapperModuke', () => {
let moduleFixture: TestingModule;
let mapper: AutoMapper;
let mapperMap: Map<string, AutoMapper>;
Expand Down

0 comments on commit e3d9837

Please sign in to comment.