Announcing ionic native mocks!

Ionic Native Mocks are designed to be used as placeholders during development for the actual Ionic Native modules. If you don’t know what Ionic Native is, here is the official summary:

Ionic Native is a curated set of wrappers for Apache Cordova plugins that make adding any native functionality you need to your Ionic mobile application easier.

Ionic Native wraps plugin callbacks in a Promise or Observable, providing a common interface for all plugins and making it easy to use plugins with Angular change detection

Currently Available Mocks

  • actions-sheets
  • barcode-scanner
  • ble
  • bluetooth-serial
  • calendar
  • camera
  • contacts
  • date-picker
  • db-meter
  • device-motion
  • device-orientation
  • device
  • dialog
  • email-composer
  • geolocation
  • globalization
  • image-picker
  • keyboard
  • network
  • social-sharing
  • splash-screen
  • sqlite
  • status-bar
  • toast
  • touch-id
  • vibration
  • Installation

This project allows developers to use Ionic Native Mocks in place of the actual Ionic Native modules. They can be installed in via to methods.

  • via npm: Installing these prebuilt mocks is easy but they are not easily customized.
  • via GitHub and manually added to your Ionic project: Installing this way means the mocks can be customized to return specific data, like a specific parsing of a QR code.

More information at: Ionic Native Mocks

3 Likes

Can you explain what happens when you use these mocks and then run ionic serve?
Is any functionality actually implemented in the Mocks? If not, how would I do so?

When you add the Ionic Native Mocks to your app, you can make the same calls as you would to the corresponding Ionic Native. When you run your app locally, you will not get errors/warnings about using Cordova plugins. These do NOT magically make Ionic Native actually do want the plug would do.

If you just use the npm version, customize the responses is a bit tricky, as the Typescript has been transpiled to Javascript.

You want to have custom responses, say returning a valid decoded data stream from a QR Code, then use the code on the GitHub repo directly.

Does that help?

Chris

1 Like

This sounds like a great tool – at least it attempts to address an issue that’s been bothering me for a while. Just to clarify:

  1. When I want to compile on a device, the only change in code I need to make is to remove the useClass statements in the provider declarations?

  2. Importing by hand from Github means what exactly? Can I put TypeScript code in @ionic-native-mocks/camera ? I don’t understand node-modules well enough to follow this, I’m afraid.

Thanks!

1 Like

Indeed it does.

So the mocks are basically “void”-ified copies of the normal implementation that makes sure all the methods work without Cordova and its plugins in place. Correct?

Can I also use the compiled version from npm, then “extend” your class and overwrite the “void”-ified methods you have that make sure not errors are thrown (with ones that “fake” some functionality? Or do I have to use the Typescript version and mess with it manually?

Does somebody could help about this asking ?
I like to know how to implement some fake data with the provided classes ? Does have I to declare class’s occurences in the app.module.ts ? I see the exemple of documentation and thought to custom the repository’s source avalaible at Github,by copying and merging the needed class(es) in the app.module.ts, wich is filled with the fake data ? Is it true ?