/**
 * Created by fran lopez on 9/11/15.
 */

    /**
     * CommonJS module to define a e2e test to check operations with
     * book using the views defined in path "/public/books/views".
     */

    describe('Books E2E Tests:', function() {
        describe('Test for books', function() {
            it('Should not be able to find a book, because the name of writer is empty', function() {

                browser.get('http://localhost:3000/#!/books/booksByWriter');
                element(by.css('input[type=submit]')).click();
                element(by.binding('error')).getText().then(function(errorText) {
                    expect(errorText).toBe('El nombre del escritor del libro no puede ser nulo');
                });
            });

            it("Should not be able to find books, because there aren't books with this name of writer", function() {

                browser.get('http://localhost:3000/#!/books/booksByWriter');

                var bookTitle = element(by.css('input[type=text]'));

                bookTitle.sendKeys('aaaaaaaaa');
                element(by.css('input[type=submit]')).click();
                element(by.css('label')).getText().then(function(errorText) {
                    expect(errorText).toBe('No existen libros que coincidan con el patrón introducido');
                });
            });

            it("Should be able to find books, because there are books with this name of writer", function() {

                browser.get('http://localhost:3000/#!/books/booksByWriter');

                var bookWriter = element(by.css('input[type=text]'));

                bookWriter.sendKeys('Dan Brown');
                element(by.css('input[type=submit]')).click();
                expect(element(by.css('li a')).isPresent()).toBe(true);
            });

            it('Should not be able to find a book, because the name of genre is empty', function() {

                browser.get('http://localhost:3000/#!/books/booksByGenre');
                element(by.css('input[type=submit]')).click();
                element(by.binding('error')).getText().then(function(errorText) {
                    expect(errorText).toBe('El género del libro no puede ser nulo');
                });
            });

            it("Should be able to find books, because there are books with this genre", function() {

                browser.get('http://localhost:3000/#!/books/booksByGenre');

                element(by.binding('genre')).click();
                element(by.css('input[type=submit]')).click();
                expect(element(by.css('li a')).isPresent()).toBe(true);
            });

            it('Should not be able to find a book, because the name of protagonist is empty', function() {

                browser.get('http://localhost:3000/#!/books/booksByProtagonist');
                element(by.css('input[type=submit]')).click();
                element(by.binding('error')).getText().then(function(errorText) {
                    expect(errorText).toBe('El nombre del protagonista del libro no puede ser nulo');
                });
            });

            it("Should not be able to find books, because there aren't books with this name of protagonist", function() {

                browser.get('http://localhost:3000/#!/books/booksByProtagonist');

                var bookProtagonist = element(by.css('input[type=text]'));

                bookProtagonist.sendKeys('aaaaaaaaa');
                element(by.css('input[type=submit]')).click();
                element(by.css('label')).getText().then(function(errorText) {
                    expect(errorText).toBe('No existen libros que coincidan con el patrón introducido');
                });
            });

            it("Should be able to find books, because there are books with this name of protagonist", function() {

                browser.get('http://localhost:3000/#!/books/booksByProtagonist');

                var bookProtagonist = element(by.css('input[type=text]'));

                bookProtagonist.sendKeys('Ottavia Salina');
                element(by.css('input[type=submit]')).click();
                expect(element(by.css('li a')).isPresent()).toBe(true);
            });

            it('Should not be able to find a book, because the rating is empty', function() {

                browser.get('http://localhost:3000/#!/books/booksByRating');
                element(by.css('input[type=submit]')).click();
                element(by.binding('error')).getText().then(function(errorText) {
                    expect(errorText).toBe('La valoración del libro no puede ser un valor nulo');
                });
            });

            it("Should be able to find books, because there are books with this rating", function() {

                browser.get('http://localhost:3000/#!/books/booksByRating');

                element(by.binding('rating')).click();
                element(by.css('input[type=submit]')).click();
                expect(element(by.css('li a')).isPresent()).toBe(true);
            });

            it('Should not be able to find a book, because the title is empty', function() {

                browser.get('http://localhost:3000/#!/books/booksByTitle');
                element(by.css('input[type=submit]')).click();
                element(by.binding('error')).getText().then(function(errorText) {
                    expect(errorText).toBe('El título del libro no puede ser nulo');
                });
            });

            it("Should not be able to find books, because there aren't books with this title", function() {

                browser.get('http://localhost:3000/#!/books/booksByTitle');

                var bookTitle = element(by.css('input[type=text]'));

                bookTitle.sendKeys('aaaaaaaaa');
                element(by.css('input[type=submit]')).click();
                element(by.css('label')).getText().then(function(errorText) {
                    expect(errorText).toBe('No existen libros que coincidan con el patrón introducido');
                });
            });

            it("Should be able to find books, because there are books with this title", function() {

                browser.get('http://localhost:3000/#!/books/booksByTitle');

                var bookTitle = element(by.css('input[type=text]'));

                bookTitle.sendKeys('El Código Da Vinci');
                element(by.css('input[type=submit]')).click();
                expect(element(by.css('li a')).isPresent()).toBe(true);
            });

            it('Should not be able to find a book, because the publication date is empty', function() {

                browser.get('http://localhost:3000/#!/books/booksByYear');
                element(by.css('input[type=submit]')).click();
                element(by.binding('error')).getText().then(function(errorText) {
                    expect(errorText).toBe('La fecha de publicación del libro no puede ser nula');
                });
            });

            it("Should be able to find books, because there are books with this publication date", function() {

                browser.get('http://localhost:3000/#!/books/booksByYear');

                element(by.binding('year')).click();
                element(by.css('input[type=submit]')).click();
                expect(element(by.css('li a')).isPresent()).toBe(true);
            });
        });
    });