{"size": 3661, "ext": "js", "max_stars_repo_path": "src/Constraints/__tests__/LengthValidator.js", "max_stars_repo_name": "maarekj/jsconstraint", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/Constraints/__tests__/LengthValidator.js", "max_issues_repo_name": "maarekj/jsconstraint", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/Constraints/__tests__/LengthValidator.js", "max_forks_repo_name": "maarekj/jsconstraint", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 43.5833333333, "max_line_length": 107, "alphanum_fraction": 0.569789675}
import LengthValidator from '../LengthValidator';
import Violation from '../../Violation';
import {mockExecutionContext} from '../../mocks';

describe('Test length validator', () => {
    const validator = new LengthValidator();
    it('accept method', () => {
        expect(validator.acceptConstraint({name: 'length'})).toBe(true);
        expect(validator.acceptConstraint({name: 'not_found'})).toBe(false);
    });

    describe('with max', () => {
        it('with blank value', () => {
            const context = mockExecutionContext();
            validator.validate("", {name: 'length', max: 5}, context);
            expect(context.addViolation).toHaveBeenCalledTimes(0);
        });

        it('with valid value', () => {
            const context = mockExecutionContext();
            validator.validate("1234", {name: 'length', max: 5}, context);
            expect(context.addViolation).toHaveBeenCalledTimes(0);
        });

        it('with just valid value', () => {
            const context = mockExecutionContext();
            validator.validate("12345", {name: 'length', max: 5}, context);
            expect(context.addViolation).toHaveBeenCalledTimes(0);
        });

        describe('with invalid value', () => {
            it('with not message provided', () => {
                const context = mockExecutionContext();
                validator.validate("123456", {name: 'length', max: 5}, context);
                expect(context.addViolation).toHaveBeenCalledTimes(1);
                expect(context.addViolation).toHaveBeenCalledWith(new Violation('validations.length_max'));
            });

            it('with message provided', () => {
                const context = mockExecutionContext();
                validator.validate("123456", {name: 'length', max: 5, maxMessage: "my message"}, context);
                expect(context.addViolation).toHaveBeenCalledTimes(1);
                expect(context.addViolation).toHaveBeenCalledWith(new Violation('my message'));
            });
        });
    });

    describe('with min', () => {
        it('with blank value', () => {
            const context = mockExecutionContext();
            validator.validate("", {name: 'length', min: 5}, context);
            expect(context.addViolation).toHaveBeenCalledTimes(0);
        });

        it('with valid value', () => {
            const context = mockExecutionContext();
            validator.validate("123456", {name: 'length', min: 5}, context);
            expect(context.addViolation).toHaveBeenCalledTimes(0);
        });

        it('with just valid value', () => {
            const context = mockExecutionContext();
            validator.validate("12345", {name: 'length', min: 5}, context);
            expect(context.addViolation).toHaveBeenCalledTimes(0);
        });

        describe('with invalid value', () => {
            it('with not message provided', () => {
                const context = mockExecutionContext();
                validator.validate("1234", {name: 'length', min: 5}, context);
                expect(context.addViolation).toHaveBeenCalledTimes(1);
                expect(context.addViolation).toHaveBeenCalledWith(new Violation('validations.length_min'));
            });

            it('with message provided', () => {
                const context = mockExecutionContext();
                validator.validate("1234", {name: 'length', min: 5, minMessage: "my message"}, context);
                expect(context.addViolation).toHaveBeenCalledTimes(1);
                expect(context.addViolation).toHaveBeenCalledWith(new Violation('my message'));
            });
        });
    });
});
