2019-05-01 18:09:29 +00:00
|
|
|
export default function(scenario, find, fillIn, triggerKeyEvent, currentPage) {
|
2019-06-04 16:55:10 +00:00
|
|
|
const dont = `( don't| shouldn't| can't)?`;
|
2019-02-21 13:05:05 +00:00
|
|
|
const fillInElement = function(page, name, value) {
|
|
|
|
const cm = document.querySelector(`textarea[name="${name}"] + .CodeMirror`);
|
|
|
|
if (cm) {
|
2019-06-04 16:55:10 +00:00
|
|
|
if (!cm.CodeMirror.options.readOnly) {
|
|
|
|
cm.CodeMirror.setValue(value);
|
|
|
|
} else {
|
|
|
|
throw new Error(`The ${name} editor is set to readonly`);
|
|
|
|
}
|
2019-02-21 13:05:05 +00:00
|
|
|
return page;
|
|
|
|
} else {
|
|
|
|
return page.fillIn(name, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
scenario
|
|
|
|
.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 fillInElement(prev, item, data[item]);
|
|
|
|
}, currentPage());
|
|
|
|
})
|
|
|
|
.then(
|
2019-05-01 18:09:29 +00:00
|
|
|
[
|
2019-06-04 16:55:10 +00:00
|
|
|
`I${dont} fill in the $property form with yaml\n$yaml`,
|
|
|
|
`I${dont} fill in $property with yaml\n$yaml`,
|
|
|
|
`I${dont} fill in the $property with yaml\n$yaml`,
|
|
|
|
`I${dont} fill in the property form with json\n$json`,
|
2019-05-01 18:09:29 +00:00
|
|
|
|
2019-06-04 16:55:10 +00:00
|
|
|
`I${dont} fill in the $property form on the $component component with yaml\n$yaml`,
|
|
|
|
`I${dont} fill in the $property form on the $component component with json\n$json`,
|
|
|
|
`I${dont} fill in the $property on the $component component with yaml\n$yaml`,
|
2019-05-01 18:09:29 +00:00
|
|
|
],
|
2019-06-04 16:55:10 +00:00
|
|
|
function(negative, property, component, data, next) {
|
2019-05-01 18:09:29 +00:00
|
|
|
try {
|
|
|
|
switch (true) {
|
|
|
|
case typeof component === 'string':
|
|
|
|
property = `${component}.${property}`;
|
|
|
|
// fallthrough
|
|
|
|
case typeof data === 'undefined':
|
|
|
|
data = component;
|
|
|
|
// // fallthrough
|
|
|
|
// case typeof property !== 'string':
|
|
|
|
// data = property;
|
|
|
|
}
|
|
|
|
let obj;
|
|
|
|
try {
|
|
|
|
obj = find(property);
|
|
|
|
} catch (e) {
|
|
|
|
obj = currentPage();
|
|
|
|
}
|
|
|
|
return Object.keys(data).reduce(function(prev, item, i, arr) {
|
|
|
|
const name = `${obj.prefix || property}[${item}]`;
|
2019-06-04 16:55:10 +00:00
|
|
|
if (negative) {
|
|
|
|
try {
|
|
|
|
fillInElement(prev, name, data[item]);
|
|
|
|
throw new TypeError(`${item} is editable`);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TypeError) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fillInElement(prev, name, data[item]);
|
|
|
|
}
|
2019-05-01 18:09:29 +00:00
|
|
|
}, obj);
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-21 13:05:05 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(['I type "$text" into "$selector"'], function(text, selector) {
|
|
|
|
return fillIn(selector, text);
|
|
|
|
})
|
|
|
|
.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); // TODO: This is 's', be more generic
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|