Example of how to partially seed the store prior to a test. #ngrx
interface MinimumStoreRequirments {
userStore: UserStoreState;
marketerStore: MarketerStoreState;
currentUserStore: CurrentUserState;
}
describe('GlobalHeaderContainerComponent', () => {
const userStoreMock: UserStoreState = {
users: {},
};
const currentUserStoreMock: CurrentUserState = {
state: 'unloaded',
user_id: null,
};
const marketerStoreMock: MarketerStoreState = {
marketers: {},
};
let users: UserStoreState;
let user: CurrentUserState;
let marketer: MarketerStoreState;
beforeEach(async(() => {
users = userStoreMock;
user = currentUserStoreMock;
marketer = marketerStoreMock;
// This returns a store object that meets the combination requirement of our minimum needed
// state and the original AppState
this.buildStore = (): MinimumStoreRequirments | AppState => {
const store: MinimumStoreRequirments | AppState = <MinimumStoreRequirments | AppState>{};
store.userStore = users;
store.marketerStore = marketer;
store.currentUserStore = user;
return store;
}
// The mock store that returns the result of `buildStore`
const StoreMock = {
dispatch: jasmine.createSpy('dispatch'),
select: jasmine.createSpy('select').and.callFake((selector: any) => {
return Observable.of(selector(this.buildStore()));
}),
};
TestBed.configureTestingModule({
declarations: [
GlobalHeaderContainerComponent,
],
providers: [
{
provide: Store,
useValue: StoreMock,
},
],
})
.overrideComponent(GlobalHeaderContainerComponent, {
set: {
template: '',
}
})
;
this.buildComponent = () => {
this.fixture = TestBed.createComponent(GlobalHeaderContainerComponent);
this.component = this.fixture.componentInstance;
this.store = TestBed.get(Store);
this.fixture.detectChanges();
}
}));
it(`should be created`, () => {
this.buildComponent();
expect(this.component).toBeTruthy();
});
describe(`currentUser$`, () => {
const currentUser = new User({
id: 123,
marketer_id: 1,
firstname: 'a',
lastname: 'b',
superadmin: false,
email: 'a',
});
beforeEach(() => {
user = {state: 'loaded', user_id: currentUser.id};
users = {users: {[currentUser.id]: currentUser}};
this.buildComponent();
});
it(`should return the current user`, () => {
this.component.currentUser$.subscribe((returnUser: any) => {
expect(returnUser).toEqual(currentUser);
});
});
});
});