modal_helpers.js 2.35 KB
Newer Older
1 2 3
/**
 * Provides helper methods for invoking Studio modal windows in Jasmine tests.
 */
Andy Armstrong committed
4 5
define(["jquery", "js/spec_helpers/view_helpers"],
    function($, view_helpers) {
6
        var installModalTemplates,
7
            getModalElement,
8
            isShowingModal,
9
            hideModalIfShowing,
10
            pressModalButton,
11 12
            cancelModal,
            cancelModalIfShowing;
13 14

        installModalTemplates = function(append) {
Andy Armstrong committed
15
            view_helpers.installViewTemplates(append);
16 17
            view_helpers.installTemplate('basic-modal');
            view_helpers.installTemplate('modal-button');
18 19
        };

20
        getModalElement = function(modal) {
21 22 23 24 25 26
            var modalElement;
            if (modal) {
                modalElement = modal.$('.wrapper-modal-window');
            } else {
                modalElement = $('.wrapper-modal-window');
            }
27 28 29
            return modalElement;
        };

30 31 32 33 34
        getModalTitle = function(modal) {
            var modalElement = getModalElement(modal);
            return modalElement.find('.modal-window-title').text();
        };

35 36
        isShowingModal = function(modal) {
            var modalElement = getModalElement(modal);
37 38 39
            return modalElement.length > 0;
        };

40 41 42 43 44 45
        hideModalIfShowing = function(modal) {
            if (isShowingModal(modal)) {
                modal.hide();
            }
        };

46 47
        pressModalButton = function(selector, modal) {
            var modalElement, button;
48
            modalElement = getModalElement(modal);
49 50 51 52 53 54 55
            button = modalElement.find(selector + ':visible');
            expect(button.length).toBe(1);
            button.click();
        };

        cancelModal = function(modal) {
            pressModalButton('.action-cancel', modal);
56 57
        };

58 59 60 61 62 63
        cancelModalIfShowing = function(modal) {
            if (isShowingModal(modal)) {
                cancelModal(modal);
            }
        };

Andy Armstrong committed
64
        return $.extend(view_helpers, {
65
            'getModalElement': getModalElement,
66
            'getModalTitle': getModalTitle,
67 68
            'installModalTemplates': installModalTemplates,
            'isShowingModal': isShowingModal,
69
            'hideModalIfShowing': hideModalIfShowing,
70
            'pressModalButton': pressModalButton,
71 72
            'cancelModal': cancelModal,
            'cancelModalIfShowing': cancelModalIfShowing
Andy Armstrong committed
73
        });
74
    });