-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MockInstance): console.warn on forgotten resets #857
- Loading branch information
Showing
10 changed files
with
159 additions
and
18 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
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,77 @@ | ||
// tslint:disable no-console | ||
|
||
import { Component } from '@angular/core'; | ||
import { | ||
MockBuilder, | ||
MockInstance, | ||
MockRender, | ||
ngMocks, | ||
} from 'ng-mocks'; | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '{{ name }}', | ||
}) | ||
class TargetComponent { | ||
public readonly name?: string = 'target'; | ||
} | ||
|
||
describe('issue-857', () => { | ||
beforeEach(() => MockBuilder(null, TargetComponent)); | ||
|
||
describe('throws on forgotten resets', () => { | ||
let consoleWarn; | ||
beforeAll(() => { | ||
consoleWarn = console.warn; | ||
console.warn = | ||
typeof jest === 'undefined' | ||
? jasmine.createSpy('console.warn') | ||
: jest.fn().mockName('console.warn'); | ||
|
||
ngMocks.config({ | ||
onMockInstanceRestoreNeed: 'warn', | ||
}); | ||
}); | ||
afterAll(() => { | ||
MockInstance(TargetComponent); | ||
console.warn = consoleWarn; | ||
|
||
ngMocks.config({ | ||
onMockInstanceRestoreNeed: null, | ||
}); | ||
}); | ||
|
||
it('internal override', () => { | ||
MockInstance(TargetComponent, 'name', 'mock'); | ||
const instance = | ||
MockRender(TargetComponent).point.componentInstance; | ||
expect(instance.name).toEqual('mock'); | ||
}); | ||
|
||
it('default override', () => { | ||
expect(console.warn).toHaveBeenCalledWith( | ||
[ | ||
'MockInstance: side effects have been detected (TargetComponent).', | ||
'Forgot to add MockInstance.scope() or to call MockInstance.restore()?', | ||
].join(' '), | ||
); | ||
}); | ||
}); | ||
|
||
describe('respects valid resets', () => { | ||
MockInstance.scope(); | ||
|
||
it('internal override', () => { | ||
MockInstance(TargetComponent, 'name', 'mock'); | ||
const instance = | ||
MockRender(TargetComponent).point.componentInstance; | ||
expect(instance.name).toEqual('mock'); | ||
}); | ||
|
||
it('default override', () => { | ||
const instance = | ||
MockRender(TargetComponent).point.componentInstance; | ||
expect(instance.name).toEqual(undefined); | ||
}); | ||
}); | ||
}); |
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
28 changes: 28 additions & 0 deletions
28
libs/ng-mocks/src/lib/mock-instance/mock-instance-forgot-reset.ts
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,28 @@ | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
|
||
export default (checkReset: Array<[any, any]>) => { | ||
const showError: string[] = []; | ||
|
||
// istanbul ignore next: because of the installed global scope switcher we cannot test this part | ||
while (checkReset.length) { | ||
const [declaration, config] = checkReset.pop() || /* istanbul ignore next */ []; | ||
if (config === ngMocksUniverse.configInstance.get(declaration)) { | ||
showError.push(typeof declaration === 'function' ? declaration.name : declaration); | ||
} | ||
} | ||
|
||
// istanbul ignore if: because of the installed global scope switcher we cannot test this part | ||
if (showError.length) { | ||
const globalFlags = ngMocksUniverse.global.get('flags'); | ||
const errorMessage = [ | ||
`MockInstance: side effects have been detected (${showError.join(', ')}).`, | ||
`Forgot to add MockInstance.scope() or to call MockInstance.restore()?`, | ||
].join(' '); | ||
if (globalFlags.onMockInstanceRestoreNeed === 'warn') { | ||
// tslint:disable-next-line no-console | ||
console.warn(errorMessage); | ||
} else if (globalFlags.onMockInstanceRestoreNeed === 'throw') { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
}; |
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