Skip to content

Instantly share code, notes, and snippets.

@dualyticalchemy
Created February 7, 2021 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dualyticalchemy/e4c0831accf7a42b1e8773f667b4efec to your computer and use it in GitHub Desktop.
Save dualyticalchemy/e4c0831accf7a42b1e8773f667b4efec to your computer and use it in GitHub Desktop.
const { getElement } = require('./utils/getElement.js');
const { getScope } = require('./utils/getScope.js');
const { load } = require('./utils/load.js');
const config = async () => {
cy.log(__dirname);
const platformConf = await load(__dirname, '../../tests/conf/platform.conf.json');
cy.log(platformConf);
return platformConf;
};
const { has } = require('../../tests/e2e/utils/has.js')(config(), 'homeUnitTabletConfig');
const attr = (sel, name, app) => cy.get(sel)
.should('have.attr', name, app);
const tabletContext = 1;
const phoneContext = 0;
function Given ({text}) {
return new Cypress.Promise(function (acc, rej) {
const e = text.match(/\#([A-Za-z\-]+)/);
cy.log(e);
if (!e || !e.length) {
return rej(e);
}
expect(e).to.exist;
const state = e[0].split('#');
cy.visit(`/#/${state && state[1]}`);
attr('html', 'ng-app', 'ems');
acc(e);
});
}
function When ({text, context}) {
return new Cypress.Promise(function (acc, rej) {
const e = text.match(/When ([A-Za-z0-9]+).*([A-Za-z0-9]+)/g);
if (!e || !e.length) {
return rej(e);
}
expect(e).to.be.an('array');
const Subject = e[0];
expect(Subject).to.have.string('I');
const phrase = e.join('');
const args = phrase.split(' ');
const action = args[2] || 'click';
const label = phrase.split('"');
const strippedLabel = label[1];
cy.log(`@phrase: ${ e.join().split(/\"/)[0] }`);
cy.log(`@action: ${ action }`);
cy.log(`@label: ${ strippedLabel }`);
if (action.includes('add') || action.includes('create')) {
}
if (action.includes('read') || action.includes('open')) {
}
if (action.includes('update')) {
}
if (action.includes('delete')) {
}
if (action.includes('close')) {
}
if (action.includes('swipe')) {
}
if (action.includes('proceed')) {
}
if (Subject.includes('I') && action.includes('click')) {
cy.log('@diagnostic: Some next affordance exists on the page as a link under the primary click or tap capability model');
expect(getElement('a')).to.exist;
// var btn = cy.get(`[aria-label="${strippedLabel}"]`)
// .eq(0)
// .should('have.attr', 'aria-label', strippedLabel)
// .parent();
// @TODO for the hamburger menu, we have to look in the tablet context
// since we use a directive conditionally displaying tablet specific
// behaviors; we can think of aria-* attributes as the placeholders where
// ubiquitous language (https://www.martinfowler.com/bliki/BoundedContext.html)
// is expressed; such labels are like a tunnel through which domain experts
// can call behaviors
// btn = cy.get(`[role="button"][aria-label="${strippedLabel}"]`)
// .eq(tabletContext)
// .should('have.attr', 'aria-label', strippedLabel);
cy.log(`@diagnostic "${action}" (or some other service word[0], like "perform", "sees", DOM event names, ubiquitous language, etc.) maps to "${action}()"`);
cy.log('---');
cy.log([
'[0] "We could then speculate that service words are products of the social ',
'system, whereas content words are realizations (representations) of ',
'cognitive system macrostates." ("Modeling Hypermedia-Based Communication". ',
'2006. https://arxiv.org/abs/cs/0605122)'].join(''));
cy.wait(3000);
cy.log(context);
cy.log(config());
cy.log('has("iPad"):', has('iPad'));
//if ('iPad') {
cy.get('body').within(() => {
cy
//.get('zoll-tablet-view')
.get(`[aria-label="${strippedLabel}"]`)
[action]({ multiple: true, force: true });
cy.wait(1000);
});
//}
// cy.get('zoll-mobile-view').within(($list) => {
// cy.get(`[aria-label="${strippedLabel}"]`)
// .parent()
// [action]();
// cy.wait(1000);
// });
// });
}
acc(e);
});
}
function Then ({given, when}) {
expect(when).to.be.an('array');
return function (res) {
const str = res.match(/should see ([A-Za-z0-9\s\"]+)/);
const label = str[1].split('"');
cy.log(`@diagnostic Outcome: ${str[0]}`);
expect(label).to.be.an('array');
expect(label[1].trim()).to.be.a('string');
//cy.contains(label[1].trim())
//.should('have.attr', 'ng-click');
return new Cypress.Promise((acc, rej) => {
if (given) {
expect(given).to.exist;
}
cy.log(`@validEnd: ${res.replace(/Given|When|Then/g, "@event:").split()}`);
// @TODO we could "harden" some tests such that the body class is checked to
// determine if what was "given" implies a different body class page
// name under the "Then" condition; we might assume all anchors
// necessarily invoke the $stateChangeStart in our app implementation
// @example
// ```javascript
// cy.get('body').should('not.have.class', given[1]);
// ```
acc({
res: res,
given: given[1],
when: when,
body: cy.get('body')
.should('have.class', 'platform-ready')
});
});
}
}
export {
Given,
When,
Then
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment