Skip to content

Instantly share code, notes, and snippets.

Avatar

Bob MacNeal bobmacneal

View GitHub Profile
View truncate.js
export const truncate = (phrase, maxLength) => {
if (phrase.length > maxLength) {
return `${phrase.substring(0, maxLength - 4)} ...`
} else {
return phrase
}
}
View camelCaseToSnakeCase.js
export const camelCaseToSnakeCase = (item) => {
return item.replace(/[\w]([A-Z])/g, (m) => {
return m[0] + '_' + m[1]
}).toLowerCase()
}
View diffBetweenTwoObjects.js
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
}
@bobmacneal
bobmacneal / CypressSockJsNodeStub.js
Created Dec 23, 2019
Cypress Command to Stub /sockjs-node/info?t=*
View CypressSockJsNodeStub.js
Cypress.Commands.add('mockSockJs', () => {
const url = '/sockjs-node/info?t=*'
cy.server()
cy.route({
method: 'GET',
url: url,
response: 'fixture:sockJs.json',
})
})
@bobmacneal
bobmacneal / CypressRequestToJson.js
Last active Dec 23, 2019
A function to write a Cypress.io fixture (for subsequent XHR mocking) via a "temporarily live" API request
View CypressRequestToJson.js
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: {
You can’t perform that action at this time.