modal_helpers.js 2.59 KB
Newer Older
1 2 3
/**
 * Provides helper methods for invoking Studio modal windows in Jasmine tests.
 */
4
define(['jquery', 'common/js/spec_helpers/template_helpers', 'common/js/spec_helpers/view_helpers'],
5
    function($, TemplateHelpers, ViewHelpers) {
6
        var installModalTemplates, getModalElement, getModalWindow, getModalTitle, isShowingModal,
7
            hideModalIfShowing, pressModalButton, cancelModal, cancelModalIfShowing;
8 9

        installModalTemplates = function(append) {
10 11 12
            ViewHelpers.installViewTemplates(append);
            TemplateHelpers.installTemplate('basic-modal');
            TemplateHelpers.installTemplate('modal-button');
13 14
        };

15
        getModalElement = function(modal) {
16 17 18 19 20 21
            var modalElement;
            if (modal) {
                modalElement = modal.$('.wrapper-modal-window');
            } else {
                modalElement = $('.wrapper-modal-window');
            }
22 23 24
            return modalElement;
        };

25 26 27 28 29
        getModalWindow = function(modal) {
            var modalElement = getModalElement(modal);
            return modalElement.find('.modal-window');
        };

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);
            }
        };

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