-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from dafortin/master
Added date type
- Loading branch information
Showing
6 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* The PropTypes.date validator | ||
*/ | ||
|
||
import Ember from 'ember' | ||
const {typeOf} = Ember | ||
|
||
import logger from '../logger' | ||
|
||
export default function (ctx, name, value, def, logErrors, throwErrors) { | ||
const valid = typeOf(value) === 'date' | ||
|
||
if (!valid && logErrors) { | ||
logger.warn(ctx, `Expected property ${name} to be a date but instead got: ${typeOf(value)}`, throwErrors) | ||
} | ||
|
||
return valid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/** | ||
* Unit test for the PropTypes.date validator | ||
*/ | ||
import Ember from 'ember' | ||
import SpreadMixin from 'ember-spread' | ||
import {afterEach, beforeEach, describe} from 'mocha' | ||
import sinon from 'sinon' | ||
|
||
import { | ||
itSupportsUpdatableOption, | ||
itValidatesOnUpdate, | ||
itValidatesTheProperty, | ||
spyOnValidateMethods | ||
} from 'dummy/tests/helpers/validator' | ||
|
||
import PropTypesMixin, {PropTypes} from 'ember-prop-types/mixins/prop-types' | ||
|
||
const requiredDef = { | ||
required: true, | ||
type: 'date' | ||
} | ||
|
||
const notRequiredDef = { | ||
required: false, | ||
type: 'date' | ||
} | ||
|
||
describe('Unit / validator / PropTypes.date', function () { | ||
const ctx = {propertyName: 'bar'} | ||
let sandbox, Foo | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create() | ||
spyOnValidateMethods(sandbox) | ||
}) | ||
|
||
afterEach(function () { | ||
sandbox.restore() | ||
}) | ||
|
||
describe('when required option not present', function () { | ||
beforeEach(function () { | ||
ctx.def = notRequiredDef | ||
Foo = Ember.Object.extend(SpreadMixin, PropTypesMixin, { | ||
propTypes: { | ||
bar: PropTypes.date() | ||
} | ||
}) | ||
}) | ||
|
||
describe('when initialized with date value', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({bar: new Date()}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
itValidatesOnUpdate(ctx, 'date', 'Expected property bar to be a date') | ||
}) | ||
|
||
describe('when initialized without value', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create() | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
itValidatesOnUpdate(ctx, 'date', 'Expected property bar to be a date') | ||
}) | ||
|
||
describe('when initialized with date value via spread property', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({ | ||
options: { | ||
bar: new Date() | ||
} | ||
}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
}) | ||
|
||
describe('when initialized without value via spread property', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({ | ||
options: {} | ||
}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
}) | ||
}) | ||
|
||
describe('when required', function () { | ||
beforeEach(function () { | ||
ctx.def = requiredDef | ||
Foo = Ember.Object.extend(SpreadMixin, PropTypesMixin, { | ||
propTypes: { | ||
bar: PropTypes.date({required: true}) | ||
} | ||
}) | ||
}) | ||
|
||
describe('when initialized with date value', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({bar: new Date()}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
itValidatesOnUpdate(ctx, 'date', 'Expected property bar to be a date') | ||
}) | ||
|
||
describe('when initialized without value', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create() | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false, 'Missing required property bar') | ||
itValidatesOnUpdate(ctx, 'date', 'Expected property bar to be a date') | ||
}) | ||
|
||
describe('when initialized with date value via spread property', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({ | ||
options: { | ||
bar: new Date() | ||
} | ||
}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
}) | ||
|
||
describe('when initialized without value via spread property', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({ | ||
options: {} | ||
}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false, 'Missing required property bar') | ||
}) | ||
}) | ||
|
||
describe('when not required', function () { | ||
beforeEach(function () { | ||
ctx.def = notRequiredDef | ||
Foo = Ember.Object.extend(SpreadMixin, PropTypesMixin, { | ||
propTypes: { | ||
bar: PropTypes.date({required: false}) | ||
} | ||
}) | ||
}) | ||
|
||
describe('when initialized with date value', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({bar: new Date()}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
itValidatesOnUpdate(ctx, 'date', 'Expected property bar to be a date') | ||
}) | ||
|
||
describe('when initialized without value', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create() | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
itValidatesOnUpdate(ctx, 'date', 'Expected property bar to be a date') | ||
}) | ||
|
||
describe('when initialized with date value via spread property', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({ | ||
options: { | ||
bar: new Date() | ||
} | ||
}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
}) | ||
|
||
describe('when initialized without value via spread property', function () { | ||
beforeEach(function () { | ||
ctx.instance = Foo.create({ | ||
options: {} | ||
}) | ||
}) | ||
|
||
itValidatesTheProperty(ctx, false) | ||
}) | ||
}) | ||
|
||
itSupportsUpdatableOption('date', new Date(), new Date(86400000)) | ||
}) |
Oops, something went wrong.