Coder Social home page Coder Social logo

ionic-mocks's Introduction

npm version Build Status Coverage Status Commitizen friendly semantic-release

ionic-mocks

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.

Supported Types

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

Native Plugins

  • 3DTouch
  • File
  • GoogleAnalyics
  • Keyboard
  • Network
  • NFC
  • Splashscreen
  • StatusBar
  • Vibration

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

Contributing

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. [email protected]

Contributors

Frequently Asked Questions:

Q: I am receiving a 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() }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.