Last active
March 29, 2018 13:59
-
-
Save benjamincharity/bc425c7205cacace0fa9c9ba11688e4a to your computer and use it in GitHub Desktop.
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
import { Action, Store } from '@ngrx/store'; | |
import { Subject } from 'rxjs/Subject'; | |
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
/** | |
* Standard mockstore that can be used in unittests to mock a @ngrx/store | |
* | |
* https://github.com/ngrx/store/issues/128#issuecomment-316654714 | |
*/ | |
export class MockStore<T> { | |
reducers = new Map<string, BehaviorSubject<any>>(); | |
/** | |
* Simple solution to support selecting/subscribing to this mockstore as usual. | |
* | |
* @param name - Reducer name | |
* @returns {undefined|BehaviorSubject<any>} | |
*/ | |
select(name) { | |
if (!this.reducers.has(name)) { | |
this.reducers.set(name, new BehaviorSubject({})); | |
} | |
return this.reducers.get(name); | |
} | |
/** | |
* Used to set a fake state | |
* | |
* @param reducerName - The name of your reducer | |
* @param data - The mock data | |
*/ | |
mockState(reducerName, data) { | |
this.select(reducerName).next(data); | |
} | |
dispatch(data: any) { | |
// ... | |
} | |
} |
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
export interface UserArgs { | |
id: number; | |
firstName: string; | |
lastName: string; | |
} | |
export class User { | |
public firstName: string; | |
public lastName: string; | |
public id: number; | |
constructor({firstName, lastName, id}: UserArgs) { | |
this.id = id; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
get fullName() { | |
return `${this.firstName} ${this.lastName}`; | |
} | |
} | |
const userMock = new User({firstName: 'foo', lastName: 'bar', id: 10}); | |
describe(`getStateValue`, () => { | |
let storeMock; | |
beforeEach(() => { | |
storeMock = new MockStore(); | |
storeMock.mockState('users', [userMock]); | |
}); | |
test(`should return the value of the store`, () => { | |
storeMock.select('users').subscribe((v) => { | |
expect(v).toEqual([userMock]); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment