[Documentation - Angular] What is the best practice for spying on component methods? #26081
Replies: 3 comments 5 replies
-
@CMadden8 I think you raise an interesting slim area in our documentation around this. Certainly you can spy on any public of the Component Class. The reason "why" we don't have recommendations around this in general is that ideally tests aren't tied to the methods of the class but the side effects those methods have on the UI itself. For example: export class ButtonComponent {
@Input() type: 'primary' | 'secondary' | 'danger' | 'outline' = 'primary'
getStyles(): string {
let styles =
'items-center py-2 px-4 text-sm font-medium text-center rounded-lg focus:ring-4 focus:outline-none';
switch (this.type) {
case 'primary':
styles +=
' text-white bg-blue-700 hover:bg-blue-800 focus:ring-blue-300';
break;
case 'secondary':
styles +=
' text-gray-800 bg-gray-100 hover:bg-gray-200 focus:ring-gray-300';
break;
case 'danger':
styles += ' text-white bg-red-600 hover:bg-red-700 focus:ring-red-300';
break;
case 'outline':
styles +=
' text-gray-800 bg-gray-200/10 hover:bg-gray-200/75 focus:ring-gray-300 border border-gray-300';
}
return styles;
}
} Given the example Component above we could write a test that validates the Alternatively, we could write a similar test using a different approach with Component Testing: it('valididates the correct style of gray given type outline', () => {
cy.mount(ButtonComponent, {
componentProperties: {
type: 'outline'
}
})
cy.get('button').should('have.class', 'ext-gray-800 bg-gray-200/10 hover:bg-gray-200/75 focus:ring-gray-300 border border-gray-300')
}); Does this help provide any clarity? |
Beta Was this translation helpful? Give feedback.
-
Sorry, simple question can you spy on service calls in a component test? I can't get it to work. I will read this whole thread now |
Beta Was this translation helpful? Give feedback.
-
Yes you can. You would just need to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've seen the below from the docs, but is this really the only way of spying on the component methods?
The component testing feature is very good in general - I'm trying to switch our team's unit tests from Jest to Cypress component testing. But the documentation is quite slim in terms of content/examples...
Generally you want to spy on the component's methods, rather than the component's outputs (which seems to be the focus of the documentation, for some reason...). I find outputs are rarely used, even without something like ngrx.
Yet the docs focus on auto-creation of output spies, or creation of output spies via the mount config. Cool, but what about the more common methods you want to spy on - the methods on the component itself?
I'm not trying to be a smartass, but I think this should be the focus of the documentation, as auto-creation of spies for component methods would be a million times more useful than output spies. In particular I think the documentation should give us more examples of even a basic version of how to do this.
It's a very promising alternative to Jest etc - and it's working great so far... But the documentation could use some further effort IMO.
Even googling around, there are virtually no useful examples on how to unit test what you 99% of the time want to unit test: spying on the methods of the components themselves...
Beta Was this translation helpful? Give feedback.
All reactions