0
export class ComponentDetailsComponent implements OnInit {   
    @ViewChild('treeGridReference') treeGrid: jqxTreeGridComponent;
....more code....
  this.treeGrid.clearSelection();// how do I mock this?
}

Had it been a constructor dependency I could have just created a stub for jqxTreeGridComponent with empty clearSelection method, something similar to :

class jqxTreeGridComponentStub{
  clearSelection(){}
  }
}
{ provide: jqxTreeGridComponent, useClass: jqxTreeGridComponentStub },
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

1 Answers1

1

You can provide a mock component in your declarations array in configure Testing module,

@Component({selector: 'jqxTreeGridComponent-selector', template: ''})
class jqxTreeGridComponent{
   clearSelection(){}
}

describe('', () => {
    beforeEach(async(() => {
            TestBed.configureTestingModule({
                declarations: [ ComponentDetailsComponent , jqxTreeGridComponent],
            }).compileComponents();
        }));
    })
}

To read more about stub components, read here in the documentation.

Akshay Rana
  • 1,455
  • 11
  • 20
  • That doesn't ensure the provided component is a variable inside the parent component (although with the testbed it probably will). Also, this prevents the mocking, meaning you provide the actual component, which is a bad practice in unit testing –  Jun 28 '19 at 12:21
  • You are not just testing a class, but a Component and it includes your template as well, View child will create the variable with the stub component you provided. It's how you add Shallow components while testing. There are 2 ways to do it, and it is mentioned in the documentation. https://angular.io/guide/testing#stubbing-unneeded-components Don't just down vote, if it's an alternative. – Akshay Rana Jun 28 '19 at 12:34
  • If you look only at the code provided, you *assume* he's testing the template. Nothing states that the test bed is used. And often, the template is tested through e2e testing and not through unit testing. Your solution assumes too many things and is suited for only specific scenarios. I don't say you're wrong, don't mistake my words ! I'm saying your answer is too specific for the poor quantity of code provided. –  Jun 28 '19 at 12:38
  • Well, he added the unit test tag to the question. And writing unit tests without TestBed, good luck with that. Also in the documentation, it's mentioned if you are interacting with the child component, this method is a good option. **The stub component approach has another advantage. While the stubs in this example were empty, you could give them stripped-down templates and classes if your tests need to interact with them in some way.** – Akshay Rana Jun 28 '19 at 12:44
  • All of my unit testing is done without the test bed ... And it's way faster. And again, I don't say you're wrong, I say you're assuming too much. Could you care to read a message when I take time to write it ? –  Jun 28 '19 at 12:47
  • 1
    Cool, I assumed he is using TestBed, which most of angular developers are. Well, it is still an alternative in that case. – Akshay Rana Jun 28 '19 at 12:58
  • I am using TestBed but I am not performing e2e tests I am keeping template out of it. I am only testing component – SamuraiJack Jun 28 '19 at 13:59