-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #575 from satanTime/issues/572
fix(core): a config parameter to suppress MockRender errors #572
- Loading branch information
Showing
9 changed files
with
235 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Component, Directive } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { Type } from '../common/core.types'; | ||
import helperDefinePropertyDescriptor from '../mock-service/helper.define-property-descriptor'; | ||
|
||
import funcGenerateTemplate from './func.generate-template'; | ||
import funcInstallPropReader from './func.install-prop-reader'; | ||
|
||
const generateWrapper = ({ params, options, inputs }: any) => { | ||
class MockRenderComponent { | ||
public constructor() { | ||
if (!params) { | ||
for (const input of inputs || []) { | ||
let value: any = null; | ||
helperDefinePropertyDescriptor(this, input, { | ||
get: () => value, | ||
set: (newValue: any) => (value = newValue), | ||
}); | ||
} | ||
} | ||
funcInstallPropReader(this, params); | ||
} | ||
} | ||
|
||
Component(options)(MockRenderComponent); | ||
TestBed.configureTestingModule({ | ||
declarations: [MockRenderComponent], | ||
}); | ||
|
||
return MockRenderComponent; | ||
}; | ||
|
||
export default (template: any, meta: Directive, params: any, flags: any): Type<any> => { | ||
const mockTemplate = funcGenerateTemplate(template, { ...meta, params }); | ||
const options: Component = { | ||
providers: flags.providers, | ||
selector: 'mock-render', | ||
template: mockTemplate, | ||
}; | ||
|
||
return generateWrapper({ ...meta, params, options }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// tslint:disable no-console | ||
|
||
import { Component, Injector } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: 'target', | ||
}) | ||
class TargetComponent {} | ||
|
||
describe('issue-572', () => { | ||
ngMocks.faster(); | ||
let consoleWarn: typeof console.warn; | ||
|
||
beforeAll(() => MockBuilder(TargetComponent)); | ||
beforeAll(() => (consoleWarn = console.warn)); | ||
|
||
beforeEach(() => { | ||
console.warn = jasmine.createSpy('console.warn'); | ||
}); | ||
|
||
afterAll(() => { | ||
console.warn = consoleWarn; | ||
ngMocks.config({ onTestBedFlushNeed: 'default' }); | ||
}); | ||
|
||
it('throws on TestBed change', () => { | ||
try { | ||
TestBed.get(Injector); | ||
MockRender(TargetComponent); | ||
fail('should throw'); | ||
} catch (e) { | ||
expect(console.warn).not.toHaveBeenCalled(); | ||
expect(e).not.toEqual( | ||
jasmine.objectContaining({ | ||
ngMocksConsoleCatch: jasmine.anything(), | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
it('warns via console on TestBed change', () => { | ||
ngMocks.config({ onTestBedFlushNeed: 'warn' }); | ||
|
||
TestBed.get(Injector); | ||
expect(console.warn).not.toHaveBeenCalled(); | ||
const fixture = MockRender(TargetComponent); | ||
expect(console.warn).toHaveBeenCalled(); | ||
|
||
// renders properly | ||
expect(ngMocks.formatText(fixture)).toEqual('target'); | ||
}); | ||
|
||
it('keeps the config', () => { | ||
ngMocks.config({}); | ||
|
||
TestBed.get(Injector); | ||
expect(console.warn).not.toHaveBeenCalled(); | ||
MockRender(TargetComponent); | ||
expect(console.warn).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// tslint:disable no-console | ||
|
||
import { ngMocks } from 'ng-mocks'; | ||
|
||
describe('ng-mocks-throw-on-console', () => { | ||
ngMocks.throwOnConsole(); | ||
|
||
it('throws on warn', () => { | ||
try { | ||
console.warn('warn message'); | ||
fail('should have failed'); | ||
} catch (e) { | ||
expect(e).toEqual( | ||
jasmine.objectContaining({ | ||
message: 'warn message', | ||
ngMocksConsoleCatch: 'warn', | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
it('throws on error', () => { | ||
try { | ||
console.error('error message'); | ||
fail('should have failed'); | ||
} catch (e) { | ||
expect(e).toEqual( | ||
jasmine.objectContaining({ | ||
message: 'error message', | ||
ngMocksConsoleCatch: 'error', | ||
}), | ||
); | ||
} | ||
}); | ||
}); |