Guest User

Untitled

a guest
Aug 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. const _ = require('lodash');
  2. let suite;
  3.  
  4. beforeEach(() => {
  5. suite = {
  6. sampleData: [1, 2, 3],
  7. sampleFunc: jest.fn(value => value),
  8. sampleDataLength: 3
  9. };
  10. });
  11.  
  12. afterEach(() => {
  13. suite.sampleFunc.mockClear();
  14. suite = null;
  15. });
  16.  
  17. describe('lodash wrapper', () => {
  18. it('toArray', () => {
  19. const chain = _(suite.sampleData)
  20. .map(value => suite.sampleFunc(value))
  21. .toArray();
  22.  
  23. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleDataLength); // 0 times
  24.  
  25. console.log(chain.size());
  26. const first = _.first(chain);
  27.  
  28. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleDataLength); // 3 times
  29. });
  30.  
  31. it('value', () => {
  32. const chain = _(suite.sampleData)
  33. .map(value => suite.sampleFunc(value))
  34. .value();
  35.  
  36. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleDataLength);
  37.  
  38. console.log(chain.length);
  39. const first = _.first(chain);
  40.  
  41. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleDataLength);
  42. });
  43. });
  44.  
  45. describe('chain', () => {
  46. it('toArray', () => {
  47. const chain = _.chain(suite.sampleData)
  48. .map(value => suite.sampleFunc(value))
  49. .toArray();
  50.  
  51. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleData.length); // 0 times
  52.  
  53. console.log(chain.size());
  54. const first = _.first(chain);
  55.  
  56. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleData.length); // called 6 times instead of 3
  57. });
  58.  
  59. it('value', () => {
  60. const chain = _.chain(suite.sampleData)
  61. .map(value => suite.sampleFunc(value))
  62. .value();
  63.  
  64. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleDataLength);
  65.  
  66. console.log(chain.length);
  67. const first = _.first(chain);
  68.  
  69. expect(suite.sampleFunc).toHaveBeenCalledTimes(suite.sampleDataLength);
  70. });
  71. });
Add Comment
Please, Sign In to add comment