0

I have part of template

<fj-dataTable [value]="memorySlots" emptyMessage="No records found" …
    <p-column field="designation" …

When I mistype in value or memorySlots or emptyMessage then Angular catches this error during production build.

When I mistype in designation then compilation goes ok. Thus I'd like to add unit test testing assignment of properties in template. But this is the grand-child component. How can I achieve this?

koral
  • 2,807
  • 3
  • 37
  • 65

1 Answers1

0

Simply use a variable for your fields :

<p-column [field]="fields.designation" ...

In your component :

fields = {
  designation: 'designation'
};

In your tests, you can now simply test on hard coded values :

expect(component.fields.designation).toEqual('designation');
  • for testing purposes I've modified template ` – koral May 21 '18 at 10:15
  • If you ever change the value of that variable in your code for whatever reason, your test won't work because the value is hard coded. –  May 21 '18 at 10:16
  • Go ahead, try mistyping designation and see how it behaves. –  May 21 '18 at 10:17
  • Ok but I don't want to modify component's template. I have ` – koral May 21 '18 at 10:19
  • No you're right, that's what I'm suggesting. And you not wanting to change the template is another issue, here I'm telling you how to easily test what you want. If you're not satisfied, go ahead and use the query selector `By.css`, but you'll have a hard time using it. –  May 21 '18 at 10:27