// In JavaScript code, use strict is highly recommended, it allows the engine to be more optimized.
"use strict";

// Load the Appium driver, this sets up our connection to Appium and the emulator or device.
const nsAppium = require("nativescript-dev-appium");

// Just like Jasmine, Mocha uses describe to start a testing group.
describe("Simple example", function () {

    // This is fairly important, you need to give the driver time to wait
    // so that your app has time to start up on the emulator/device.
    // This number might still be too small if you have a slow machine.
    this.timeout(100000);

    // This holds the driver; that will be used to communicate with Appium & Device.
    let driver;

    // This is ran once before any tests are ran.  (There is also a beforeEach)
    before(function () {

        // VERY, VERY important line here; you NEED a driver to communicate to your device.
        // No driver, no tests will work.
        driver = nsAppium.createDriver();
    });

    // This is ran once at the end of all the tests. (There is also a afterEach)
    after(function () {

        // Also important, the Appium system works off of promises; so you return the promise from the after function
        return driver

        // This tells the driver to quit....
        .quit()

        // And finally after it has quit we print it finished....
        .finally(function () {
            console.log("Driver quit successfully");
        });
    });

    // Just like jasmine, we define a test here.
    it("should find the + button", function () {

        // Again, VERY important, you need to return the promise, no returned promise; test will do weird things!
        return driver

        // This searches for an element by the Dom path; so you can find sub items.
        // The double "//" means the element can be anywhere, you can use
        // "/path/to/component" to make a direct link to a specific xpath item
        .elementByXPath("//" + nsAppium.getXPathElement('Button'))


        // This element should eventually exist
        .text().should.eventually.exist.equal('\uf067');

        // We could also chain more commands after this like
        //.tap() if we wanted to run the button

    });

    it("should have a Demo label", function () {

        // Again, VERY important, you need to return the promise, no returned promise; test will do weird things!
        return driver

        // Find all Label elements, that has text of "Demo"
        .elementByXPath("//" + nsAppium.getXPathElement("Label") + "[@text='Demo']")

        // This item should eventually exist
        .should.eventually.exist

        // Tap it
        .tap();

    });

    it("Should change to another screen", function () {
        // Again, VERY important, you need to return the promise, no returned promise; test will do weird things!
        return driver

        // Find all Label elements, that has text of "Demo"
        .waitForElementByXPath("//" + nsAppium.getXPathElement("Label") + "[@text='Drums']")

        // This item should eventually exist
        .should.eventually.exist.text();

    });


    it("Should change mute button", function () {
        // Again, VERY important, you need to return the promise, no returned promise; test will do weird things!
        return driver

        // Find all Label elements, that has text of "Demo"
        .waitForElementByXPath("//" + nsAppium.getXPathElement("Label") + "[@text='\uf028']")

        // This item should eventually exist
        .should.eventually.exist

        .tap()

        .text().should.eventually.become("\uf026");

    });

});

