Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Created July 8, 2020 00:14
Show Gist options
  • Save joegaudet/c1948a537161bd71338a9cf2ed38af6b to your computer and use it in GitHub Desktop.
Save joegaudet/c1948a537161bd71338a9cf2ed38af6b to your computer and use it in GitHub Desktop.
Some cypress commands to work with ember-data
// use ember-expose-global and replace MyAppName with you
Cypress.Commands.add("getStore", () => {
return cy
.window()
.then((win) => cy.wrap(win.MyAppName.__container__.lookup('service:store')));
});
/**
* Create Record
* @param {string} Record Name
* @param {object} attrs
*/
Cypress.Commands.add("createRecord",
(type, attrs = {}) =>
cy.getStore()
.then((store) => cy.wrap(store.createRecord(type, attrs).save()))
);
/**
* Create Record
* @param {string} Record Name
* @param {string} Record id
* @param {object} options
*/
Cypress.Commands.add("findRecord", (type, id, options = {}) =>
cy.getStore()
.then((store) => cy.wrap(store.findRecord(type, id, options)))
);
/**
* Update Record
* @param {string} Record Name
* @param {string} Record id
* @param {object} attributes
* @param {object} find_options
*/
Cypress.Commands.add("updateRecord", (type, id, attrs, options = {}) => {
return cy
.getStore()
.then((store) => cy.wrap(store.findRecord(type, id, options)))
.then((record) => {
record.setProperties(attrs);
return cy.wrap(record.save());
});
});
/**
* Query an array of records
* @param {string} Record Name
* @param {object} attributes
* @param {object} find_options
*/
Cypress.Commands.add("query", (type, options) => {
return cy
.getStore()
.then((store) => cy.wrap(store.query(type, options)));
});
/**
* Peek in the store for a particular record
*
* @param {string} Record Name
* @param {number} Record Id
*/
Cypress.Commands.add("peekRecord", (type, id) =>
cy.getStore().then((store) => store.peekRecord(type, id)));
/**
* Peek in the store for all records of a given type
*
* @param {string} Record Name
*/
Cypress.Commands.add("peekAll", (type) =>
cy.getStore().then((store) => store.peekAll(type)));
/**
* Peek in the store for the most recent of a given type
*
* @param {string} Record Name
*/
Cypress.Commands.add("lastRecord",
(type) => cy.peekAll(type)
.then((records) => records.get('lastObject'))
);
/**
* Peek in the store for the least recent of a given type
*
* @param {string} Record Name
*/
Cypress.Commands.add("firstRecord",
(type) => cy.peekAll(type)
.then((records) => records.get('firstObject'))
);
/**
* Destroy the last record of a given type
*
* @param {string} Record Name
*/
Cypress.Commands.add("destroyLast",
(type) => cy.lastRecord(type)
.then((lastRecord) => cy.destroyRecord(lastRecord))
);
/**
* Find and destroy a record
*
* @param {string} Record Name
* @param {id} Record Id
*/
Cypress.Commands.add("findAndDestroyRecord",
(type, id) =>
cy.findRecord(type, id)
.then((record) => cy.destroyRecord(record))
);
/**
* Destroy a given record
*
* @param {string} Record Name
* @param {id} Record Id
*/
Cypress.Commands.add("destroyRecord", (record) => cy.wrap(record.destroyRecord()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment