|
| 1 | +const fetcher = jest.fn() |
| 2 | +jest.mock('../lib/fetch', () => ({ fetch: fetcher })) |
| 3 | + |
| 4 | +import { createTestAnalytics } from './test-helpers/create-test-analytics' |
| 5 | + |
| 6 | +describe('disable', () => { |
| 7 | + it('should dispatch callbacks and emit an http request, even if disabled', async () => { |
| 8 | + const analytics = createTestAnalytics({ |
| 9 | + disable: true, |
| 10 | + }) |
| 11 | + const emitterCb = jest.fn() |
| 12 | + analytics.on('http_request', emitterCb) |
| 13 | + await new Promise((resolve) => |
| 14 | + analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve) |
| 15 | + ) |
| 16 | + expect(emitterCb).toBeCalledTimes(1) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should call fetch if disabled is false', async () => { |
| 20 | + const analytics = createTestAnalytics({ |
| 21 | + disable: false, |
| 22 | + }) |
| 23 | + await new Promise((resolve) => |
| 24 | + analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve) |
| 25 | + ) |
| 26 | + expect(fetcher).toBeCalled() |
| 27 | + }) |
| 28 | + it('should not call fetch if disabled is true', async () => { |
| 29 | + const analytics = createTestAnalytics({ |
| 30 | + disable: true, |
| 31 | + }) |
| 32 | + await new Promise((resolve) => |
| 33 | + analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve) |
| 34 | + ) |
| 35 | + expect(fetcher).not.toBeCalled() |
| 36 | + }) |
| 37 | +}) |
0 commit comments