|
| 1 | +import { it, describe, expect, beforeEach } from '@jest/globals'; |
| 2 | +import { isLimitRateHit } from './isLimitRateHit'; |
| 3 | +import type { LimitRate } from '../../types/LimitRate'; |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + localStorage.clear(); |
| 7 | +}); |
| 8 | + |
| 9 | +describe('limit rate is disabed', () => { |
| 10 | + it('empty limit rate options', () => { |
| 11 | + const limitRate: LimitRate = {}; |
| 12 | + |
| 13 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('throttle is 0', () => { |
| 17 | + const limitRate: LimitRate = { |
| 18 | + throttle: 0, |
| 19 | + }; |
| 20 | + |
| 21 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('no record', () => { |
| 25 | + const limitRate: LimitRate = { |
| 26 | + id: 'app', |
| 27 | + throttle: 1000, |
| 28 | + }; |
| 29 | + |
| 30 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('no hit limit', async () => { |
| 34 | + const limitRate: LimitRate = { |
| 35 | + id: 'app', |
| 36 | + throttle: 100, |
| 37 | + }; |
| 38 | + |
| 39 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 40 | + |
| 41 | + await new Promise((r) => setTimeout(r, 150)); |
| 42 | + |
| 43 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('not same page or ID', async () => { |
| 47 | + const limitRate: LimitRate = { |
| 48 | + throttle: 100, |
| 49 | + }; |
| 50 | + |
| 51 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 52 | + |
| 53 | + location.replace('/new-form'); |
| 54 | + |
| 55 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('limit rate is enabled', () => { |
| 60 | + it('hit limit', async () => { |
| 61 | + const limitRate: LimitRate = { |
| 62 | + id: 'app', |
| 63 | + throttle: 100, |
| 64 | + }; |
| 65 | + |
| 66 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 67 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeTruthy(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('restore after page refresh and hit limit', () => { |
| 71 | + const limitRate: LimitRate = { |
| 72 | + throttle: 100, |
| 73 | + }; |
| 74 | + |
| 75 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 76 | + |
| 77 | + location.reload(); |
| 78 | + |
| 79 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeTruthy(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('next page refresh and hit limit', () => { |
| 83 | + const limitRate: LimitRate = { |
| 84 | + id: 'app', |
| 85 | + throttle: 100, |
| 86 | + }; |
| 87 | + |
| 88 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeFalsy(); |
| 89 | + |
| 90 | + location.replace('/new-form'); |
| 91 | + |
| 92 | + expect(isLimitRateHit(localStorage, location.pathname, limitRate)).toBeTruthy(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments