<commit_before>/*global angular,inject,describe,it,expect,beforeEach*/
describe('directive: date-field', function() {
    'use strict';

    var dateDirective = require('../../../../ng-admin/Crud/field/maDateField');
    var DateField = require('admin-config/lib/Field/DateField');

    angular.module('testapp_DateField', ['ui.bootstrap', 'ui.bootstrap.tpls'])
        .filter('translate', () => text => text)
        .directive('maDateField', dateDirective);

    var $compile,
        scope,
        directiveUsage = '<ma-date-field field="field" value="value"></ma-date-field>';

    beforeEach(angular.mock.module('testapp_DateField'));

    beforeEach(inject(function(_$compile_, _$rootScope_){
        $compile = _$compile_;
        scope = _$rootScope_;
    }));

    it("should contain an input tag", function () {
        scope.field = new DateField();
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).attr('type')).toBe('text');
    });

    it('should format default value correctly', function () {
        scope.value = new Date(2014, 2, 1);
        scope.field = (new DateField()).format('yyyy-MM-dd');

        var element = $compile(directiveUsage)(scope);
        scope.$digest();

        expect(element.find('input').eq(0).val()).toBe('2014-03-01');
    });

    it("should use the supplied format as datepicker parameter", function () {
        scope.field = new DateField().format('yyyy-MM');
        var date = new Date('2015-01-23');
        scope.value = date;
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).attr('uib-datepicker-popup')).toBe('yyyy-MM');
    });

    it("should add any supplied attribute", function () {
        scope.field = new DateField().attributes({ placeholder: 'here the date' });
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).attr('placeholder')).toEqual('here the date');
    });

    it("should contain the bounded value", function () {
        var now = '2015-04-05';
        scope.value = new Date(now);
        scope.field = new DateField();
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).val()).toBe(now);
    });

    it('should update rawValue when updating value directly', () => {
        const now = new Date('2016-09-18');
        scope.value = now;
        scope.field = new DateField();

        const element = $compile(directiveUsage)(scope);
        scope.$digest();

        scope.value = new Date('2010-01-01');
        scope.$digest();

        expect(element.find('input').eq(0).val()).toBe('2010-01-01');
    });

    it('should update value when updating the rawValue directly', () => {
        const now = new Date('2016-09-18');
        scope.value = now;
        scope.field = new DateField();

        const element = $compile(directiveUsage)(scope);
        const isolatedScope = element.isolateScope();

        isolatedScope.rawValue = new Date('2010-01-01');
        isolatedScope.$digest();

        expect(isolatedScope.value).toBe('2010-01-01');
    });
});<commit_msg>Add test<commit_after>/*global angular,inject,describe,it,expect,beforeEach*/
describe('directive: date-field', function() {
    'use strict';

    var dateDirective = require('../../../../ng-admin/Crud/field/maDateField');
    var DateField = require('admin-config/lib/Field/DateField');

    angular.module('testapp_DateField', ['ui.bootstrap', 'ui.bootstrap.tpls'])
        .filter('translate', () => text => text)
        .directive('maDateField', dateDirective);

    var $compile,
        scope,
        directiveUsage = '<ma-date-field field="field" value="value"></ma-date-field>';

    beforeEach(angular.mock.module('testapp_DateField'));

    beforeEach(inject(function(_$compile_, _$rootScope_){
        $compile = _$compile_;
        scope = _$rootScope_;
    }));

    it("should contain an input tag", function () {
        scope.field = new DateField();
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).attr('type')).toBe('text');
    });

    it('should format default value correctly', function () {
        scope.value = new Date(2014, 2, 1);
        scope.field = (new DateField()).format('yyyy-MM-dd');

        var element = $compile(directiveUsage)(scope);
        scope.$digest();

        expect(element.find('input').eq(0).val()).toBe('2014-03-01');
    });

    it("should use the supplied format as datepicker parameter", function () {
        scope.field = new DateField().format('yyyy-MM');
        var date = new Date('2015-01-23');
        scope.value = date;
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).attr('uib-datepicker-popup')).toBe('yyyy-MM');
    });

    it("should add any supplied attribute", function () {
        scope.field = new DateField().attributes({ placeholder: 'here the date' });
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).attr('placeholder')).toEqual('here the date');
    });

    it("should contain the bounded value", function () {
        var now = '2015-04-05';
        scope.value = new Date(now);
        scope.field = new DateField();
        var element = $compile(directiveUsage)(scope);
        scope.$digest();
        expect(element.find('input').eq(0).val()).toBe(now);
    });

    it('should update rawValue when updating value directly', () => {
        const now = new Date('2016-09-18');
        scope.value = now;
        scope.field = new DateField();

        const element = $compile(directiveUsage)(scope);
        scope.$digest();

        scope.value = new Date('2010-01-01');
        scope.$digest();

        expect(element.find('input').eq(0).val()).toBe('2010-01-01');
    });

    it('should update value when updating the rawValue directly', () => {
        const now = new Date('2016-09-18');
        scope.value = now;
        scope.field = new DateField();

        const element = $compile(directiveUsage)(scope);
        const isolatedScope = element.isolateScope();

        isolatedScope.rawValue = new Date('2010-01-01');
        isolatedScope.$digest();

        expect(isolatedScope.value).toBe('2010-01-01');
    });

    it('should not update value if input is invalid', () => {
        const now = '2016-09-18';
        scope.value = now;
        scope.field = new DateField();

        const element = $compile(directiveUsage)(scope);
        const isolatedScope = element.isolateScope();

        element.find('input').val('2016-09-1').triggerHandler('input');
        isolatedScope.$digest();
        expect(isolatedScope.value).toBe('2016-09-18');

        element.find('input').val('2016-09-19').triggerHandler('input');
        isolatedScope.$digest();
        expect(isolatedScope.value).toBe('2016-09-19');
    })
});
{'fraction_non_alphanumeric': 0.17641270733764408, 'fraction_numerical': 0.029097554118639304, 'mean_word_length': 2.858459869848156, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '5321757', 'n_tokens_mistral': 2181, 'n_tokens_neox': 1996, 'n_words': 397}