Skip to content

Instantly share code, notes, and snippets.

@saradornblaser
Created December 10, 2018 18:12
Show Gist options
  • Save saradornblaser/0381656660367b8fad324a101c38f920 to your computer and use it in GitHub Desktop.
Save saradornblaser/0381656660367b8fad324a101c38f920 to your computer and use it in GitHub Desktop.
const chai = require('chai');
const expect = chai.expect;
const chaiThings = require('chai-things');
const chaiSpies = require('chai-spies');
const sinon = require('sinon');
chai.use(chaiThings);
chai.use(chaiSpies);
// Models
const db = require('../server/models');
const Campus = db.models.campus;
const Student = db.models.student;
// Routes
const app = require('../server/app');
const agent = require('supertest')(app);
// Components
import React from 'react';
import enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16'
enzyme.configure({ adapter: new Adapter() });
import { CampusInput } from '../client/components/CampusInput'
// Redux
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import configureMockStore from 'redux-mock-store';
import thunkMiddleware from 'redux-thunk';
const middlewares = [thunkMiddleware];
const mockStore = configureMockStore(middlewares);
const initialState = {
campuses: [],
selectedCampus: {},
students: [],
};
const store = mockStore(initialState);
import reducer from '../client/redux/reducer';
import { ADD_CAMPUS } from '../client/redux/constants';
import { postCampus, addCampus } from '../client/redux/actions';
// Utils
const utils = require('../utils');
describe('Tier Three', () => {
describe('`throttle` utility method', () => {
xit('takes a function and a number (throttle time - in milliseconds) and returns a throttled function', () => {
const funcToThrottle = (name) => {
console.log(`What up ${name}`);
}
const throttleTime = 50;
const throttledFunction = utils.throttle(funcToThrottle, throttleTime);
expect(throttledFunction).to.be.a('function');
});
describe('returned throttled function', () => {
it('runs the original function and upon invocation passes it the same arguments', () => {
const spiedFunction = chai.spy();
const throttleTime = 50;
const throttledFunction = utils.throttle(spiedFunction, throttleTime);
// `throttle` itself does not call the original function
expect(spiedFunction).not.to.have.been.called;
throttledFunction(1, 'omri', 'polar bear');
// calling the throttled function (the result of `throttle`) calls the original function
expect(spiedFunction).to.have.been.called.once;
expect(spiedFunction).to.have.been.called.with.exactly(1, 'omri', 'polar bear');
})
xit('ensures that multiple function calls within the throttling period will not invoke the original function', (done) => {
const spiedFunction = chai.spy();
const throttleTime = 50;
const throttledFunction = utils.throttle(spiedFunction, throttleTime);
expect(spiedFunction).not.to.have.been.called;
throttledFunction();
expect(spiedFunction).to.have.been.called.once;
throttledFunction();
throttledFunction();
// wait period has not been long enough, so the original function is not called a second time
expect(spiedFunction).to.have.been.called.once;
setTimeout(() => {
throttledFunction();
throttledFunction();
// wait period still has not been long enough, so the original function is not called a second time
expect(spiedFunction).to.have.been.called.once;
setTimeout(() => {
// previous invocations of the throttled function do NOT trigger the original to be called later
expect(spiedFunction).to.have.been.called.once;
done();
}, 70);
}, 40);
});
xit('can invoke the original function after the throttling period is over', (done) => {
const spiedFunction = chai.spy();
const throttleTime = 50;
const throttledFunction = utils.throttle(spiedFunction, throttleTime);
throttledFunction();
setTimeout(() => {
throttledFunction();
// wait period has been long enough, so the original function is called a second time
expect(spiedFunction).to.have.been.called.twice;
throttledFunction();
// wait period has not been long enough, so the original function is NOT called a third time
expect(spiedFunction).to.have.been.called.twice;
setTimeout(() => {
throttledFunction();
// wait period has been long enough, so the original function is called a third time
expect(spiedFunction).to.have.been.called.exactly(3);
setTimeout(() => {
// previous invocations of the throttled function do NOT trigger the original to be called later
expect(spiedFunction).to.have.been.called.exactly(3);
done();
}, 60);
}, 60);
}, 60);
});
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment