2

I have the below code in the parent.component.ts file, where I'm accessing the child grid as QueryList using @ViewChildren.

@ViewChildren(ChildComponent) childComponent: QueryList<any>;

I have this below function in the component where I accessed the childComponent property like this.

checkProperty() { 
 let isNewRow = this.childComponent[_results].some(
   item => item.newRowCreated === true
 )
 if(isNewRow) { 
  this.newRowEnabled = true;
 }
}

While writing jasmine test for checkProperty() method the mock data which I created for this is throwing error and I'm unable to test this method. Please find the test case code.

it('test checkProperty() method', () => {
  component.childComponent = {
    changes: {},
    first: {},
    last: {},
    length: 1,
    dirty: false,
    _results: [{newRowCreated: true}]
  }
  fixture.detectChanges();
  component.checkProperty();
  expect(component.newRowEnabled).toBe(true);
});
   

This code is throwing error in place where the childComponent is mocked in the spec file. Im getting the below error.

Type 'changes: {},first: {},last: {},length: 1,dirty: false,_results: [{newRowCreated: true}' is missing the following properties form type QueryList<any>: map,filter, find, reduce and 7 more.

Is this the right way to mock the childComponent as QueryList? Please help to fix this.

knbibin
  • 1,099
  • 7
  • 18
  • 36

1 Answers1

0

To mock things you could use ng-mocks.

It supports mock child components, @ViewChildren: https://ng-mocks.sudo.eu/guides/view-child, etc.

So you may try something like that:

beforeEach(() => TestBed.configureTestingModule({
  declarations: [
    ParentComponent,
    // Mocking the child component
    MockComponent(ChildComponent),
  ],
}));

it('test checkProperty() method', () => {
  // changing properties of the child component
  ngMocks.findInstance(ChildComponent).newRowCreated = true;

  // triggers and assertions
  fixture.detectChanges();
  component.checkProperty();
  expect(component.newRowEnabled).toBe(true);
});
satanTime
  • 12,631
  • 1
  • 25
  • 73
  • Which version of ng-mocks need to use? I installed as dev dependecy and v8.1 got installed but Im not able to import ngMocks its saying the path has no exported member 'ngMocks'. Import { ngMocks } from 'ng-mocks'; is throwing error. – knbibin Jul 20 '21 at 10:18
  • @knbibin, You need the latest one, it supports all Angular versions. – satanTime Jul 20 '21 at 10:21
  • npm install ng-mocks is installing the v8.1 only. If I try manually to insatll the recent version 12.3.1 its saying No matching version found for ng-mocks@12.3.1 – knbibin Jul 20 '21 at 10:28
  • very interesting. Do you have a registry mirror or a proxy? also might you try `ng-mocks@^12.0.0`? – satanTime Jul 20 '21 at 10:34
  • I tried all the options. Why its throwing the import error in v8.1? – knbibin Jul 20 '21 at 10:42
  • in 8.1 there was no `ngMocks` yet. It was `MockHelper` I guess. – satanTime Jul 20 '21 at 10:47
  • might you post your env configuration? node version, npm version, jest / jasmine? – satanTime Jul 20 '21 at 10:48
  • in 8.1 instead of `ngMocks.findInstance` you can use `MockHelper.getDirective`: https://www.npmjs.com/package/ng-mocks/v/8.1.0#mockhelper – satanTime Jul 20 '21 at 10:49
  • MockHelper.getDirective(fixture.debugElement, ChildComponent). newRowCreated = true; is giving cannot read prooerty 'newRowCreated' of undefined. Anything wrong here. – knbibin Jul 20 '21 at 12:11
  • could post in the question the template of the parent component? also has `fixture.detectChanges();` been called before `MockHelper.getDirective`? – satanTime Jul 20 '21 at 12:13