|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | 
|  | 3 | + * or more contributor license agreements. Licensed under the Elastic License; | 
|  | 4 | + * you may not use this file except in compliance with the Elastic License. | 
|  | 5 | + */ | 
|  | 6 | + | 
|  | 7 | +import { httpServiceMock } from '../../../../../src/core/public/mocks'; | 
|  | 8 | +import { FeatureUsageService } from './feature_usage_service'; | 
|  | 9 | + | 
|  | 10 | +describe('FeatureUsageService', () => { | 
|  | 11 | +  let http: ReturnType<typeof httpServiceMock.createSetupContract>; | 
|  | 12 | +  let service: FeatureUsageService; | 
|  | 13 | + | 
|  | 14 | +  beforeEach(() => { | 
|  | 15 | +    http = httpServiceMock.createSetupContract(); | 
|  | 16 | +    service = new FeatureUsageService(); | 
|  | 17 | +  }); | 
|  | 18 | + | 
|  | 19 | +  describe('#setup', () => { | 
|  | 20 | +    describe('#register', () => { | 
|  | 21 | +      it('calls the endpoint with the correct parameters', async () => { | 
|  | 22 | +        const setup = service.setup({ http }); | 
|  | 23 | +        await setup.register('my-feature', 'platinum'); | 
|  | 24 | +        expect(http.post).toHaveBeenCalledTimes(1); | 
|  | 25 | +        expect(http.post).toHaveBeenCalledWith('/internal/licensing/feature_usage/register', { | 
|  | 26 | +          body: JSON.stringify({ | 
|  | 27 | +            featureName: 'my-feature', | 
|  | 28 | +            licenseType: 'platinum', | 
|  | 29 | +          }), | 
|  | 30 | +        }); | 
|  | 31 | +      }); | 
|  | 32 | +    }); | 
|  | 33 | +  }); | 
|  | 34 | + | 
|  | 35 | +  describe('#start', () => { | 
|  | 36 | +    describe('#notifyUsage', () => { | 
|  | 37 | +      it('calls the endpoint with the correct parameters', async () => { | 
|  | 38 | +        service.setup({ http }); | 
|  | 39 | +        const start = service.start({ http }); | 
|  | 40 | +        await start.notifyUsage('my-feature', 42); | 
|  | 41 | + | 
|  | 42 | +        expect(http.post).toHaveBeenCalledTimes(1); | 
|  | 43 | +        expect(http.post).toHaveBeenCalledWith('/internal/licensing/feature_usage/notify', { | 
|  | 44 | +          body: JSON.stringify({ | 
|  | 45 | +            featureName: 'my-feature', | 
|  | 46 | +            lastUsed: 42, | 
|  | 47 | +          }), | 
|  | 48 | +        }); | 
|  | 49 | +      }); | 
|  | 50 | + | 
|  | 51 | +      it('correctly convert dates', async () => { | 
|  | 52 | +        service.setup({ http }); | 
|  | 53 | +        const start = service.start({ http }); | 
|  | 54 | + | 
|  | 55 | +        const now = new Date(); | 
|  | 56 | + | 
|  | 57 | +        await start.notifyUsage('my-feature', now); | 
|  | 58 | + | 
|  | 59 | +        expect(http.post).toHaveBeenCalledTimes(1); | 
|  | 60 | +        expect(http.post).toHaveBeenCalledWith('/internal/licensing/feature_usage/notify', { | 
|  | 61 | +          body: JSON.stringify({ | 
|  | 62 | +            featureName: 'my-feature', | 
|  | 63 | +            lastUsed: now.getTime(), | 
|  | 64 | +          }), | 
|  | 65 | +        }); | 
|  | 66 | +      }); | 
|  | 67 | +    }); | 
|  | 68 | +  }); | 
|  | 69 | +}); | 
0 commit comments