Skip to content

Commit

Permalink
Merge branch 'release/1.0.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 8, 2017
2 parents 0a59723 + 0da41c3 commit 670d24c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 71 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="1.0.9"></a>
## [1.0.9](https://github.com/adonisjs/adonis-session/compare/v1.0.8...v1.0.9) (2017-08-08)


### Bug Fixes

* **globals:** flash view globals use resolve method ([abc0fdf](https://github.com/adonisjs/adonis-session/commit/abc0fdf))



<a name="1.0.8"></a>
## [1.0.8](https://github.com/adonisjs/adonis-session/compare/v1.0.7...v1.0.8) (2017-08-05)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/session",
"version": "1.0.8",
"version": "1.0.9",
"description": "This repo is the official session provider for Adonisjs apps. It supports multiple drivers to store session data.",
"main": "providers/SessionProvider",
"directories": {
Expand Down
10 changes: 5 additions & 5 deletions src/Session/FlashGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ const _ = require('lodash')

module.exports = function (View) {
View.global('old', function (key, defaultValue) {
return _.get(this.flashMessages, key, defaultValue)
return _.get(this.resolve('flashMessages'), key, defaultValue)
})

View.global('errors', function () {
return this.$globals.old('errors')
return this.resolve('old')('errors')
})

View.global('hasErrors', function (key) {
return !!_.size(this.$globals.errors())
return !!_.size(this.resolve('errors')())
})

View.global('getErrorFor', function (key) {
const errors = this.$globals.errors()
const errors = this.resolve('errors')()

/**
* If errors is an object and not an array
Expand All @@ -44,6 +44,6 @@ module.exports = function (View) {
})

View.global('hasErrorFor', function (key) {
return !!this.$globals.error(key)
return !!this.resolve('getErrorFor')(key)
})
}
109 changes: 44 additions & 65 deletions test/flash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ const Session = require('../src/Session')
const FlashGlobals = require('../src/Session/FlashGlobals')
const { cookie: Cookie } = require('../src/Session/Drivers')

const View = {
resolve: function (key) {
let value = this.$data[key]
if (!value) {
value = this.$globals[key]
value = typeof (value) === 'function' ? value.bind(this) : value
}
return value
},
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback
},
setData: function (key, values) {
this.$data = {}
this.$data[key] = values
}
}

test.group('Flash Messages', () => {
test('store flash messages to the session', async (assert) => {
const server = http.createServer((req, res) => {
Expand Down Expand Up @@ -132,95 +151,55 @@ test.group('Flash Messages', () => {
})
})

test.group('Flash View Globals', () => {
test('return flash message from view globals', (assert) => {
const View = {
flashMessages: { username: 'virk' },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}

test.group('Flash View Globals', (group) => {
group.before(() => {
FlashGlobals(View)
assert.equal(View.$globals.old('username'), 'virk')
})

test('return flash message from view globals', (assert) => {
View.setData('flashMessages', { username: 'virk' })
assert.equal(View.resolve('old')('username'), 'virk')
})

test('return error messages from flash messages', (assert) => {
const View = {
flashMessages: { username: 'virk', errors: [{ message: 'Some error message' }] },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}
View
.setData('flashMessages', { username: 'virk', errors: [{ message: 'Some error message' }] })

FlashGlobals(View)
assert.deepEqual(View.$globals.errors(), [{ message: 'Some error message' }])
assert.deepEqual(View.resolve('errors')(), [{ message: 'Some error message' }])
})

test('return error message for a specific field', (assert) => {
const View = {
flashMessages: { username: 'virk', errors: [{ message: 'Some error message', field: 'username' }] },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}
View
.setData('flashMessages', { username: 'virk', errors: [{ message: 'Some error message', field: 'username' }] })

FlashGlobals(View)
assert.equal(View.$globals.getErrorFor('username'), 'Some error message')
assert.equal(View.resolve('getErrorFor')('username'), 'Some error message')
})

test('return true when has errors', (assert) => {
const View = {
flashMessages: { username: 'virk', errors: [{ message: 'Some error message', field: 'username' }] },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}
View
.setData('flashMessages', { username: 'virk', errors: [{ message: 'Some error message', field: 'username' }] })

FlashGlobals(View)
assert.isTrue(View.$globals.hasErrors())
assert.isTrue(View.resolve('hasErrors')())
})

test('return false when no errors', (assert) => {
const View = {
flashMessages: { username: 'virk', errors: [] },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}
View
.setData('flashMessages', { username: 'virk', errors: [] })

FlashGlobals(View)
assert.isFalse(View.$globals.hasErrors())
assert.isFalse(View.resolve('hasErrors')())
})

test('return error from a plain object', (assert) => {
const View = {
flashMessages: { username: 'virk', errors: { username: 'username is required' } },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}
View
.setData('flashMessages', { username: 'virk', errors: { username: 'username is required' } })

FlashGlobals(View)
assert.equal(View.$globals.getErrorFor('username'), 'username is required')
assert.equal(View.resolve('getErrorFor')('username'), 'username is required')
})

test('return error from a plain object', (assert) => {
const View = {
flashMessages: { username: 'virk', errors: { username: 'username is required' } },
$globals: {},
global: function (name, callback) {
this.$globals[name] = callback.bind(this)
}
}
View
.setData('flashMessages', { username: 'virk', errors: { username: 'username is required' } })

FlashGlobals(View)
assert.equal(View.$globals.getErrorFor('username'), 'username is required')
assert.equal(View.resolve('getErrorFor')('username'), 'username is required')
})
})

0 comments on commit 670d24c

Please sign in to comment.