consul/ui-v2/tests/steps.js

219 lines
7.4 KiB
JavaScript
Raw Normal View History

2018-05-11 12:47:21 +00:00
/* eslint no-console: "off" */
import yadda from './helpers/yadda';
import { currentURL, click, triggerKeyEvent } from '@ember/test-helpers';
import getDictionary from '@hashicorp/ember-cli-api-double/dictionary';
2018-05-11 12:47:21 +00:00
import pages from 'consul-ui/tests/pages';
import api from 'consul-ui/tests/helpers/api';
const create = function(number, name, value) {
// don't return a promise here as
// I don't need it to wait
api.server.createList(name, number, value);
};
var currentPage;
export default function(assert) {
return (
yadda.localisation.English.library(
getDictionary(function(model, cb) {
switch (model) {
case 'datacenter':
case 'datacenters':
case 'dcs':
model = 'dc';
break;
case 'services':
model = 'service';
break;
case 'nodes':
model = 'node';
break;
case 'kvs':
model = 'kv';
break;
case 'acls':
model = 'acl';
break;
}
cb(null, model);
}, yadda)
)
// doubles
.given(['$number $model model', '$number $model models'], function(number, model) {
return create(number, model);
})
.given(['$number $model model with the value "$value"'], function(number, model, value) {
return create(number, model, value);
})
.given(
['$number $model model[s]? from yaml\n$yaml', '$number $model model from json\n$json'],
function(number, model, data) {
return create(number, model, data);
}
)
// interactions
.when('I visit the $name page', function(name) {
currentPage = pages[name];
return currentPage.visit();
})
.when('I visit the $name page for the "$id" $model', function(name, id, model) {
currentPage = pages[name];
return currentPage.visit({
[model]: id,
});
})
.when(
['I visit the $name page for yaml\n$yaml', 'I visit the $name page for json\n$json'],
function(name, data) {
currentPage = pages[name];
return pages[name].visit(data);
}
)
.when('I click "$selector"', function(selector) {
return click(selector);
})
.when('I click $prop on the $component', function(prop, component) {
// Collection
2018-06-04 13:53:51 +00:00
var obj;
2018-05-11 12:47:21 +00:00
if (typeof currentPage[component].objectAt === 'function') {
2018-06-04 13:53:51 +00:00
obj = currentPage[component].objectAt(0);
2018-05-11 12:47:21 +00:00
} else {
2018-06-04 13:53:51 +00:00
obj = currentPage[component];
}
const func = obj[prop].bind(obj);
try {
return func();
} catch (e) {
console.error(e);
throw new Error(`The '${prop}' property on the '${component}' page object doesn't exist`);
2018-05-11 12:47:21 +00:00
}
})
.when('I submit', function(selector) {
return currentPage.submit();
})
.then('I fill in "$name" with "$value"', function(name, value) {
return currentPage.fillIn(name, value);
})
.then(['I fill in with yaml\n$yaml', 'I fill in with json\n$json'], function(data) {
return Object.keys(data).reduce(function(prev, item, i, arr) {
return prev.fillIn(item, data[item]);
}, currentPage);
})
.then(['I type with yaml\n$yaml'], function(data) {
const keys = Object.keys(data);
return keys
.reduce(function(prev, item, i, arr) {
return prev.fillIn(item, data[item]);
}, currentPage)
.then(function() {
return Promise.all(
keys.map(function(item) {
return triggerKeyEvent(`[name="${item}"]`, 'keyup', 83);
})
);
});
})
// debugging helpers
.then('print the current url', function(url) {
console.log(currentURL());
return Promise.resolve();
})
.then('log the "$text"', function(text) {
console.log(text);
return Promise.resolve();
})
.then('pause for $milliseconds', function(milliseconds) {
return new Promise(function(resolve) {
setTimeout(resolve, milliseconds);
});
})
// assertions
.then('a $method request is made to "$url" with the body from yaml\n$yaml', function(
method,
url,
data
) {
const request = api.server.history[api.server.history.length - 2];
assert.equal(
request.method,
method,
2018-06-07 11:35:37 +00:00
`Expected the request method to be ${method}, was ${request.method}`
2018-05-11 12:47:21 +00:00
);
2018-06-07 11:35:37 +00:00
assert.equal(request.url, url, `Expected the request url to be ${url}, was ${request.url}`);
2018-05-11 12:47:21 +00:00
const body = JSON.parse(request.requestBody);
Object.keys(data).forEach(function(key, i, arr) {
assert.equal(
body[key],
data[key],
2018-06-07 11:35:37 +00:00
`Expected the payload to contain ${key} to equal ${body[key]}, ${key} was ${data[key]}`
2018-05-11 12:47:21 +00:00
);
});
})
.then('a $method request is made to "$url" with the body "$body"', function(
method,
url,
data
) {
const request = api.server.history[api.server.history.length - 2];
assert.equal(
request.method,
method,
2018-06-07 11:35:37 +00:00
`Expected the request method to be ${method}, was ${request.method}`
2018-05-11 12:47:21 +00:00
);
2018-06-07 11:35:37 +00:00
assert.equal(request.url, url, `Expected the request url to be ${url}, was ${request.url}`);
2018-05-11 12:47:21 +00:00
const body = request.requestBody;
assert.equal(
body,
data,
2018-06-07 11:35:37 +00:00
`Expected the request body to be ${body}, was ${request.requestBody}`
2018-05-11 12:47:21 +00:00
);
})
2018-06-04 13:53:51 +00:00
.then('a $method request is made to "$url"', function(method, url) {
const request = api.server.history[api.server.history.length - 2];
assert.equal(
request.method,
method,
2018-06-07 11:35:37 +00:00
`Expected the request method to be ${method}, was ${request.method}`
2018-06-04 13:53:51 +00:00
);
2018-06-07 11:35:37 +00:00
assert.equal(request.url, url, `Expected the request url to be ${url}, was ${request.url}`);
2018-06-04 13:53:51 +00:00
})
2018-05-11 12:47:21 +00:00
.then('the url should be $url', function(url) {
const current = currentURL();
2018-06-07 11:35:37 +00:00
assert.equal(current, url, `Expected the url to be ${url} was ${current}`);
2018-05-11 12:47:21 +00:00
})
.then(['I see $num $model', 'I see $num $model model', 'I see $num $model models'], function(
num,
model
) {
const len = currentPage[`${model}s`].filter(function(item) {
return item.isVisible;
}).length;
2018-06-07 11:35:37 +00:00
assert.equal(len, num, `Expected ${num} ${model}s, saw ${len}`);
2018-05-11 12:47:21 +00:00
})
.then(['I see $num $model model with the $property "$value"'], function(
num,
model,
property,
value
) {
const len = currentPage[`${model}s`].filter(function(item) {
return item.isVisible && item[property] == value;
}).length;
assert.equal(
len,
num,
2018-06-07 11:35:37 +00:00
`Expected ${num} ${model}s with ${property} set to "${value}", saw ${len}`
2018-05-11 12:47:21 +00:00
);
})
.then(['I see $property on the $component'], function(property, component) {
assert.ok(currentPage[component][property], `Expected to see ${property} on ${component}`);
})
.then(['I see $property'], function(property, component) {
assert.ok(currentPage[property], `Expected to see ${property}`);
})
.then('ok', function() {
assert.ok(true);
})
);
}