Have ideas to improve npm?Join in the discussion! »

ionic3-mocksTypeScript icon, indicating that this package has built-in type declarations

0.11.22 • Public • Published

Build Status

ionic-mocks

Simple test doubles for Ionic 3+ 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.

Supported Types

  • ActionSheet
  • ActionSheetController
  • Alert
  • AlertController
  • App
  • Content
  • Config
  • Events
  • Form
  • Haptic
  • InfiniteScroll
  • ItemSliding
  • Loading
  • LoadingController
  • Menu
  • MenuController
  • Modal
  • ModalController
  • Platform
  • Popover
  • PopoverController
  • NavController
  • Nav
  • NavParams
  • StatusBar
  • Tab
  • Tabs
  • Toast
  • ToastController
  • ViewController

Native Plugins

  • 3DTouch
  • Keyboard
  • Network
  • Splashscreen
  • Network
  • ScreenOrientation

Installation

npm install --save-dev ionic-mocks

Examples

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();
    });
});

Pre-mocked return types

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();
        });
 
    });
});

Override ionic providers in TestingModule

TestBed.configureTestingModule({
    imports: [IonicModule],
    declarations: [MyComponentUnderTest],
    providers: [
            {provide: ViewController, useFactory: () => ViewControllerMock.instance()}
    ]
});

Keywords

none

Install

npm i ionic3-mocks

DownloadsWeekly Downloads

5

Version

0.11.22

License

MIT

Unpacked Size

93.5 kB

Total Files

151

Last publish

Collaborators

  • avatar