Skip to content

Instantly share code, notes, and snippets.

View bobmacneal's full-sized avatar

Bob MacNeal bobmacneal

View GitHub Profile
@bobmacneal
bobmacneal / CypressRequestToJson.js
Last active December 23, 2019 14:00
A function to write a Cypress.io fixture (for subsequent XHR mocking) via a "temporarily live" API request
describe('Cypress Request to JSON Writer', () => {
const JSON_PLACEHOLDER_API_URL = 'https://jsonplaceholder.typicode.com'
const FILENAME = 'users.json'
it('Writes XHR to a JSON fixture file', () => {
const url = `${JSON_PLACEHOLDER_API_URL}/users` // the API call
// Use cy.request to hit the API url. Add bearer token if auth required for API.
cy.request({
url: url,
auth: {
@bobmacneal
bobmacneal / CypressSockJsNodeStub.js
Created December 23, 2019 15:12
Cypress Command to Stub /sockjs-node/info?t=*
Cypress.Commands.add('mockSockJs', () => {
const url = '/sockjs-node/info?t=*'
cy.server()
cy.route({
method: 'GET',
url: url,
response: 'fixture:sockJs.json',
})
})
import isEqual from 'lodash/isEqual'
import isObject from 'lodash/isObject'
import transform from 'lodash/transform'
export const diffBetweenTwoObjects = (object, base) => {
const changes = (object, base) => {
return transform(object, (result, value, key) => {
if (!isEqual(value, base[key])) {
result[key] = (isObject(value) && isObject(base[key])) ? changes(value, base[key]) : value
}
export const camelCaseToSnakeCase = (item) => {
return item.replace(/[\w]([A-Z])/g, (m) => {
return m[0] + '_' + m[1]
}).toLowerCase()
}
export const truncate = (phrase, maxLength) => {
if (phrase.length > maxLength) {
return `${phrase.substring(0, maxLength - 4)} ...`
} else {
return phrase
}
}