'use strict';

suite('rb/views/DraftReviewBannerView', function () {
    var template = '<div id="review-banner" hidden class="hidden">\n <div class="banner">\n  <h1>You have a pending review.</h1>\n  <input id="review-banner-edit" type="button"\n         value="Edit Review" />\n  <input id="review-banner-publish-container" type="button"\n         value="Publish" />\n  <input id="review-banner-discard" type="button"\n         value="Discard" />\n </div>\n</div>\'';
    var model = void 0;
    var view = void 0;

    beforeEach(function () {
        model = new RB.DraftReview();
        view = new RB.DraftReviewBannerView({
            el: $(template).appendTo($testsScratch),
            model: model
        });

        view.render();
    });

    afterEach(function () {
        view.remove();
    });

    describe('Button states', function () {
        var $buttons = void 0;

        beforeEach(function () {
            $buttons = view.$('input');
        });

        describe('Enabled', function () {
            it('Default', function () {
                expect($buttons.prop('disabled')).toBe(false);
            });

            it('When saved', function () {
                $buttons.prop('disabled', true);
                model.trigger('saved');
                expect($buttons.prop('disabled')).toBe(false);
            });

            it('When destroyed', function () {
                $buttons.prop('disabled', true);
                model.trigger('destroyed');
                expect($buttons.prop('disabled')).toBe(false);
            });
        });

        describe('Disabled', function () {
            it('When saving', function () {
                model.trigger('saving');
                expect($buttons.prop('disabled')).toBe(true);
            });

            it('When destroying', function () {
                model.trigger('destroying');
                expect($buttons.prop('disabled')).toBe(true);
            });
        });
    });

    describe('Button events', function () {
        it('Edit Review', function () {
            spyOn(RB.ReviewDialogView, 'create');

            view.$('#review-banner-edit').click();

            expect(RB.ReviewDialogView.create).toHaveBeenCalled();
            expect(RB.ReviewDialogView.create.calls.argsFor(0)[0].review).toBe(model);
        });

        it('Publish', function () {
            spyOn(model, 'publish');

            view.$('#review-banner-publish').click();

            expect(model.publish).toHaveBeenCalled();
        });

        it('Publish to Submitter Only', function () {
            spyOn(model, 'publish');

            /*
             * The alternative buttons from the split button are added to the
             * <body>.
             */
            $('#review-banner-publish-submitter-only').click();

            expect(model.publish).toHaveBeenCalled();
        });

        it('Publish and Archive', function () {
            spyOn(model, 'publish');
            spyOn(view, '_onPublishClicked').and.callThrough();

            /*
             * The alternative buttons from the split button are added to the
             * <body>.
             */
            $('#review-banner-publish-and-archive').click();

            expect(model.publish).toHaveBeenCalled();
            expect(view._onPublishClicked).toHaveBeenCalledWith({
                publishAndArchive: true
            });
        });

        it('Discard', function () {
            var $buttons = $();

            spyOn(model, 'destroy');
            spyOn($.fn, 'modalBox').and.callFake(function (options) {
                options.buttons.forEach(function ($btn) {
                    $buttons = $buttons.add($btn);
                });

                /* Simulate the modalBox API for what we need. */
                return {
                    modalBox: function modalBox(cmd) {
                        expect(cmd).toBe('buttons');
                        return $buttons;
                    }
                };
            });

            view.$('#review-banner-discard').click();
            expect($.fn.modalBox).toHaveBeenCalled();

            $buttons.filter('input[value="Discard"]').click();
            expect(model.destroy).toHaveBeenCalled();
        });
    });

    describe('Methods', function () {
        it('show', function () {
            expect(view.$el.hasClass('hidden')).toBe(true);
            expect(view.$el.prop('hidden')).toBe(true);

            view.show();
            expect(view.$el.hasClass('hidden')).toBe(false);
            expect(view.$el.prop('hidden')).toBe(false);
        });

        it('hide', function () {
            view.$el.addClass('hidden').prop('hidden', true);

            view.hide();
            expect(view.$el.hasClass('hidden')).toBe(true);
            expect(view.$el.prop('hidden')).toBe(true);
        });
    });
});

//# sourceMappingURL=draftReviewBannerViewTests.js.map