I noticed that the latest testing recommendations from Ember didn't have a clear behavior for initializing component with properties. this.owner.lookup is used to fetch singletons and non-singletons and will lazily create them with blank properties. This is not how Ember components are instantiated in a real app and so we cannot use this method to test a number of components that have logic inside init that depend on required properties.
My approach uses this.owner.__container__.factoryFor('component:my-component').create(props). Please suggest better ways if any.
I've updated the documentation since it wasn't updated previously to follow the new approach to calling subject for unit testing components.
I also updated the test to follow the latest conventions like use async/await, using helpers like render from @ember/test-helpers, accessing DOM through this.element vs this.$. Removed the need for needs array in the setup-component-test helpers because the new testing framework boots up the app and loads all dependencies. Stubbing can be done by interacting with the DI container this.owner.registerthis.owner.inject. Naturally, I removed test related to test dependencies.
CHANGELOG
Fixed unit from setup-component-test to initialize components with supplied properties in subject(overrides)
Updated documentation to show the correct way to call test helpers
Removed old test that no longer applies due to how tests are boot up
PATCH
I noticed that the latest testing recommendations from Ember didn't have a clear behavior for initializing component with properties.
this.owner.lookup
is used to fetch singletons and non-singletons and will lazily create them with blank properties. This is not how Ember components are instantiated in a real app and so we cannot use this method to test a number of components that have logic insideinit
that depend on required properties.My approach uses
this.owner.__container__.factoryFor('component:my-component').create(props)
. Please suggest better ways if any.I've updated the documentation since it wasn't updated previously to follow the new approach to calling
subject
for unit testing components.I also updated the test to follow the latest conventions like use
async/await
, using helpers likerender
from@ember/test-helpers
, accessing DOM throughthis.element
vsthis.$
. Removed the need forneeds
array in thesetup-component-test
helpers because the new testing framework boots up the app and loads all dependencies. Stubbing can be done by interacting with the DI containerthis.owner.register
this.owner.inject
. Naturally, I removed test related to test dependencies.CHANGELOG
unit
fromsetup-component-test
to initialize components with supplied properties insubject(overrides)