Skip to content

Commit

Permalink
Merge pull request #53 from brockpetrie/input-parsing
Browse files Browse the repository at this point in the history
Input parsing
  • Loading branch information
brockpetrie committed Nov 16, 2017
2 parents ef8b82a + 11161dc commit 2613a2d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "vue-moment",
"version": "2.2.0",
"version": "3.0.0",
"description": "Handy Moment.js filters for your Vue.js project",
"main": "vue-moment.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -37,6 +37,8 @@ Moment.js expects your input to be either: a valid ISO 8601 formatted string (se
<span>{{ [ someDate, ["MM.DD.YY", "MM-DD-YY", "MM-DD-YYYY"] ] | moment("dddd, MMMM Do YYYY") }}</span>
```

As of 3.0.0, passing an empty or invalid input will no longer initiate moment with a new `Date` object.

## Filtering Methods

### format (default)
Expand Down
67 changes: 66 additions & 1 deletion tests/vue-moment.spec.js
Expand Up @@ -6,7 +6,7 @@ Vue.use(VueMoment, {
moment,
})

const now = moment()
let now = moment()
const tomorrow = moment().add(1, 'day')

const vm = new Vue({
Expand Down Expand Up @@ -117,4 +117,69 @@ describe('VueMoment', () => {
})
})

describe('handle inputs', () => {
beforeEach(() => {
global.console.warn = jest.fn()
})

afterAll(() => {
vm.now = moment()
})

it('handles string', (done) => {
vm.now = '2017-01-01'
vm.args = ['YYYY-MM-DD']
vm.$nextTick(() => {
expect(console.warn).not.toBeCalled()
expect(vm.$el.textContent).toContain('2017-01-01')
done()
})
})

it('handles object', (done) => {
vm.now = {y: 2017, m: 1, d: 1}
vm.args = ['YYYY-MM-DD']
vm.$nextTick(() => {
expect(console.warn).not.toBeCalled()
expect(vm.$el.textContent).toContain('2017-01-01')
done()
})
})

it('handles Date object', (done) => {
vm.now = new Date(2017, 0, 1);
vm.args = ['YYYY-MM-DD']
vm.$nextTick(() => {
expect(console.warn).not.toBeCalled()
expect(vm.$el.textContent).toContain('2017-01-01')
done()
})
})

it('handles Moment object', (done) => {
vm.now = moment('2017-01-01')
vm.args = ['YYYY-MM-DD']
vm.$nextTick(() => {
expect(console.warn).not.toBeCalled()
expect(vm.$el.textContent).toContain('2017-01-01')
done()
})
})

it('handles undefined', (done) => {
vm.now = undefined
vm.$nextTick(() => {
expect(console.warn).toBeCalled()
done()
})
})

it('handles invalid string', (done) => {
vm.now = 'foo'
vm.$nextTick(() => {
expect(console.warn).toBeCalled()
done()
})
})
})
})
2 changes: 1 addition & 1 deletion vue-moment.js
Expand Up @@ -31,7 +31,7 @@ module.exports = {
date = moment(input);
}

if (!date.isValid()) {
if (!input || !date.isValid()) {
// Log a warning if moment couldn't reconcile the input. Better than throwing an error?
console.warn('Could not build a valid `moment` object from input.');
return input;
Expand Down

0 comments on commit 2613a2d

Please sign in to comment.