Skip to content

Commit

Permalink
UITEST-67 fix setCirculationRules (#35)
Browse files Browse the repository at this point in the history
Returning a value from a Promise isn't that hard if you know what you're
doing, but I had no idea what I was doing. The light went on while I was
doing the dishes. Maybe I should spend more time washing the dishes.

Refs UITEST-67
  • Loading branch information
zburke authored Jul 26, 2019
1 parent b559d9b commit 0c91592
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,29 +505,50 @@ module.exports.checkout = (nightmare, done, itemBarcode, userBarcode) => {
.catch(done);
};

module.exports.setCirculationRules = (nightmare, done, newRules) => {
let oldRules = '';
nightmare
/**
* Visit Settings > Circulation > Circulation rules and save newRules
* as the circulation rules, returning the old rules in a Promise that
* may be resolved like so:
let cachedRules = '';
setCirculationRules(nightmare, rules)
.then(oldRules => {
cachedRules = oldRules;
})
.then(done)
.catch(done);
});
* If `done()` receives any arguments, it expects an error first. This means
* if you want to ignore the return value from setCirculationRules in your
* then() block, you need to explicitly configure it not to receive the value:
setCirculationRules(nightmare, cachedRules)
.then(() => done())
.catch(done);
});
* calling .then(done) would implicitly pass the return value to done,
* causing Nightmare to throw the error, "done() invoked with non-Error".
*/
module.exports.setCirculationRules = (nightmare, newRules) => {
return nightmare
.wait('a[href="/settings/circulation"]')
.click('a[href="/settings/circulation"]')
.wait('a[href="/settings/circulation/rules"]')
.click('a[href="/settings/circulation/rules"]')
.wait('#form-loan-rules')
.wait(1000)
.wait('.CodeMirror')
.evaluate((rules) => {
const defaultRules = document.getElementsByClassName('CodeMirror')[0].CodeMirror.getValue();
const oldRules = document.getElementsByClassName('CodeMirror')[0].CodeMirror.getValue();
document.getElementsByClassName('CodeMirror')[0].CodeMirror.setValue(rules);
return defaultRules;
return oldRules;
}, newRules)
.then((rules) => {
.then(rules => {
nightmare
.wait('#clickable-save-loan-rules')
.click('#clickable-save-loan-rules')
.wait(() => !document.querySelector('#OverlayContainer div[class^="calloutBase"]'))
.then(done)
.catch(done);
oldRules = rules;
})
.catch(done);
return oldRules;
.wait(() => !document.querySelector('#OverlayContainer div[class^="calloutBase"]'));
return rules;
});
};

0 comments on commit 0c91592

Please sign in to comment.