<commit_before>/* globals Event, jasmine, ajaxForm */
describe('ajax-form custom element tests', function() {
    beforeEach(function() {
        this.form = document.createElement('form');
    });

    describe('method normalization', function() {
        beforeEach(function() {
            var ajaxContainer = document.createElement('div');            
            ajaxContainer.appendChild(document.createElement('core-ajax'));
            this.form.shadowRoot = ajaxContainer;
        });
        
        it('throws an error if the method is not "post" or "put"', function() {
            expect(ajaxForm.domReady.bind(this.form)).toThrow();

            this.form.setAttribute('method', 'get');
            expect(ajaxForm.domReady.bind(this.form)).toThrow();
        });

        it('adds a listener to squelch submit events if the method is "post"', function() {
            spyOn(this.form, 'addEventListener');

            this.form.setAttribute('method', 'post');
            ajaxForm.domReady.call(this.form);
            expect(this.form.addEventListener.calls.mostRecent().args[0]).toBe('submit');
        });

        it('adds a listener to squelch submit events if the method is "put"', function() {
            spyOn(this.form, 'addEventListener');

            this.form.setAttribute('method', 'put');
            ajaxForm.domReady.call(this.form);
            expect(this.form.addEventListener.calls.mostRecent().args[0]).toBe('submit');
        });
    });

    describe('form validation', function() {
        beforeEach(function() {
            jasmine.clock().install();
            
            this.form.setAttribute('method', 'post');
            this.form.fire = jasmine.createSpy('fire');
            
            var ajaxContainer = document.createElement('div');            
            ajaxContainer.appendChild(document.createElement('core-ajax'));
            this.form.shadowRoot = ajaxContainer;
        });
        
        afterEach(function() {
            jasmine.clock().uninstall();
        });

        it('ensures invalid fields are idenitified and presented via a single event', function() {
            var textInput1 = document.createElement('input'),
                textInput2 = document.createElement('input'),
                textInput3 = document.createElement('input');

            textInput1.setAttribute('name', 'test1');
            textInput2.setAttribute('name', 'test2');
            textInput3.setAttribute('name', 'test3');

            this.form.appendChild(textInput1);
            this.form.appendChild(textInput2);
            this.form.appendChild(textInput3);

           ajaxForm.domReady.call(this.form);
            
            var inputInvalidEvent = document.createEvent('Event');
            inputInvalidEvent.initEvent('invalid', true, true);
            
            textInput1.dispatchEvent(inputInvalidEvent);
            jasmine.clock().tick(5);
            
            textInput3.dispatchEvent(inputInvalidEvent);
            jasmine.clock().tick(10);

            expect(this.form.fire).toHaveBeenCalledWith('invalid', [textInput1, textInput3]);
        });
    });
});<commit_msg>test($ajax-form): #15 - Test overridden `submit()` method<commit_after>/* globals Event, jasmine, ajaxForm */
describe('ajax-form custom element tests', function() {
    beforeEach(function() {
        this.form = document.createElement('form');
    });

    describe('method normalization', function() {
        beforeEach(function() {
            var ajaxContainer = document.createElement('div');            
            ajaxContainer.appendChild(document.createElement('core-ajax'));
            this.form.shadowRoot = ajaxContainer;
        });
        
        it('throws an error if the method is not "post" or "put"', function() {
            expect(ajaxForm.domReady.bind(this.form)).toThrow();

            this.form.setAttribute('method', 'get');
            expect(ajaxForm.domReady.bind(this.form)).toThrow();
        });

        it('adds a listener to squelch submit events if the method is "post"', function() {
            spyOn(this.form, 'addEventListener');

            this.form.setAttribute('method', 'post');
            ajaxForm.domReady.call(this.form);
            expect(this.form.addEventListener.calls.mostRecent().args[0]).toBe('submit');
        });

        it('adds a listener to squelch submit events if the method is "put"', function() {
            spyOn(this.form, 'addEventListener');

            this.form.setAttribute('method', 'put');
            ajaxForm.domReady.call(this.form);
            expect(this.form.addEventListener.calls.mostRecent().args[0]).toBe('submit');
        });
    });

    describe('form validation', function() {
        beforeEach(function() {
            jasmine.clock().install();
            
            this.form.setAttribute('method', 'post');
            this.form.fire = jasmine.createSpy('fire');
            
            var ajaxContainer = document.createElement('div');            
            ajaxContainer.appendChild(document.createElement('core-ajax'));
            this.form.shadowRoot = ajaxContainer;
        });
        
        afterEach(function() {
            jasmine.clock().uninstall();
        });

        it('ensures invalid fields are idenitified and presented via a single event', function() {
            var textInput1 = document.createElement('input'),
                textInput2 = document.createElement('input'),
                textInput3 = document.createElement('input');

            textInput1.setAttribute('name', 'test1');
            textInput2.setAttribute('name', 'test2');
            textInput3.setAttribute('name', 'test3');

            this.form.appendChild(textInput1);
            this.form.appendChild(textInput2);
            this.form.appendChild(textInput3);

           ajaxForm.domReady.call(this.form);

            var inputInvalidEvent = document.createEvent('Event');
            inputInvalidEvent.initEvent('invalid', true, true);

            textInput1.dispatchEvent(inputInvalidEvent);
            jasmine.clock().tick(5);

            textInput3.dispatchEvent(inputInvalidEvent);
            jasmine.clock().tick(10);

            expect(this.form.fire).toHaveBeenCalledWith('invalid', [textInput1, textInput3]);
        });
    });

    describe('submit interception', function() {
        beforeEach(function() {
            jasmine.clock().install();

            this.form.setAttribute('method', 'post');
            this.form.fire = jasmine.createSpy('submit');

            var ajaxContainer = document.createElement('div');
            ajaxContainer.appendChild(document.createElement('core-ajax'));
            this.form.shadowRoot = ajaxContainer;
        });

        afterEach(function() {
            jasmine.clock().uninstall();
        });

        it('submit method is overridden', function() {
            var textInput1 = document.createElement('input'),
                textInput2 = document.createElement('input'),
                textInput3 = document.createElement('input');

            textInput1.setAttribute('name', 'test1');
            textInput2.setAttribute('name', 'test2');
            textInput3.setAttribute('name', 'test3');

            this.form.appendChild(textInput1);
            this.form.appendChild(textInput2);
            this.form.appendChild(textInput3);

            ajaxForm.domReady.call(this.form);

            this.form.submit();

            expect(this.form.fire).toHaveBeenCalledWith('submit');
        });
    });
});

{'fraction_non_alphanumeric': 0.13793558606124603, 'fraction_numerical': 0.007391763463569166, 'mean_word_length': 2.257523645743766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5803087', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1884, 'n_words': 318}