-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): excluding StoreDevtoolsModule by default #589
- Loading branch information
Showing
9 changed files
with
278 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, NgModule, OnInit } from '@angular/core'; | ||
import { | ||
createAction, | ||
createFeatureSelector, | ||
createReducer, | ||
on, | ||
Store, | ||
StoreModule, | ||
StoreRootModule, | ||
} from '@ngrx/store'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
const increaseValue = createAction('set-value'); | ||
const resetValue = createAction('reset-value'); | ||
|
||
const myReducer = { | ||
featureKey: 'test', | ||
reducer: createReducer( | ||
0, | ||
on(increaseValue, state => state + 1), | ||
on(resetValue, () => 0), | ||
), | ||
}; | ||
|
||
const selectValue = createFeatureSelector(myReducer.featureKey); | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '{{ value$ | async }}', | ||
}) | ||
class MyComponent implements OnInit { | ||
public value$ = this.store.select(selectValue); | ||
|
||
public constructor(private readonly store: Store) {} | ||
|
||
public ngOnInit(): void { | ||
this.store.dispatch(increaseValue()); | ||
} | ||
|
||
public reset(): void { | ||
this.store.dispatch(resetValue()); | ||
} | ||
} | ||
|
||
const metaReducer = state => state; | ||
|
||
@NgModule({ | ||
declarations: [MyComponent], | ||
exports: [MyComponent], | ||
imports: [ | ||
CommonModule, | ||
StoreModule.forRoot( | ||
{ | ||
[myReducer.featureKey]: myReducer.reducer, | ||
}, | ||
{ | ||
metaReducers: [metaReducer], | ||
}, | ||
), | ||
], | ||
}) | ||
class MyModule {} | ||
|
||
describe('issue-589:meta-reducers', () => { | ||
beforeEach(() => | ||
MockBuilder(MyComponent, MyModule).keep(StoreRootModule), | ||
); | ||
|
||
it('keeps meta reducers', () => { | ||
expect(ngMocks.formatText(MockRender(MyComponent))).toEqual('1'); | ||
}); | ||
}); |
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,73 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, NgModule, OnInit } from '@angular/core'; | ||
import { | ||
createAction, | ||
createFeatureSelector, | ||
createReducer, | ||
on, | ||
Store, | ||
StoreModule, | ||
StoreRootModule, | ||
} from '@ngrx/store'; | ||
import { | ||
INITIAL_OPTIONS, | ||
StoreDevtoolsModule, | ||
} from '@ngrx/store-devtools'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
const increaseValue = createAction('set-value'); | ||
const resetValue = createAction('reset-value'); | ||
|
||
const myReducer = { | ||
featureKey: 'test', | ||
reducer: createReducer( | ||
0, | ||
on(increaseValue, state => state + 1), | ||
on(resetValue, () => 0), | ||
), | ||
}; | ||
|
||
const selectValue = createFeatureSelector(myReducer.featureKey); | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '{{ value$ | async }}', | ||
}) | ||
class MyComponent implements OnInit { | ||
public value$ = this.store.select(selectValue); | ||
|
||
public constructor(private readonly store: Store) {} | ||
|
||
public ngOnInit(): void { | ||
this.store.dispatch(increaseValue()); | ||
} | ||
|
||
public reset(): void { | ||
this.store.dispatch(resetValue()); | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [MyComponent], | ||
exports: [MyComponent], | ||
imports: [ | ||
CommonModule, | ||
StoreModule.forRoot({ | ||
[myReducer.featureKey]: myReducer.reducer, | ||
}), | ||
StoreDevtoolsModule.instrument({ | ||
maxAge: 6, | ||
}), | ||
], | ||
}) | ||
class MyModule {} | ||
|
||
describe('issue-589:dev-tools', () => { | ||
beforeEach(() => | ||
MockBuilder(MyComponent, MyModule).keep(StoreRootModule), | ||
); | ||
|
||
it('excludes StoreDevtoolsModule by default', () => { | ||
expect(ngMocks.formatText(MockRender(MyComponent))).toEqual('1'); | ||
}); | ||
}); |
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,61 @@ | ||
import { InjectionToken, NgModule } from '@angular/core'; | ||
import { | ||
MockBuilder, | ||
MockRender, | ||
ngMocks, | ||
NgModuleWithProviders, | ||
} from 'ng-mocks'; | ||
|
||
const TOKEN = new InjectionToken('TOKEN'); | ||
|
||
@NgModule() | ||
class TargetModule { | ||
public static forRoot(): NgModuleWithProviders<TargetModule> { | ||
return { | ||
ngModule: TargetModule, | ||
providers: [ | ||
{ | ||
provide: TOKEN, | ||
useValue: 1, | ||
}, | ||
], | ||
}; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
imports: [TargetModule.forRoot()], | ||
}) | ||
class MyModule {} | ||
|
||
ngMocks.globalExclude(TargetModule); | ||
|
||
// Looks like a module with providers has some issues with excluding it globally | ||
// and then mocking in a mock builder setup. | ||
describe('issue-589', () => { | ||
describe('default exclude', () => { | ||
beforeEach(() => MockBuilder(null, MyModule)); | ||
|
||
it('excludes StoreDevtoolsModule', () => { | ||
expect(() => MockRender(TOKEN)).toThrowError( | ||
/No provider for InjectionToken TOKEN/, | ||
); | ||
}); | ||
}); | ||
|
||
describe('explicit keep', () => { | ||
beforeEach(() => MockBuilder(null, MyModule).keep(TargetModule)); | ||
|
||
it('excludes StoreDevtoolsModule', () => { | ||
expect(MockRender(TOKEN).point.componentInstance).toEqual(1); | ||
}); | ||
}); | ||
|
||
describe('explicit mock', () => { | ||
beforeEach(() => MockBuilder(null, MyModule).mock(TargetModule)); | ||
|
||
it('excludes StoreDevtoolsModule', () => { | ||
expect(MockRender(TOKEN).point.componentInstance).toEqual(0); | ||
}); | ||
}); | ||
}); |