Skip to content

Instantly share code, notes, and snippets.

View olusoladavid's full-sized avatar

Olusola Oguntimehin olusoladavid

View GitHub Profile
@olusoladavid
olusoladavid / test.js
Created July 25, 2022 11:39 — forked from apieceofbart/test.js
Async testing with jest fake timers and promises
// Let's say you have a function that does some async operation inside setTimeout (think of polling for data)
function runInterval(callback, interval = 1000) {
setInterval(async () => {
const results = await Promise.resolve(42) // this might fetch some data from server
callback(results)
}, interval)
}
// Goal: We want to test that function - make sure our callback was called
@olusoladavid
olusoladavid / cypress.js
Created March 1, 2019 16:20
Waiting for requests in Cypress
describe('A sample that waits till a request is posted', () => {
before(() => {
cy.visit('/');
})
beforeEach(() => {
cy.server();
cy.route('POST', '/api/v1/requests').as('requestData');
});
function doPost(e){
var req = null;
try {
req = queryStringToJSON(e.postData.contents);
/* Extract the action from the request text */
var action = getAction(req);
if (!actionIsValid(action)) throw 'Hi. You sent an invalid command';
/* Extract the action arguments from the request text */
var args = getActionArgs(req);
args.forEach(function(arg, index) {
var EMAIL_COL = 0; // 0 is the index of the email column in the data range
var NUM_COL = 1;
/* This function finds a value in a sheet and returns that value */
function findValueInSheet(key, keyColumn, valueColumn, _sheet) {
var sheet = _sheet;
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
var selectedRow = null;
function doPost(e){
var req = null;
try {
req = queryStringToJSON(e.postData.contents);
/* Extract the action from the request text */
var action = getAction(req);
if (!actionIsValid(action)) throw 'Hi. You sent an invalid command';
/* Extract the action arguments from the request text */
var args = getActionArgs(req);
args.forEach(function(arg, index) {
var actions = {
'check': {
'requiredArgsCount': 1,
'helpText': [{ 'text': 'Type `/listbot check <youremail>` to check your status' }],
'args': {
0: [/\w+@\w+\.com$/, 'Oops. It appears you did not supply an email. Please check']
},
'execute': getUserWaitStatus
}
};
/**
* Each action has a required number of arguments which will be parsed
* from the text sent by the user
* Each action has a helpText which can be used later with a help action
* Each action has an argument list starting with 0
* Each argument in the argument list has a regex validator and
* a validation error message
* The regex used for email in this example does not cover all
* cases as it's mostly for demo purposes
* Each action has a core execution function that handles the main job of the task
{
token: 'gIkuvaNzQIHg97ATvDxqgjtO',
team_id: 'T0001',
team_domain: 'example',
enterprise_id: 'E0001',
enterprise_name: 'Globular%20Construct%20Inc',
channel_id: 'C2147483705',
channel_name: 'test'
user_id: 'U2147483697',
user_name: 'Steve',
@olusoladavid
olusoladavid / doPost1.js
Last active December 19, 2018 00:27
doPost gist
function doPost(e){
var req = null;
try {
/* Slack's request comes in a query string format. We convert
it to a javascript object to make it easy to use */
req = queryStringToJSON(e.postData.contents);
} catch (error) {
}
}