{"size": 5283, "ext": "js", "max_stars_repo_path": "tests/specs/form/DurationEditorView.spec.js", "max_stars_repo_name": "PavelSolodkin/core-ui", "max_stars_repo_stars_event_min_datetime": "2016-01-21T10:41:03.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-27T05:32:09.000Z", "max_issues_repo_path": "tests/specs/form/DurationEditorView.spec.js", "max_issues_repo_name": "PavelSolodkin/core-ui", "max_issues_repo_issues_event_min_datetime": "2016-07-21T17:33:50.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-14T11:49:02.000Z", "max_forks_repo_path": "tests/specs/form/DurationEditorView.spec.js", "max_forks_repo_name": "PavelSolodkin/core-ui", "max_forks_repo_forks_event_min_datetime": "2016-01-15T12:25:22.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-29T08:38:29.000Z"}
{"max_stars_count": 32, "max_issues_count": 1107, "max_forks_count": 57, "avg_line_length": 29.5139664804, "max_line_length": 91, "alphanum_fraction": 0.4448230172}
import core from 'coreApi';
import 'jasmine-jquery';
import 'jasmine-expect';
import FocusTests from './FocusTests';

/*eslint-ignore*/
describe('Editors', () => {
    const show = view =>
    window.app
        .getView()
        .getRegion('contentRegion')
        .show(view);

    describe('DurationEditor', () => {
        FocusTests.runFocusTests({
            initialize: () => {
                const model = new Backbone.Model({
                    value: 'P3DT3H4M'
                });
                return new core.form.editors.DurationEditor({
                    model,
                    key: 'value'
                });
            },
            focusElement: '.js-input'
        });

        it('should have `value` matched with initial value', () => {
            // arrange
            const model = new Backbone.Model({
                value: 'P3DT3H4M'
            });
            const view = new core.form.editors.DurationEditor({
                model,
                key: 'value'
            });
            show(view);

            // act
            const value = view.getValue();

            // assert
            const expected = model.get('value');
            expect(value).toEqual(expected);
        });

        it('should hide clear button if hideClearButton = true', () => {
            const model = new Backbone.Model({
                data: 'P3DT3H4M'
            });
            const view = new core.form.editors.DurationEditor({
                model,
                key: 'data',
                changeMode: 'keydown',
                autocommit: true,
                hideClearButton: true
            });

            show(view);

            view.trigger('mouseenter');
            expect(view.$('.js-clear-button').length).toEqual(0);
        });

        it('should set max value if value over max when blur() is called', () => {
            const max = 'P12DT18H4M';
            const overMaxInput = '15 d 18 h 4 m 0 s';
            const model = new Backbone.Model({
                data: 'P3DT3H4M'
            });
            const view = new core.form.editors.DurationEditor({
                model,
                key: 'data',
                changeMode: 'keydown',
                autocommit: true,
                max
            });

            show(view);

            const input = view.$('input');

            input.focus()
                .val(overMaxInput)
                .blur();

            const editorValue = view.getValue();
            const modelValue = model.get('data');

            expect(editorValue).toEqual(max);
            expect(modelValue).toEqual(max);
        });

        it('should set min value if value less min when blur() is called', () => {
            const min = 3600000; //1 hour
            const minISO = 'PT1H';
            const secondISO = 'PT1S';
            const secondInput = '0 d 0 h 0 m 1 s';
            const model = new Backbone.Model({
                data: 'P3DT3H4M'
            });
            const view = new core.form.editors.DurationEditor({
                model,
                key: 'data',
                changeMode: 'keydown',
                autocommit: true,
                min
            });

            show(view);

            const input = view.$('input');

            input.focus()
                .val(secondInput)
                .blur();

            const editorValue = view.getValue();
            const modelValue = model.get('data');

            expect(editorValue).toEqual(minISO);
            expect(modelValue).toEqual(minISO);
        });

        it('should set null to model after clear', done => {
            const model = new Backbone.Model({
                data: 'P3DT3H4M'
            });
            const view = new core.form.editors.DurationEditor({
                model,
                key: 'data',
                changeMode: 'keydown',
                autocommit: true
            });

            model.on('change:data', (model, data) => {
                expect(data).toBeNull(`onClear set '${data}'`);
                done();
            });

            view.on('attach', () => {
                view.$el.trigger('mouseenter');
                const clearButton = view.$('.js-clear-button');
                expect(clearButton.length === 1).toBeTrue('editor has no clear button!');
                clearButton.click();
                view.blur();
            });

            show(view);
        });

        it('should set zero duration to model if editor value = "0 d 0 h 0 m 0 s"', () => {
            const nullInput = '0 d 0 h 0 m 0 s';
            const model = new Backbone.Model({
                data: 'P3DT3H4M'
            });
            const view = new core.form.editors.DurationEditor({
                model,
                key: 'data',
                changeMode: 'keydown',
                autocommit: true
            });

            show(view);

            const input = view.$('input');

            input.focus()
                .val(nullInput)
                .blur();

            let editorValue = view.getValue();
            let modelValue = model.get('data');
            expect(editorValue).toEqual('P0D');
            expect(modelValue).toEqual('P0D');
        });
    });
});
