hmpo-form-controller
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -163,3 +163,3 @@ /*eslint no-unused-vars: [2, {"vars": "all", "args": "none"}]*/ | ||
}, | ||
getNextStep: function (req) { | ||
getNextStep: function (req, res) { | ||
var next = this.options.next || req.path; | ||
@@ -169,3 +169,16 @@ if (req.baseUrl !== '/') { | ||
} | ||
return next; | ||
var forks = this.options.forks || []; | ||
function evalCondition(condition) { | ||
return _.isFunction(condition) ? | ||
condition(req, res) : | ||
condition.value === req.form.values[condition.field]; | ||
} | ||
// If a fork condition is met, its target supercedes the next property | ||
return forks.reduce(function (result, value) { | ||
return evalCondition(value.condition) ? | ||
req.baseUrl + value.target : | ||
result; | ||
}, next); | ||
}, | ||
@@ -172,0 +185,0 @@ getErrorStep: function (err, req) { |
{ | ||
"name": "hmpo-form-controller", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,1 +60,66 @@ # passports-form-controller | ||
By default the application of a validator is optional on empty strings. If you need to ensure a field is validated as being 9 characters long and exists then you need to use both an `exactlength` and a `required` validator. | ||
### steps config | ||
#### Handles journey forking | ||
Each step definition accepts a `next` property, the value of which is the next route in the journey. By default, when the form is successfully submitted, the next steps will load. However, there are times when it is necessary to fork from the current journey based on a users response to certain questions in a form. For such circumstances there exists the `forks` property. | ||
In this example, when the submits the form, if the field called 'example-radio' has the value 'superman', the page at '/fork-page' will load, otherwise '/next-page' will be loaded. | ||
```js | ||
'/my-page': { | ||
next: '/next-page', | ||
forks: [{ | ||
target: '/fork-page', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'superman' | ||
} | ||
}] | ||
} | ||
``` | ||
The condition property can also take a function. In the following example, if the field called 'name' is more than 30 characters in length, the page at '/fork-page' will be loaded. | ||
```js | ||
'/my-page': { | ||
next: '/next-page', | ||
forks: [{ | ||
target: '/fork-page', | ||
condition: function (req, res) { | ||
return req.form.values['name'].length > 30; | ||
} | ||
}] | ||
} | ||
``` | ||
Forks is an array and therefore each fork is interrogated in order from top to bottom. The last fork whose condition is met will assign its target to the next page variable. | ||
In this example, if the last condition resolves to true - even if the others also resolve to true - then the page at '/fork-page-three' will be loaded. The last condition to be met is always the fork used to determine the next step. | ||
```js | ||
'/my-page': { | ||
next: '/next-page', | ||
forks: [{ | ||
target: '/fork-page-one', | ||
condition: function (req, res) { | ||
return req.form.values['name'].length > 30; | ||
} | ||
}, { | ||
target: '/fork-page-two', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'superman' | ||
} | ||
}, { | ||
target: '/fork-page-three', | ||
condition: function (req, res) { | ||
return typeof req.form.values['email'] === 'undefined'; | ||
} | ||
}] | ||
} | ||
``` |
@@ -501,8 +501,7 @@ var Form = require('../../'); | ||
describe('successHandler', function () { | ||
describe('getNextStep', function () { | ||
var form, req, res; | ||
beforeEach(function () { | ||
form = new Form({ template: 'index', next: '/success' }); | ||
form = new Form({ template: 'index', next: '/next-page' }); | ||
req = request({ | ||
@@ -519,4 +518,3 @@ params: {}, | ||
it('redirects to `next` page', function () { | ||
form.successHandler(req, res); | ||
res.redirect.should.have.been.calledWith('/success'); | ||
form.getNextStep(req, res).should.be.equal('/next-page'); | ||
}); | ||
@@ -526,6 +524,133 @@ | ||
req.baseUrl = '/base'; | ||
form.successHandler(req, res); | ||
res.redirect.should.have.been.calledWith('/base/success'); | ||
form.getNextStep(req, res).should.be.equal('/base/next-page'); | ||
}); | ||
describe('forking journeys', function () { | ||
it('returns the fork target if the condition config is met', function () { | ||
req.form.values['example-radio'] = 'conditionMet'; | ||
form.options.forks = [{ | ||
target: '/target-page', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'conditionMet' | ||
} | ||
}]; | ||
form.getNextStep(req, {}).should.contain('/target-page'); | ||
}); | ||
it('returns the original next target if the condition config is not met', function () { | ||
req.form.values['example-radio'] = 'conditionNotMet'; | ||
form.options.forks = [{ | ||
target: '/target-page', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'conditionMet' | ||
} | ||
}]; | ||
form.getNextStep(req, {}).should.equal('/next-page'); | ||
}); | ||
it('returns the fork target if the condition function is met', function () { | ||
form.options.forks = [{ | ||
target: '/target-page', | ||
condition: function () { | ||
return true; | ||
} | ||
}]; | ||
form.getNextStep(req, {}).should.contain('/target-page'); | ||
}); | ||
it('returns the original next target if the condition function is not met', function () { | ||
form.options.forks = [{ | ||
target: '/target-page', | ||
condition: function () { | ||
return false; | ||
} | ||
}]; | ||
form.getNextStep(req, {}).should.equal('/next-page'); | ||
}); | ||
describe('with more than one fork', function () { | ||
describe('when the fields are the same', function () { | ||
beforeEach(function () { | ||
req.form = { values: { | ||
'example-radio': 'condition-met' | ||
}}; | ||
form.options.forks = [{ | ||
target: '/target-page', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'condition-met' | ||
} | ||
}, { | ||
target: '/target-page-2', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'condition-met' | ||
} | ||
}]; | ||
}); | ||
it('retuns the last forks\' target if each condition is met', function () { | ||
form.getNextStep(req, {}).should.contain('/target-page-2'); | ||
}); | ||
}); | ||
describe('when the fields are different', function () { | ||
beforeEach(function () { | ||
form.options.forks = [{ | ||
target: '/target-page', | ||
condition: { | ||
field: 'example-radio', | ||
value: 'conditionMet' | ||
} | ||
}, { | ||
target: '/target-page-2', | ||
condition: { | ||
field: 'example-email', | ||
value: 'conditionMet' | ||
} | ||
}]; | ||
}); | ||
it('returns the last forks\' target if each condition is met', function () { | ||
req.form = { values: { | ||
'example-radio': 'conditionMet', | ||
'example-email': 'conditionMet' | ||
}}; | ||
form.getNextStep(req, {}).should.contain('/target-page-2'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('successHandler', function () { | ||
beforeEach(function () { | ||
sinon.stub(Form.prototype, 'getNextStep'); | ||
form = new Form({ template: 'index' }); | ||
req = request({ | ||
params: {}, | ||
body: { field: 'value' }, | ||
flash: sinon.stub() | ||
}); | ||
res = { | ||
redirect: sinon.stub() | ||
}; | ||
}); | ||
afterEach(function () { | ||
Form.prototype.getNextStep.restore(); | ||
}); | ||
it('emits "complete" event', function () { | ||
@@ -532,0 +657,0 @@ form.successHandler(req, res); |
@@ -44,2 +44,2 @@ var Validators = require('../../').validators; | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79176
21
1906
125