File: methods.js

package info (click to toggle)
node-pikaday 1.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 380 kB
  • sloc: javascript: 1,193; makefile: 14
file content (130 lines) | stat: -rwxr-xr-x 4,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var Pikaday = require('../'),
    expect = require('expect.js');

describe('Pikaday public method', function ()
{
    'use strict';

    describe('#getDate()', function() {
        it('should return null if date not set', function() {
            expect(new Pikaday().getDate()).to.be(null);
        });
    });

    describe('#toString()', function ()
    {
        it('should return empty string when date not set', function ()
        {
            var pikaday = new Pikaday();
            expect(pikaday.toString()).to.be.empty;
        });

        it('should return date string, formatted by moment, when date is set', function() {
            var date = new Date(2014, 3, 25),
            pikaday = new Pikaday({
                format: 'DD-MM-YY'
            });

            pikaday.setDate(date);
            expect(pikaday.toString()).to.eql('25-04-14');
        });

        it('should use toString function if one is provided', function () {
            var date = new Date(2014, 3, 25),
            pikaday = new Pikaday({
                toString: function(d) {
                    var date = d.getDate();
                    var month = d.getMonth() + 1;
                    return 'custom: ' + date + '/' + month;
                }
            });

            pikaday.setDate(date);
            expect(pikaday.toString()).to.eql('custom: 25/4');
        });

        it('should pass current format option to the toString function', function () {
            var date = new Date(2014, 3, 25);
            var expectedFormat = 'DD/MM/YYYY';
            var passedFormat;

            var pikaday = new Pikaday({
                format: expectedFormat,
                toString: function(d, format) {
                    passedFormat = format;
                    return '';
                }
            });

            pikaday.setDate(date);
            pikaday.toString(); // invoke toString to set the passedFormat variable
            expect(passedFormat).to.eql(expectedFormat);
        });

        it('should use parse function if one is provided', function () {
            var expectedDate = new Date(2017, 3, 6);
            var pikaday = new Pikaday({
                parse: function() {
                    return new Date(2017, 3, 6);
                }
            });

            // mock input field
            pikaday._o.field = {
                value: '',
                setAttribute: function() {},
                dispatchEvent: function() {},
            };
            pikaday._onInputChange({});

            expect(pikaday.getDate().getTime()).to.eql(expectedDate.getTime());
        });

        it('should pass input value and current format to the parse function', function () {
            var expectedValue = 'test value';
            var expectedFormat = 'DD/MM/YYYY';
            var passedValue;
            var passedFormat;
            var pikaday = new Pikaday({
                format: expectedFormat,
                parse: function(value, format) {
                    passedValue = value;
                    passedFormat = format;
                    return new Date(2017, 3, 6);
                }
            });

            // mock input field
            pikaday._o.field = {
                value: expectedValue,
                setAttribute: function() {},
                dispatchEvent: function() {},
            };
            pikaday._onInputChange({});

            expect(passedValue).to.eql(expectedValue);
            expect(passedFormat).to.eql(expectedFormat);
        });
    });

    describe('When specifying minDate option in Constructor', function () {
        it('Should remove the time portion (flattening to midnight)', function () {
            var date = new Date(2015, 1, 17, 22, 10, 5),
                expected = new Date(2015, 1, 17, 0, 0, 0),
                pikaday = new Pikaday({ minDate: date });

            expect(pikaday._o.minDate).to.eql(expected);
        });
    });

    describe('#setMinDate()', function () {
        it('should flatten date to midnight ignoring time portion (consistent with minDate option in ctor)', function () {
            var date = new Date(2015, 1, 17, 22, 10, 5),
                expected = new Date(2015, 1, 17, 0, 0, 0),
                pikaday = new Pikaday();

            pikaday.setMinDate(date);
            expect(pikaday._o.minDate).to.eql(expected);
        });
    });
});