Skip to content

Commit

Permalink
another naive commit to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Jun 25, 2021
1 parent 6fbbbb5 commit 7fa7260
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
22 changes: 11 additions & 11 deletions addon/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import validationError from 'ember-validators/utils/validation-error';
* @param {String} options.onOrAfter The specified date must be on or after this date
* @param {String} options.precision Limit the comparison check to a specific granularity.
* Possible Options: [`year`, `month`, `week`, `day`, `hour`, `minute`, `second`].
* @param {String} options.format Input value date format
* @param {String} options.format Input value date format - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
* @param {String} options.errorFormat Error output date format. Defaults to `MMM Do, YYYY`
* @param {Object} model
* @param {String} attribute
*/
export default function validateDate(value, options) {
let errorFormat = options.errorFormat || 'MMM Do, YYYY';
let { locale, format, precision, allowBlank } = options;
let { locale = 'en-us', format, precision, allowBlank } = options;
let { before, onOrBefore, after, onOrAfter } = options;
let date;

Expand Down Expand Up @@ -57,7 +57,7 @@ export default function validateDate(value, options) {
if (before) {
before = parseDate(before, format, locale);

if (!isBefore(date, before, precision)) {
if (!isBefore(date, before)) {
set(options, 'before', format(before, errorFormat));
return validationError('before', value, options);
}
Expand All @@ -66,7 +66,7 @@ export default function validateDate(value, options) {
if (onOrBefore) {
onOrBefore = parseDate(onOrBefore, format, locale);

if (!isSameOrBefore(date, onOrBefore, precision)) {
if (!isSameOrBefore(date, onOrBefore)) {
set(options, 'onOrBefore', format(onOrBefore, errorFormat));
return validationError('onOrBefore', value, options);
}
Expand All @@ -75,7 +75,7 @@ export default function validateDate(value, options) {
if (after) {
after = parseDate(after, format, locale);

if (!isAfter(date, after, precision)) {
if (!isAfter(date, after)) {
set(options, 'after', format(after, errorFormat));
return validationError('after', value, options);
}
Expand All @@ -84,7 +84,7 @@ export default function validateDate(value, options) {
if (onOrAfter) {
onOrAfter = parseDate(onOrAfter, format, locale);

if (!isSameOrAfter(date, onOrAfter, precision)) {
if (!isSameOrAfter(date, onOrAfter)) {
set(options, 'onOrAfter', onOrAfter.format(errorFormat));
return validationError('onOrAfter', value, options);
}
Expand All @@ -107,22 +107,22 @@ function isValidDate(d) {

// WIP naive implementation
// TODO: timezone beware
function isSame(date, comp/*, _precision*/) {
function isSame(date, comp) {
return date === comp;
}

function isBefore(date, comp/*, _precision*/) {
function isBefore(date, comp) {
return date < comp;
}

function isAfter(date, comp/*, _precision*/) {
function isAfter(date, comp) {
return date > comp;
}

function isSameOrAfter(date, comp/*, _precision*/) {
function isSameOrAfter(date, comp) {
return isSame(date, comp) || isAfter(date, comp);
}

function isSameOrBefore(date, comp/*, _precision*/) {
function isSameOrBefore(date, comp) {
return isSame(date, comp) || isBefore(date, comp);
}
67 changes: 46 additions & 21 deletions tests/unit/validators/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('allow blank', function (assert) {

options = {
allowBlank: true,
before: '1/1/2015',
before: new Date('1/1/2015'),
};

result = validate('', cloneOptions(options));
Expand Down Expand Up @@ -50,7 +50,7 @@ test('valid input date format', function (assert) {
assert.expect(3);

options = {
format: 'DD/M/YYYY',
format: { year: 'numeric', month: 'numeric', day: '2-digit' }
};

result = validate('27/3/15', cloneOptions(options));
Expand All @@ -71,7 +71,7 @@ test('error date format', function (assert) {

options = {
errorFormat: 'M/D/YYYY',
before: '1/1/2015',
before: new Date('1/1/2015'),
};

result = validate('1/1/2016', cloneOptions(options));
Expand All @@ -82,7 +82,7 @@ test('before', function (assert) {
assert.expect(2);

options = {
before: '1/1/2015',
before: new Date('1/1/2015'),
};

result = validate('1/1/2016', cloneOptions(options));
Expand All @@ -97,9 +97,9 @@ test('before', function (assert) {

test('before now', function (assert) {
assert.expect(2);
let now = moment().format('MMM Do, YYYY');
let now = new Intl.DateTimeFormat('en', { month: 'short', year: 'numeric', day: '2-digit' }).format();
options = {
before: 'now',
before: new Date(),
};

result = validate('1/1/3015', cloneOptions(options));
Expand All @@ -113,7 +113,7 @@ test('before or on', function (assert) {
assert.expect(3);

options = {
onOrBefore: '1/1/2015',
onOrBefore: new Date('1/1/2015'),
};

result = validate('1/1/2016', cloneOptions(options));
Expand All @@ -131,9 +131,9 @@ test('before or on', function (assert) {

test('before now or on', function (assert) {
assert.expect(3);
let now = moment().format('MMM Do, YYYY');
let now = new Intl.DateTimeFormat('en', { month: 'short', year: 'numeric', day: '2-digit' }).format();
options = {
onOrBefore: 'now',
onOrBefore: new Date(),
};

result = validate('1/1/3015', cloneOptions(options));
Expand All @@ -150,7 +150,7 @@ test('before or on precision', function (assert) {
let precisions = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year'];

assert.expect(precisions.length * 3 - 1);
let now = moment(new Date('2013-02-08T09:30:26'));
let now = new Intl.DateTimeFormat('en', { month: 'short', year: 'numeric', day: '2-digit' }).format(new Date('2013-02-08T09:30:26'));
let dateString = now.toString();
let nowMessage = now.format('MMM Do, YYYY');

Expand Down Expand Up @@ -181,7 +181,7 @@ test('after', function (assert) {
assert.expect(2);

options = {
after: '1/1/2015',
after: new Date('1/1/2015'),
};

result = validate('1/1/2014', cloneOptions(options));
Expand All @@ -193,9 +193,9 @@ test('after', function (assert) {

test('after now', function (assert) {
assert.expect(2);
let now = moment().format('MMM Do, YYYY');
let now = new Intl.DateTimeFormat('en', { month: 'short', year: 'numeric', day: '2-digit' }).format();
options = {
after: 'now',
after: new Date(),
};

result = validate('1/1/2014', cloneOptions(options));
Expand All @@ -209,7 +209,7 @@ test('after or on', function (assert) {
assert.expect(3);

options = {
onOrAfter: '1/1/2015',
onOrAfter: new Date('1/1/2015'),
};

result = validate('1/1/2014', cloneOptions(options));
Expand All @@ -227,9 +227,9 @@ test('after or on', function (assert) {

test('after now or on', function (assert) {
assert.expect(3);
let now = moment().format('MMM Do, YYYY');
let now = new Intl.DateTimeFormat('en', { month: 'short', year: 'numeric', day: '2-digit' }).format();
options = {
onOrAfter: 'now',
onOrAfter: new Date(),
precision: 'second',
};

Expand All @@ -244,12 +244,12 @@ test('after now or on', function (assert) {
});

test('after or on precision', function (assert) {
let precisions = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year'];
let precisions = ['second', 'minute', 'hour', 'day', 'month', 'year'];

assert.expect(precisions.length * 3 - 1);
let now = moment(new Date('2013-02-08T09:30:26'));
let now = new Date('2013-02-08T09:30:26');
let dateString = now.toString();
let nowMessage = now.format('MMM Do, YYYY');
let nowMessage = '';

for (let i = 0; i < precisions.length; i++) {
let precision = precisions[i];
Expand All @@ -259,8 +259,9 @@ test('after or on precision', function (assert) {
result = validate(now, cloneOptions(options));
assert.equal(processResult(result), true);

let date = subOne(now, precision);
result = validate(
moment(now).subtract(1, precision),
date,
cloneOptions(options)
);
assert.equal(
Expand All @@ -271,11 +272,35 @@ test('after or on precision', function (assert) {
if (i + 1 !== precisions.length) {
options = { onOrAfter: dateString, precision: precisions[i + 1] };

let date = subOne(now, precision);
result = validate(
moment(now).subtract(1, precisions),
date,
cloneOptions(options)
);
assert.equal(processResult(result), true);
}
}
});

function subOne(d, precision) {
switch (precision) {
case 'second':
d.setSeconds(d.getSeconds() - 1);
return d;
case 'minute':
d.setMinutes(d.setMinutes() - 1);
return d;
case 'hour':
d.setHours(d.setHours() - 1);
return d;
case 'day':
d.setDate(d.getDate() - 1);
return d;
case 'month':
d.setMonth(d.getMonth() - 1);
return d;
case 'year':
d.setFullYear(d.getFullYear() - 1);
return d;
}
}

0 comments on commit 7fa7260

Please sign in to comment.