Ionic-framework: Providers for improved testability

Created on 21 Jun 2016  路  5Comments  路  Source: ionic-team/ionic-framework

In V2, there are some APIs that are called via static methods (Modal.create(), Alert.create, etc) and some APIs that use the new keyword (gestures, for example).

It would make apps more testable if we could create thin wrappers/factories that can be injected via constructor injection.

Some places where this would be good:

  1. Global variables like window. A WindowProvider class w/ a getWindow method or something
  2. AlertProvider class, etc. Create instance method calls static create method on the Alert class. Apply concept to all overlays.
  3. Etc, I am sure there are several places where we could take advantage of this pattern

Basically, this will not only make our code more testable, but it will make our users code more testable as well.

Existing APIs should not change at all, the "Provider" is merely a thin wrapper that's 100% optional and can be introduced at any time.

Most helpful comment

You guys rock!

@danbucholtz I am still running Beta9 since I haven't merged the Beta10 upgrade branch in dev yet, but will give it a spin on a branch to get a feel for it.

@jgw96 thanks for the tip. Always love to get a glimpse of the bleeding edge so will try nightly asap. Also Thank you for making Ionic2 so awesome. We are building a huge project with it and counting on your great work to make our lives easier and our app better.

All 5 comments

Was just looking if something like that is possible, as I am running into a lot of trouble trying to write tests for components that have Alert, Loading, Modal and ActionSheet. I know I can write my own wrappers and be on my way, but I would much prefer if this was "provided" (pun intended) out of the box

@masimakopoulos, this is implemented and in the latest beta. There may be a few more places we need to implement the concept, but most overlays have this now. I'm going to keep this open for now as it's a WIP.

http://ionicframework.com/docs/v2/api/components/alert/AlertController/
http://ionicframework.com/docs/v2/api/components/loading/LoadingController/
etc.

We should add these for the storage options too.

new Storage(LocalStorage)
new Storage(SqlStorage)

Should both use the same concepts.

Thanks,
Dan

Also @masimakopoulos nice pun, made me laugh haha (: Also i would just like to add that if you want to give those new api's a try today before the next release you can just run npm install ionic-angular@nightly which will install the latest nightly that already has these new api's. Thanks for using Ionic!

You guys rock!

@danbucholtz I am still running Beta9 since I haven't merged the Beta10 upgrade branch in dev yet, but will give it a spin on a branch to get a feel for it.

@jgw96 thanks for the tip. Always love to get a glimpse of the bleeding edge so will try nightly asap. Also Thank you for making Ionic2 so awesome. We are building a huge project with it and counting on your great work to make our lives easier and our app better.

Hello all! I am going to close this issue for now as the new @ionic/storage that replaces the old Storage module uses providers. Thanks!

Was this page helpful?
0 / 5 - 0 ratings