ionic-mocks
Advanced tools
Weekly downloads
Readme
Simple test doubles for Ionic 2+ using Jasmine Spy Objects
This project is still very early in development and there are several things I'm sorting out. However since this is only meant to be used in your tests it should be safe to pull into a project.
npm install --save-dev ionic-mocks
import {Events, AlertController} from 'ionic-angular';
import {EventsMock, AlertControllerMock} from 'ionic-mocks';
describe('MyClass', () => {
let events: Events;
let alertCtrl: AlertController;
let classUnderTest: MyClass;
beforeEach(() => {
events = EventsMock.instance();
alertCtrl = AlertControllerMock.instance();
classUnderTest = new MyClass(events, alertCtrl);
});
it('should subscribe to events', () => {
expect(events.subscribe).toHaveBeenCalled();
});
it('should call alert create', () => {
classUnderTest.showAlert();
expect(alertCtrl.create).toHaveBeenCalled();
});
});
import {Events, AlertController, Alert} from 'ionic-angular';
import {EventsMock, AlertControllerMock, AlertMock} from 'ionic-mocks';
describe('MyComponent', () => {
let alert: Alert;
let events: Events;
let alertCtrl: AlertController;
let classUnderTest: MyClass;
beforeEach(() => {
events = EventsMock.instance();
alert = AlertMock.instance():
alertCtrl = AlertControllerMock.instance(alert);
classUnderTest = new MyClass(events, alertCtrl);
});
it('should call present on alert', (done) => {
classUnderTest.showAlert().then(() => {
expect(alert.present).toHaveBeenCalled();
done();
});
});
});
TestBed.configureTestingModule({
imports: [IonicModule],
declarations: [MyComponentUnderTest],
providers: [
{provide: ViewController, useFactory: () => ViewControllerMock.instance()}
]
});
This project has a long way to go and is full of opportunities to contribute. I am back in school and working so for the rest of the year expect my responsiveness on this project to be slow. If anyone is up to helping vet PRs please message me. sclarklasley@gmail.com
TypeError: jit__object_Object_33 is not a function
error. What does that mean?Answer: This means you've done something wrong. Take a look at this example:
// DO NOT DO THIS
// USING useClass INSTEAD OF useFactory IS INCORRECT
{ provide: App, useClass: AppMock }
// DO NOT DO THIS
// USING useFactory WITHOUT THE FAT ARROW SYNTAX IS INCORRECT
{ provide: App, useFactory: AppMock.instance() }
Make sure you are using the useFactory
property name and using the fat arrow function as the value:
// DO THIS
{ provide: App, useFactory: () => AppMock.instance() }
FAQs
Stubbed and Pre-mocked Ionic2+ Objects
The npm package ionic-mocks receives a total of 359 weekly downloads. As such, ionic-mocks popularity was classified as not popular.
We found that ionic-mocks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.