#Testing @Input() transform function

7 messages · Page 1 of 1 (latest)

swift kiln
#

If it's really the function that you want to test, then test the function, by calling it and checking that it returns the correct value.
If you want to test that it's being used by Angular when you use it on an input (which seems quite pointless to me: you would be testing Angular, not the function), then create a testing component which has such an input, use it in another parent, testing component, and see if that works fine.
If you want to test that a real component using this function on one of its inputs works correctly, then.. do that. And if that input is displayed several times in the template, then check that all these places display the correct value.

zenith rapids
#

About it being pointless, what i meant is testing that the component includes said transformation. Doesn't seem pointless to me, and it doesnt test Angular.

swift kiln
#

That's the third case then, not the second one.

zenith rapids
#

Yeah my question would be about that third case. I wanted to know if there would be a better way than testing the transformation itself in every place the @Input() is used, as it doesn't seem the most reasonable approach

swift kiln
#

It doesn't really have much to do with input transformation function. Your component has various responsibilities. The test is supposed to check that the responsibilities are fulfilled correctly. If there are 3 things that should be displayed correctly, then check that those 3 things are indeed displayed correctly. Whether 1, or 2, or 3 of them depend on the input is irrelevant: the test should check them anyway, shouldn't it?

zenith rapids
#

The responsability of the component when it comes to said @Input() is to forward it to 3 different pipes. For now, my tests only check that the values set to the @Input() are indeed being passed to the pipes.

For the edge cases controlled by the transform function, it would make sense to me to be able to test those once (with a test for the function itself) , and then just check that the component includes it, as you would do for example with a form validation.

swift kiln
#

That's not how I would do it. I would instead treat the pipes and the function as implementation details. Treat the component using them as a black box which has well-defined responsibilities, and check it fulfills them. If you change the inner implementation of the component, the test would still pass.