Open vitalii4reva opened 8 years ago
Hmm, this seems to be an issue with the esm-code included in the npm package. Do you have any example code that crashes so I can investigate?
Sure.
import { TestBed, inject } from '@angular/core/testing';
import { Router, NavigationStart } from '@angular/router';
import {
NgRedux,
DevToolsExtension
} from 'ng2-redux';
import { NgReduxRouter } from 'ng2-redux-router';
import { AppComponent } from './app.component';
import { UserSessionActions } from './store/actions/user-session.actions';
import { RouterActions } from './store/actions/router.actions';
import { MiddlewaresProvider } from './store';
import { LoginService } from './shared/services/login.service';
import { LOGIN_ROUTE } from './app.routes';
class UserSessionActionsMock {
public restoreToken = jasmine.createSpy('restoreToken');
};
let reduxMapSpy;
class NgReduxMock {
public select = jasmine.createSpy('select').and.callFake((param: string): any => {
if (param === 'sidebar') {
return {
map(cb) {
reduxMapSpy();
cb({ isVisible: true });
}
};
}
return {
subscribe(cb) {
cb('route');
}
};
});
public configureStore = jasmine.createSpy('configureStore');
public dispatch = jasmine.createSpy('dispatch');
}
class DevToolsExtensionMock {
public isEnabled = jasmine.createSpy('isEnabled').and.callFake(() => {
return true;
});
public enhancer = jasmine.createSpy('enhancer');
}
class MiddlewaresProviderMock {
public getMiddlewares = jasmine.createSpy('getMiddlewares');
}
class NgReduxRouterMock {
public dispatch = jasmine.createSpy('dispatch');
public initialize = jasmine.createSpy('initialize');
}
class RouterActionsMock {
public updateLocation = jasmine.createSpy('updateLocation');
}
class RouterMock {
public navigate = jasmine.createSpy('navigate');
public events = {
filter(eventFilterCb) {
eventFilterCb(new NavigationStart(1, '/login'));
return {
flatMap(flatMapCb) {
return {
subscribe(subscribeCb) {
subscribeCb(flatMapCb());
}
};
}
};
}
};
};
let isLoggedIn = false;
class LoginServiceMock {
public get isLoggedIn() {
return isLoggedIn;
}
}
describe('AppComponent', () => {
let userSessionActionsMock: UserSessionActionsMock;
let ngReduxMock: NgReduxMock;
let ngReduxRouterMock: NgReduxRouterMock;
let routerMock: RouterMock;
let devToolsExtensionMock: DevToolsExtensionMock;
let middlewaresProviderMock: MiddlewaresProviderMock;
let routerActionsMock: RouterActionsMock;
let loginServiceMock: LoginServiceMock;
// Set up providers
beforeEach(() => {
userSessionActionsMock = new UserSessionActionsMock();
ngReduxMock = new NgReduxMock();
ngReduxRouterMock = new NgReduxRouterMock();
routerMock = new RouterMock();
devToolsExtensionMock = new DevToolsExtensionMock();
middlewaresProviderMock = new MiddlewaresProviderMock();
routerActionsMock = new RouterActionsMock();
loginServiceMock = new LoginServiceMock();
reduxMapSpy = jasmine.createSpy('reduxMapSpy');
TestBed.configureTestingModule({
providers: [
AppComponent,
{ provide: UserSessionActions, useValue: userSessionActionsMock },
{ provide: NgRedux, useValue: ngReduxMock },
{ provide: NgReduxRouter, useValue: ngReduxRouterMock },
{ provide: Router, useValue: routerMock },
{ provide: DevToolsExtension, useValue: devToolsExtensionMock },
{ provide: MiddlewaresProvider, useValue: middlewaresProviderMock },
{ provide: RouterActions, useValue: routerActionsMock },
{ provide: LoginService, useValue: loginServiceMock },
]
});
});
// Tests
it('check if userSession and loader properties have been set',
inject([AppComponent], appComponent => {
expect(ngReduxMock.select).toHaveBeenCalledWith('sidebar');
expect(reduxMapSpy).toHaveBeenCalled();
expect(ngReduxMock.select).toHaveBeenCalledWith('loader');
expect(ngReduxMock.configureStore).toHaveBeenCalled();
expect(middlewaresProviderMock.getMiddlewares).toHaveBeenCalled();
expect(devToolsExtensionMock.isEnabled).toHaveBeenCalled();
expect(devToolsExtensionMock.enhancer).toHaveBeenCalled();
expect(ngReduxRouterMock.initialize).toHaveBeenCalled();
expect(ngReduxMock.select).toHaveBeenCalledWith('router');
expect(routerMock.navigate).toHaveBeenCalledWith(['route']);
expect(userSessionActionsMock.restoreToken).toHaveBeenCalled();
})
);
it('test "ngOnInit" function for case when isLoggedIn is true',
inject([AppComponent], appComponent => {
isLoggedIn = true;
appComponent.ngOnInit();
expect(routerActionsMock.updateLocation).not.toHaveBeenCalled();
})
);
it('test "ngOnInit" function for case when isLoggedIn is false',
inject([AppComponent], appComponent => {
isLoggedIn = false;
appComponent.ngOnInit();
expect(routerActionsMock.updateLocation).toHaveBeenCalledWith(LOGIN_ROUTE);
})
);
});
Hello.
I have an issue while running tests.
npm run test
Can you help me with it ?