Testing ObservableMedia with ReflectiveInjector
I recently moved ObservableMedia from component to a service and I had some trouble setting up tests with ReflectiveInjector. In component tests (with TestBed) test setup was easy:
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [FlexLayoutModule],
providers: [MockMatchMediaProvider]
}).compileComponents();
TestBed.get(MatchMedia).activate('xs');
TestBed.get(ObservableMedia).asObservable().take(1)
.do((o: any) => expect(service.set).toHaveBeenCalledWith('media', jasmine.any(MediaChange)))
.subscribe(null, null, done);
But with service (where I use ReflectiveInjector), things are kinda nuts with providers (;
import {
BreakPointRegistry,
DEFAULT_BREAKPOINTS_PROVIDER,
MatchMedia,
MediaChange,
OBSERVABLE_MEDIA_PROVIDER,
ObservableMedia
} from '@angular/flex-layout';
import { MockMatchMediaProvider } from '@angular/flex-layout/media-query/mock/mock-match-media';
describe('ObservableMedia', () => {
let injector: ReflectiveInjector;
let service: UIService;
let store: Store<AppState>;
function setup() {
injector = ReflectiveInjector.resolveAndCreate([
BreakPointRegistry,
DEFAULT_BREAKPOINTS_PROVIDER,
OBSERVABLE_MEDIA_PROVIDER,
MockMatchMediaProvider,
// { provide: ObservableMedia, useClass: MediaService },
// { provide: MatchMedia, useClass: MockMatchMedia },
{ provide: NgZone, useValue: new NgZone({}) },
StoreModule.provideStore({ ui: uiReducer }).providers,
UIService
]);
service = injector.get(UIService);
store = injector.get(Store);
spyOn(service, 'update');
spyOn(store, 'dispatch');
};
beforeEach(() => setup());
it('should update store with "MediaChange" when media query is activated', (done) => {
const matchMedia = injector.get(MatchMedia);
const mediaService = injector.get(ObservableMedia);
matchMedia.activate('xs');
mediaService.asObservable().take(1)
.do((o: any) => expect(service.set).toHaveBeenCalledWith('media', jasmine.any(MediaChange)))
.do((o: any) => expect(store.dispatch).toHaveBeenCalledWith(jasmine.any(UpdateUIAction)))
.subscribe(null, null, done);
});
});
I tried using
FlexLayoutModule.provideBreakPoints(DEFAULT_BREAKPOINTS).providers
, but that only replaces DEFAULT_BREAKPOINTS_PROVIDER it doesn't resolve other providers that are needed...
It would be nice to have easier setup, maybe if providers were exported from module:
const myBreakpoints: BreakPoint[] = [];
injector = ReflectiveInjector.resolveAndCreate([
FlexLayoutModule.provideBreakPoints(myBreakpoints).providers,
MockMatchMediaProvider,
UIService
]);
ReflectiveInjector was deprecated in Angular 5, which is what Flex Layout supports (unlike core, we don't have an LTS yet). Closing for now unless there is still a huge community need for this.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.