Search code examples
angularangular-materialjasmineangular-unit-test

Creating Mock for Sort Header Event Angular Material


I am trying to write a unit test for a matSortChange function. This emits a Sort Event that I thought was just an object with an active property and direction property. My test currently looks like:

it('sort unit test', () => {
 const fakeSortEvent = {active: 'fakeActive', direction: 'asc'};
 component.saveSort(fakeSortEvent);

})

The TS error I am getting is

Types of property 'direction' are incompatible.
Type 'string' is not assignable to type 'SortDirection'

When looking up what SortDirection looks like angular material docs, it seems like my fakeSortEvent object should work. How can I create a mock Sort Event?


Solution

  • Thank @diabolique for the answer in the commments. Adding type Sort to fakeSortEvent has it working. Code:

    it('sort unit test', () => {
     const fakeSortEvent: Sort = {active: 'fakeActive', direction: 'asc'};
     component.saveSort(fakeSortEvent);
    })