Skip to content

Commit

Permalink
Route: fix share and improve tests
Browse files Browse the repository at this point in the history
the assertion for validationContext (additional internal test)
  • Loading branch information
maxkoryukov committed Feb 12, 2017
1 parent 2fddfd1 commit 74fee3b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 101 deletions.
200 changes: 100 additions & 100 deletions src/resources/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,105 @@ const debug = require("debug")("route4me")
const utils = require("./../utils")
const errors = require("./../errors")

class CustomInternalPostProcessing {
/**
* Handle `duplicate` output
*
* @private
*
* @example <caption>Expected input</caption>
* Sample = {
* "optimization_problem_id":"672998C4269918AFF461E5A691BAB8D0",
* "success":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {string} - The ID of duplicate
*/
static duplicate(data, ctx, res) {
if (
!data
|| "boolean" !== typeof data["success"]
|| "string" !== typeof data["optimization_problem_id"]
) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data["success"]) {
return data["optimization_problem_id"]
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}

static pullIn(data, ctx, res) {
if (
!data
|| "boolean" !== typeof data["success"]
) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data["success"]) {
return true
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}

/**
* merge post-processor
*
* @private
*
* @example
* Sample = {
* "optimization_problem_id":"672998C4269918AFF461E5A691BAB8D0",
* "success":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {string} - The ID of merged Route
*/
static merge(data, ctx, res) {
return CustomInternalPostProcessing.duplicate(data, ctx, res)
}

/**
* unlinkAddress
*
* @private
*
* @example
* Sample = {
* "deleted":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {boolean} - Success
*/
static unlinkAddress(data, ctx, res) {
if (!data || "boolean" !== typeof data.deleted) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data.deleted) {
return true
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}
}

// ===================================

/**
Expand Down Expand Up @@ -349,7 +448,7 @@ class Routes {
path: "/actions/route/share_route.php",
qs,
form,
validationContext: CustomInternalPostProcessing.share,
validationContext: utils.CustomInternalPostProcessing.fromJsonWithStatus,
}, callback)
}

Expand Down Expand Up @@ -455,103 +554,4 @@ class Routes {
}
}

class CustomInternalPostProcessing {
/**
* Handle `duplicate` output
*
* @private
*
* @example <caption>Expected input</caption>
* Sample = {
* "optimization_problem_id":"672998C4269918AFF461E5A691BAB8D0",
* "success":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {string} - The ID of duplicate
*/
static duplicate(data, ctx, res) {
if (
!data
|| "boolean" !== typeof data["success"]
|| "string" !== typeof data["optimization_problem_id"]
) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data["success"]) {
return data["optimization_problem_id"]
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}

static pullIn(data, ctx, res) {
if (
!data
|| "boolean" !== typeof data["success"]
) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data["success"]) {
return true
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}

/**
* merge post-processor
*
* @private
*
* @example
* Sample = {
* "optimization_problem_id":"672998C4269918AFF461E5A691BAB8D0",
* "success":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {string} - The ID of merged Route
*/
static merge(data, ctx, res) {
return CustomInternalPostProcessing.duplicate(data, ctx, res)
}

/**
* unlinkAddress
*
* @private
*
* @example
* Sample = {
* "deleted":true
* }
*
* @param {Object} data - Internal
* @param {Object} ctx - Internal
* @param {Object} res - Internal
* @return {boolean} - Success
*/
static unlinkAddress(data, ctx, res) {
if (!data || "boolean" !== typeof data.deleted) {
return new errors.Route4MeValidationError("Invalid response", data)
}

if (true === data.deleted) {
return true
}

// TODO: parse real error
return new errors.Route4MeApiError("Failed", res)
}
}

module.exports = Routes
1 change: 0 additions & 1 deletion src/resources/territories.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict"

const errors = require("./../errors")
const utils = require("./../utils")

/**
Expand Down
5 changes: 5 additions & 0 deletions src/route4me.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ class Route4Me {

qs["api_key"] = this._apiKey

if (undefined === options.validationContext) {
// this is just a protective wall
throw new errors.Route4MeError("validationContext should not be undefined")
}

let v = this._validate
let c = options.validationContext || null
if ("function" === typeof c) {
Expand Down
3 changes: 3 additions & 0 deletions test/resources/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ describe(helper.toSuiteName(__filename), () => {
},
"multipart/form-data"
)

// result expectation
expect(res).true
done()
})
})
Expand Down

0 comments on commit 74fee3b

Please sign in to comment.