File: test/functional/step_definitions/LoadAvailabilityDataStepDef.js
/**
* All step defs related to places
*/
'use strict';
var _ = require('lodash');
var Q = require('q');
var Promise = require('bluebird');
var expect = require('chai').expect;
var stepDefinitionsWrapper = function () {
var createSelector = function createSelector(placeMap, properties, rowName, segmentName) {
var startingSelector = ['g[name="', segmentName.toUpperCase(), '"]', ' g[name="', rowName.toUpperCase(), '"]'].join('');
var placeKeys = [],
selectors = [];
_.forEach(placeMap, function (row) {
placeKeys = Object.keys(row);
var type = _.remove(placeKeys, function (name) {
return _.isNaN(parseInt(name));
});
properties.push(row[type]);
var placeKeyLength = placeKeys.length,
index;
for (index = 0; index < placeKeyLength; index++) {
var placeName = placeKeys[index];
var selector = [startingSelector, ' path.place[name="', placeName, '"]'].join('');
selectors.push(selector);
}
});
return selectors;
};
var validatePlaceAvailability = function validatePlaceAvailability(values, rows) {
var valueLength = values.length,
rowLength = rows[0].length,
index,
isAvailabilityMatch = true;
for (index = 0; isAvailabilityMatch && index < valueLength; index++) {
var row = Math.floor(index / rowLength);
var col = (index % rowLength) + 1;
var value = rows[row][col];
switch (value) {
case 'o':
isAvailabilityMatch = values[index] === 'OPEN';
break;
case 'x':
isAvailabilityMatch = values[index] === 'SOLD';
break;
case 'r':
isAvailabilityMatch = values[index] === 'RESALE'
break;
case 'a':
isAvailabilityMatch = values[index] === 'true';
break;
case 'n':
isAvailabilityMatch = values[index] === 'false';
break;
default:
break;
}
}!isAvailabilityMatch && console.error('Expected values:', rows.toString(), 'actual values:', values.toString());
expect(isAvailabilityMatch).to.equal(true);
};
this.Given(/^all of the seats in section (\w+) row (\w+) are (\w+)$/, function (sectionName, rowName, invType, callback) {
//var status = invType === 'available';
var options = {
section: sectionName,
row: rowName,
invType: invType
};
this.browser.setRowAvailability(options).call(callback);
});
this.Then(/^the places in row (\w+) section (\w+) should be:/, function (rowName, segmentName, data, callback) {
var self = this,
placeMap = data.hashes(),
properties = [],
selectors = createSelector(placeMap, properties, rowName, segmentName);
var limit = selectors.length / properties.length;
Promise.map(selectors, function (selector, index) {
var key = Math.floor(index / limit);
return self.browser.getAttribute(selector, properties[key]);
})
.then(function (values) {
var rows = data.rows();
validatePlaceAvailability(values, rows);
callback();
})
.catch(callback);
});
};
module.exports = stepDefinitionsWrapper;