Как смоделировать store.pipe в компоненте в angular модульном тесте - PullRequest
0 голосов
/ 30 января 2020

Я новичок в angular, поскольку я пытался выполнить модульное тестирование для одного из компонентов, который получает данные из хранилища. Я издевался над сервисными файлами и магазином. Когда я пытаюсь запустить тест, я получаю ошибку, как показано ниже. Поэтому я добавил в хранилище некоторые фальшивые данные и передал данные. Но я все еще получаю ту же проблему

Cannot read property 'switches' of undefined

Spe c file

describe('SwitchListComponent', () => {
  let component: ListComponent;
  let fixture: ComponentFixture<ListComponent>;

  let store: Store<any>;
  const switchData = {
    switches: [
      {
        availability: true,
        created_at: "2020-01-30T05:06:06.366Z",
        description: "1",
        hardware_id: "2",
        id: 1018,
        max_flows: 2,
        name: "2",
        updated_at: "2020-01-30T05:34:36.702Z"
      }
    ],
    pagination: {
      currentPage: 1,
      endAt: 10,
      nextPage: 2,
      perPage: 10,
      prevPage: null,
      startAt: 1,
      totalCount: 1012,
      totalPages: 100
    }
  };


  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        ListComponent,
        MultiSelectComponent,
      ],
      imports: [
        RouterTestingModule,
        FormsModule,
        StoreModule.forRoot({ switchesReducer }),
      ],
      providers: [
        {provide: SwitchesService, useClass: SwitchesServiceStub},
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ListComponent);
    component = fixture.componentInstance;
    store = fixture.debugElement.injector.get(Store);
    store.dispatch({
      type: 'FETCH_SWITCHES',
      payload: {
        switches: switchData.switches,
        pagination: switchData.pagination
      }
    });
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Switch List Component

this.store.pipe(select('switches')).subscribe(val => {
  this.rowData = val.switches;
  this.pagination = val.pagination;
  this.globalSearch = val.globalSearch;
});

Может кто-нибудь сказать мне, как сделать насмешку для this.store.pipe.

1 Ответ

1 голос
/ 30 января 2020

Внутри spe c .ts попробуйте изменить приведенный ниже код с

StoreModule.forRoot({ switchesReducer }),

на

StoreModule.forRoot({ switches: switchesReducer }),

Просьба обратиться к документу за дополнительной информацией

https://ngrx.io/guide/store/testing

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...