Skip to content

Commit

Permalink
feat(store): rename getMockStore to createMockStore (#3789)
Browse files Browse the repository at this point in the history
Closes #3781
  • Loading branch information
zbarbuto authored Feb 28, 2023
1 parent 9b3647f commit 6d615b3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
6 changes: 3 additions & 3 deletions modules/store/testing/spec/mock_store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { skip, take } from 'rxjs/operators';
import {
getMockStore,
createMockStore,
MockReducerManager,
MockState,
MockStore,
Expand Down Expand Up @@ -410,11 +410,11 @@ describe('Mock Store with Injector', () => {
});
});

describe('getMockStore', () => {
describe('createMockStore', () => {
let mockStore: MockStore<typeof initialState>;

beforeEach(() => {
mockStore = getMockStore({ initialState, selectors: [mockSelector] });
mockStore = createMockStore({ initialState, selectors: [mockSelector] });
});

it('should create MockStore', (done: any) => {
Expand Down
12 changes: 10 additions & 2 deletions modules/store/testing/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ function mockStoreFactory<T>(
);
}

/**
* @deprecated This function is deprecated in favor of `createMockStore`.
* If you are looking to get an existing mock store, you probably want to use `TestBed.inject(MockStore)` instead.
*
* For more info see: https://github.com/ngrx/platform/issues/3781
*/
export const getMockStore = createMockStore;

/**
* @description
* Creates mock store with all necessary dependencies outside of the `TestBed`.
Expand All @@ -141,7 +149,7 @@ function mockStoreFactory<T>(
* let store: MockStore;
*
* beforeEach(() => {
* store = getMockStore({
* store = createMockStore({
* initialState: { books: { entities: ['Book 1', 'Book 2', 'Book 3'] } },
* selectors: [
* { selector: selectAllBooks, value: ['Book 1', 'Book 2'] },
Expand All @@ -152,7 +160,7 @@ function mockStoreFactory<T>(
* });
* ```
*/
export function getMockStore<T>(config: MockStoreConfig<T> = {}): MockStore<T> {
export function createMockStore<T>(config: MockStoreConfig<T> = {}): MockStore<T> {
const injector = Injector.create({ providers: provideMockStore(config) });
return injector.get(MockStore);
}
Expand Down
4 changes: 2 additions & 2 deletions projects/ngrx.io/content/guide/effects/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ it('should get customers', () => {
})
</code-example>

For an Effect with store interaction, use `getMockStore` to create a new instance of `MockStore`.
For an Effect with store interaction, use `createMockStore` to create a new instance of `MockStore`.

<code-example header="my.effects.spec.ts">
it('should get customers', () => {
// create the store, and provide selectors.
const store = getMockStore({
const store = createMockStore({
selectors: [
{ selector: selectCustomers, value: { Bob: { name: 'Bob' } } }
]
Expand Down
14 changes: 14 additions & 0 deletions projects/ngrx.io/content/guide/migration/v15.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ export const usersFeature = createFeature({
});
```

#### Deprecated `getMockStore` in favor of `createMockStore`

BEFORE:

```ts
import { getMockStore } from '@ngrx/store/testing';
const mockStore = getMockStore();
```

```ts
import { createMockStore } from '@ngrx/store/testing';
const mockStore = createMockStore();
```

### @ngrx/router-store

#### Renamed `getSelectors` Function (Introduced in v15.2)
Expand Down
6 changes: 3 additions & 3 deletions projects/ngrx.io/content/guide/store/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ describe('Books Component', () => {
});
</code-example>

Another option to create the `MockStore` without `TestBed` is by calling the `getMockStore()` function:
Another option to create the `MockStore` without `TestBed` is by calling the `createMockStore()` function:

<code-example header="books.component.spec.ts">
import { MockStore, getMockStore } from '@ngrx/store/testing';
import { MockStore, createMockStore } from '@ngrx/store/testing';

describe('Books Component', () => {
let store: MockStore;
const initialState = { books: ['Book 1', 'Book 2', 'Book 3'] };

beforeEach(() => {
store = getMockStore({ initialState });
store = createMockStore({ initialState });
});
});
</code-example>

0 comments on commit 6d615b3

Please sign in to comment.