Skip to content

Commit

Permalink
Merge pull request #130 from dafortin/master
Browse files Browse the repository at this point in the history
Added date type
  • Loading branch information
sophypal committed Jun 23, 2017
2 parents 1dfa622 + a43e5bf commit ac6723a
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 0 deletions.
1 change: 1 addition & 0 deletions addon/utils/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function generateType (key) {
'any',
'array',
'bool',
'date',
'element',
'EmberComponent',
'EmberObject',
Expand Down
18 changes: 18 additions & 0 deletions addon/utils/validators/date.js
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
}
2 changes: 2 additions & 0 deletions addon/utils/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import any from './any'
import array from './array'
import arrayOf from './array-of'
import bool from './bool'
import date from './date'
import element from './element'
import emberComponent from './ember-component'
import emberObject from './ember-object'
Expand All @@ -24,6 +25,7 @@ const validators = {
any,
array,
bool,
date,
element,
EmberComponent: emberComponent,
EmberObject: emberObject,
Expand Down
18 changes: 18 additions & 0 deletions tests/dummy/app/fixtures/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ export default Component.extend(PropTypeMixin, {
`,
name: 'bool'
},
{
description: 'Property must be a date.',
example: `
import Ember from 'ember'
const {Component} = Ember
import PropTypeMixin, {PropTypes} from 'ember-prop-types'
export default Component.extend(PropTypeMixin, {
propTypes: {
bar: PropTypes.date,
baz: PropTypes.date.isRequired,
foo: PropTypes.date({required: true}),
spam: PropTypes.date({updatable: false})
}
})
`,
name: 'date'
},
{
description: 'Property must be an instance of Element.',
example: `
Expand Down
195 changes: 195 additions & 0 deletions tests/unit/prop-types/hash-api/date-test.js
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))
})

0 comments on commit ac6723a

Please sign in to comment.