{"size": 2803, "ext": "js", "max_stars_repo_path": "client/app/modules/books/add-book/add-book.controller.spec.js", "max_stars_repo_name": "cminhho/Book-Manager-Exercise", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/app/modules/books/add-book/add-book.controller.spec.js", "max_issues_repo_name": "cminhho/Book-Manager-Exercise", "max_issues_repo_issues_event_min_datetime": "2021-05-10T15:05:41.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T21:17:46.000Z", "max_forks_repo_path": "client/app/modules/books/add-book/add-book.controller.spec.js", "max_forks_repo_name": "cminhho/Book-Manager-Exercise", "max_forks_repo_forks_event_min_datetime": "2021-09-13T19:59:51.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-13T19:59:51.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 37.3733333333, "max_line_length": 111, "alphanum_fraction": 0.5811630396}
'use strict';

describe('Controller Tests', function () {
    describe('Controller - Add Book', function () {

        var addBookControler;
        var booksServiceMock;
        var validateServiceMock;
        var $state;
        var $q;

        beforeEach(function () {
            module('ui.router.state');
            module('book-inventory-app');
            module('book-inventory-app.books');

            booksServiceMock = jasmine.createSpyObj('BooksService', ['createBook']);
            validateServiceMock = jasmine.createSpyObj('ValidateDateService', ['isValidDate']);

            inject(function ($injector) {
                $state = $injector.get('$state');
                $q = $injector.get('$q');
                var $controller = $injector.get('$controller');

                addBookControler = $controller('AddBookController', {
                    $state: $state,
                    BooksService: booksServiceMock,
                    ValidateDateService: validateServiceMock
                });

                spyOn($state, 'go');
            });
        });

        it('should display an error message if all of the fields are not completed', function () {
            expect(addBookControler.showFieldsRequiredError).toBe(false);
            addBookControler.saveBook();
            expect(addBookControler.showFieldsRequiredError).toBe(true);
        });

        it('should display an error message if the publication date entered is not a valid date', function () {
            expect(addBookControler.showPublicationDateError).toBe(false);
            validateServiceMock.isValidDate.and.returnValue(false);
            addBookControler.saveBook();
            expect(addBookControler.showPublicationDateError).toBe(true);
        });

        it('should call createBook on the BooksService if validation is successful', function () {
            var mockBook = {
                title: 'Test',
                author: 'Test Author',
                publisher: 'Test Publisher',
                publicationDate: '1/1/2016',
                description: 'Test Description'
            };

            var deferred = $q.defer();
            booksServiceMock.createBook.and.returnValue(deferred.promise)
            validateServiceMock.isValidDate.and.returnValue(true);

            addBookControler.book = mockBook;
            addBookControler.saveBook();

            expect(booksServiceMock.createBook).toHaveBeenCalled();
            expect(booksServiceMock.createBook).toHaveBeenCalledWith(mockBook);
        });

        it('should return to the list view on cancel', function () {
            addBookControler.cancelSave();
            expect($state.go).toHaveBeenCalled();
            expect($state.go).toHaveBeenCalledWith('books');
        });
    });
});
