Guest User

Untitled

a guest
Dec 10th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. const chai = require('chai');
  2. const expect = chai.expect;
  3. const chaiThings = require('chai-things');
  4. const chaiSpies = require('chai-spies');
  5. const sinon = require('sinon');
  6. chai.use(chaiThings);
  7. chai.use(chaiSpies);
  8.  
  9. // Models
  10. const db = require('../server/models');
  11. const Campus = db.models.campus;
  12. const Student = db.models.student;
  13.  
  14. // Routes
  15. const app = require('../server/app');
  16. const agent = require('supertest')(app);
  17.  
  18. // Components
  19. import React from 'react';
  20. import enzyme, { shallow } from 'enzyme';
  21. import Adapter from 'enzyme-adapter-react-16'
  22. enzyme.configure({ adapter: new Adapter() });
  23. import { CampusInput } from '../client/components/CampusInput'
  24.  
  25. // Redux
  26. import axios from 'axios';
  27. import MockAdapter from 'axios-mock-adapter';
  28. import configureMockStore from 'redux-mock-store';
  29. import thunkMiddleware from 'redux-thunk';
  30. const middlewares = [thunkMiddleware];
  31. const mockStore = configureMockStore(middlewares);
  32. const initialState = {
  33. campuses: [],
  34. selectedCampus: {},
  35. students: [],
  36. };
  37. const store = mockStore(initialState);
  38. import reducer from '../client/redux/reducer';
  39. import { ADD_CAMPUS } from '../client/redux/constants';
  40. import { postCampus, addCampus } from '../client/redux/actions';
  41.  
  42. // Utils
  43. const utils = require('../utils');
  44.  
  45. describe('Tier Three', () => {
  46.  
  47.  
  48. describe('`throttle` utility method', () => {
  49. xit('takes a function and a number (throttle time - in milliseconds) and returns a throttled function', () => {
  50. const funcToThrottle = (name) => {
  51. console.log(`What up ${name}`);
  52. }
  53. const throttleTime = 50;
  54. const throttledFunction = utils.throttle(funcToThrottle, throttleTime);
  55. expect(throttledFunction).to.be.a('function');
  56. });
  57.  
  58. describe('returned throttled function', () => {
  59.  
  60. it('runs the original function and upon invocation passes it the same arguments', () => {
  61. const spiedFunction = chai.spy();
  62. const throttleTime = 50;
  63. const throttledFunction = utils.throttle(spiedFunction, throttleTime);
  64. // `throttle` itself does not call the original function
  65. expect(spiedFunction).not.to.have.been.called;
  66. throttledFunction(1, 'omri', 'polar bear');
  67. // calling the throttled function (the result of `throttle`) calls the original function
  68. expect(spiedFunction).to.have.been.called.once;
  69. expect(spiedFunction).to.have.been.called.with.exactly(1, 'omri', 'polar bear');
  70. })
  71.  
  72. xit('ensures that multiple function calls within the throttling period will not invoke the original function', (done) => {
  73. const spiedFunction = chai.spy();
  74. const throttleTime = 50;
  75. const throttledFunction = utils.throttle(spiedFunction, throttleTime);
  76. expect(spiedFunction).not.to.have.been.called;
  77. throttledFunction();
  78. expect(spiedFunction).to.have.been.called.once;
  79. throttledFunction();
  80. throttledFunction();
  81. // wait period has not been long enough, so the original function is not called a second time
  82. expect(spiedFunction).to.have.been.called.once;
  83. setTimeout(() => {
  84. throttledFunction();
  85. throttledFunction();
  86. // wait period still has not been long enough, so the original function is not called a second time
  87. expect(spiedFunction).to.have.been.called.once;
  88. setTimeout(() => {
  89. // previous invocations of the throttled function do NOT trigger the original to be called later
  90. expect(spiedFunction).to.have.been.called.once;
  91. done();
  92. }, 70);
  93. }, 40);
  94. });
  95.  
  96. xit('can invoke the original function after the throttling period is over', (done) => {
  97. const spiedFunction = chai.spy();
  98. const throttleTime = 50;
  99. const throttledFunction = utils.throttle(spiedFunction, throttleTime);
  100. throttledFunction();
  101. setTimeout(() => {
  102. throttledFunction();
  103. // wait period has been long enough, so the original function is called a second time
  104. expect(spiedFunction).to.have.been.called.twice;
  105. throttledFunction();
  106. // wait period has not been long enough, so the original function is NOT called a third time
  107. expect(spiedFunction).to.have.been.called.twice;
  108. setTimeout(() => {
  109. throttledFunction();
  110. // wait period has been long enough, so the original function is called a third time
  111. expect(spiedFunction).to.have.been.called.exactly(3);
  112. setTimeout(() => {
  113. // previous invocations of the throttled function do NOT trigger the original to be called later
  114. expect(spiedFunction).to.have.been.called.exactly(3);
  115. done();
  116. }, 60);
  117. }, 60);
  118. }, 60);
  119. });
  120.  
  121. });
  122.  
  123. });
  124. })
Add Comment
Please, Sign In to add comment