Skip to content

Schematics: Use MockStore in container generated tests #2028

@jtcrowson

Description

@jtcrowson

The test file generated by the container schematic is currently importing the StoreModule:

// ...
import { Store, StoreModule } from '@ngrx/store';

describe('ExampleComponent', () => {
  // ...
  let store: Store<any>;

  beforeEach(async() => {
    TestBed.configureTestingModule({
      imports: [ StoreModule.forRoot({}) ], // <--- Outdated
      declarations: [ ExampleComponent ]
    });

    await TestBed.compileComponents();
  });

  beforeEach(() => {
    // ...
    store = TestBed.get<Store>(Store);

    spyOn(store, 'dispatch').and.callThrough();
    fixture.detectChanges();
  });
  // ...
});

Instead, the schematic should use MockStore:

// ...
import { provideMockStore, MockStore } from '@ngrx/store/testing';

describe('ExampleComponent', () => {
  // ...
  let store: MockStore<any>;

  beforeEach(async() => {
    TestBed.configureTestingModule({
      providers: [ provideMockStore() ],
      declarations: [ ExampleComponent ]
    });

    await TestBed.compileComponents();
  });

  beforeEach(() => {
    // ...
    store = TestBed.get(Store);
    fixture.detectChanges();
  });
  // ...
});

If accepted, I would be willing to submit a PR for this feature

[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions