factory.integ.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { _consumePoints, POINTS_THRESHOLD } from './factory';
  2. // const mocks = vi.hoisted(() => {
  3. // return {
  4. // comsumeMock: vi.fn(),
  5. // };
  6. // });
  7. // vi.mock('rate-limiter-flexible', () => ({
  8. // RateLimiterMongo: vi.fn().mockImplementation(() => {
  9. // return {
  10. // consume: mocks.comsumeMock,
  11. // };
  12. // }),
  13. // }));
  14. describe('factory.ts', () => {
  15. it('test', async() => {
  16. // setup
  17. const method = 'GET';
  18. const key = 'test-key';
  19. const maxRequests = 1;
  20. const res = await _consumePoints('GET', key, { method, maxRequests });
  21. expect(res).toBe(1);
  22. });
  23. // describe('_consumePoints()', () => {
  24. // it('Should consume points as 1 * THRESHOLD if maxRequest: 1 is specified', async() => {
  25. // // setup
  26. // const method = 'GET';
  27. // const key = 'test-key';
  28. // const maxRequests = 1;
  29. // // when
  30. // const pointsToConsume = POINTS_THRESHOLD / maxRequests;
  31. // await _consumePoints(method, key, { method, maxRequests });
  32. // // then
  33. // expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
  34. // expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
  35. // });
  36. // it('Should consume points as 2 * THRESHOLD if maxRequest: 2 is specified', async() => {
  37. // // setup
  38. // const method = 'GET';
  39. // const key = 'test-key';
  40. // const maxRequests = 2;
  41. // // when
  42. // const pointsToConsume = POINTS_THRESHOLD / maxRequests;
  43. // await _consumePoints(method, key, { method, maxRequests });
  44. // // then
  45. // expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
  46. // expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
  47. // });
  48. // it('Should consume points as 3 * THRESHOLD if maxRequest: 3 is specified', async() => {
  49. // // setup
  50. // const method = 'GET';
  51. // const key = 'test-key';
  52. // const maxRequests = 3;
  53. // // when
  54. // const pointsToConsume = POINTS_THRESHOLD / maxRequests;
  55. // await _consumePoints(method, key, { method, maxRequests });
  56. // // then
  57. // expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
  58. // expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
  59. // });
  60. // it('Should consume points as 500 * THRESHOLD if maxRequest: 500 is specified', async() => {
  61. // // setup
  62. // const method = 'GET';
  63. // const key = 'test-key';
  64. // const maxRequests = 500;
  65. // // when
  66. // const pointsToConsume = POINTS_THRESHOLD / maxRequests;
  67. // await _consumePoints(method, key, { method, maxRequests });
  68. // // then
  69. // expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
  70. // expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
  71. // });
  72. // });
  73. });