I have a component that works as expected. I have also a working set up with Aurelia and Jest. I have also created some tests with something similar to below:
Is there anything I need to do to make it rerender when under test. It works just fine in the real app.
The point of the test is to make sure that the view presents the strong messsage and that the method show actually sets the property correctly. So it does not feel correct just to test the strongmessage value on the viewmodel.
Custom elements and attributes do not register their view model instances with the root container. When you do Container.instance.get(MessageBar), you are actually creating a new instance of that component.
What can be done is to get the element and then get the view model based on the controller instead
That did not really solve the issue for me but got me in the correct direction. I was testing some properties for equality and have found this (which probably everybody already knows)
Yes, the behavior you observed is because the component tester automatically retrieve the first view model controller (either custom element/attribute) to return it with component.viewModel. What you have works fine and simpler for single element/attribute test case. If you ever have more than one, then doing it the query + $au way will give easier time.
Regardless, happy coding
If anyone ends up here this is what I did in the end in the test method:
it('says "abc"',async () => {
var element1:Element = component.element;
var vm1:MessageBar = component.viewModel;
// make the call to the viewModel
vm1.show("abc","abc","info");
await TestHelper.waitForUI();
expect(element1.querySelector(".ms-fontWeight-semibold").textContent.trim()).toBe('abc');
});
And the we have the TestHelper:
export class TestHelper
{
static waitForUI()
{
return new Promise(function(resolve, reject) {
setTimeout(resolve, 0);
});
}
}