Skip to content

Commit

Permalink
fix: respecting internals vs externals
Browse files Browse the repository at this point in the history
BREAKING CHANGE: respects internals vs externals, to access them use guts or MockBuilder

closes #44
  • Loading branch information
satanTime committed Nov 14, 2020
1 parent 2625352 commit d4abf41
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/common/ng-mocks-universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ getGlobal().ngMocksUniverse = getGlobal().ngMocksUniverse || {
cacheDeclarations: new Map(),
cacheProviders: new Map(),
config: new Map(),
flags: new Set<string>(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider']),
flags: new Set<string>(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider', 'correctModuleExports']),
global: new Map(),
touches: new Set<AnyType<any> | InjectionToken<any>>(),
};
Expand Down
8 changes: 7 additions & 1 deletion lib/mock-helper/mock-helper.reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export default (): void => {
ngMocksUniverse.cacheProviders = new Map();
ngMocksUniverse.config = new Map();
ngMocksUniverse.global = new Map();
ngMocksUniverse.flags = new Set(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider']);
ngMocksUniverse.flags = new Set([
'cacheModule',
'cacheComponent',
'cacheDirective',
'cacheProvider',
'correctModuleExports',
]);
ngMocksUniverse.touches = new Set();
};
3 changes: 3 additions & 0 deletions lib/mock-module/mock-module.spec.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class ExampleConsumerComponent {}
export class ChildModule {}

@NgModule({
exports: [ChildModule],
imports: [ChildModule],
})
export class ParentModule {}
Expand All @@ -69,11 +70,13 @@ export class SameImports1Module {}
export class SameImports2Module {}

@NgModule({
exports: [ChildModule],
imports: [ChildModule],
})
export class LogicNestedModule {}

@NgModule({
exports: [ChildModule, LogicNestedModule],
imports: [ChildModule, LogicNestedModule],
})
export class LogicRootModule {}
Expand Down
75 changes: 75 additions & 0 deletions tests/correct-module-exports-11/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// tslint:disable:no-console

import { Component, NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockBuilder, MockModule, MockRender, ngMocks } from 'ng-mocks';

@Component({
selector: 'internal',
template: 'internal',
})
class InternalComponent {}

@Component({
selector: 'external',
template: 'external',
})
class ExternalComponent {}

@NgModule({
declarations: [InternalComponent, ExternalComponent],
exports: [ExternalComponent],
})
class TargetModule {}

describe('correct-module-exports-11:proper', () => {
// Thanks Ivy, it doesn't throw an error.
let backupWarn: typeof console.warn;
let backupError: typeof console.error;

beforeAll(() => {
backupWarn = console.warn;
backupError = console.error;
console.error = console.warn = (...args: any[]) => {
throw new Error(args.join(' '));
};
});

afterAll(() => {
console.error = backupError;
console.warn = backupWarn;
});

beforeEach(() =>
TestBed.configureTestingModule({
imports: [MockModule(TargetModule)],
}).compileComponents()
);

it('fails on not exported module', () => {
expect(() => MockRender(InternalComponent)).toThrowError(/'internal' is not a known element/);
});

it('renders an exported module', () => {
const fixture = MockRender(ExternalComponent);
expect(fixture.nativeElement.innerHTML).toEqual('<external></external>');
});
});

describe('correct-module-exports-11:guts', () => {
beforeEach(() => TestBed.configureTestingModule(ngMocks.guts(null, TargetModule)).compileComponents());

it('renders an internal module', () => {
const fixture = MockRender(InternalComponent);
expect(fixture.nativeElement.innerHTML).toEqual('<internal></internal>');
});
});

describe('correct-module-exports-11:builder', () => {
beforeEach(() => MockBuilder(null, TargetModule));

it('renders an internal module', () => {
const fixture = MockRender(InternalComponent);
expect(fixture.nativeElement.innerHTML).toEqual('<internal></internal>');
});
});
1 change: 1 addition & 0 deletions tests/issue-222/common-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TargetComponent {}

@NgModule({
declarations: [TargetComponent],
exports: [TargetComponent],
imports: [CommonModule],
})
class TargetModule {}
Expand Down

0 comments on commit d4abf41

Please sign in to comment.