|
| 1 | +/** |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { validate } from './'; |
| 18 | +import { INVALID_USER_PROFILE_SERVICE } from 'error_message'; |
| 19 | +import { OptimizelyError } from '../../error/optimizly_error'; |
| 20 | + |
| 21 | +describe('validate', () => { |
| 22 | + it("should throw if the instance does not provide a 'lookup' function", () => { |
| 23 | + const missingLookupFunction = { |
| 24 | + save: function() {}, |
| 25 | + }; |
| 26 | + |
| 27 | + expect(() => validate(missingLookupFunction)).toThrowError(OptimizelyError); |
| 28 | + |
| 29 | + try { |
| 30 | + validate(missingLookupFunction); |
| 31 | + } catch(err) { |
| 32 | + expect(err).instanceOf(OptimizelyError); |
| 33 | + expect(err.baseMessage).toBe(INVALID_USER_PROFILE_SERVICE); |
| 34 | + expect(err.params).toEqual(["Missing function 'lookup'"]); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it("should throw if 'lookup' is not a function", () => { |
| 39 | + const lookupNotFunction = { |
| 40 | + save: function() {}, |
| 41 | + lookup: 'notGonnaWork', |
| 42 | + }; |
| 43 | + |
| 44 | + expect(() => validate(lookupNotFunction)).toThrowError(OptimizelyError); |
| 45 | + |
| 46 | + try { |
| 47 | + validate(lookupNotFunction); |
| 48 | + } catch(err) { |
| 49 | + expect(err).instanceOf(OptimizelyError); |
| 50 | + expect(err.baseMessage).toBe(INVALID_USER_PROFILE_SERVICE); |
| 51 | + expect(err.params).toEqual(["Missing function 'lookup'"]); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it("should throw if the instance does not provide a 'save' function", () => { |
| 56 | + const missingSaveFunction = { |
| 57 | + lookup: function() {}, |
| 58 | + }; |
| 59 | + |
| 60 | + expect(() => validate(missingSaveFunction)).toThrowError(OptimizelyError); |
| 61 | + |
| 62 | + try { |
| 63 | + validate(missingSaveFunction); |
| 64 | + } catch(err) { |
| 65 | + expect(err).instanceOf(OptimizelyError); |
| 66 | + expect(err.baseMessage).toBe(INVALID_USER_PROFILE_SERVICE); |
| 67 | + expect(err.params).toEqual(["Missing function 'save'"]); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it("should throw if 'save' is not a function", () => { |
| 72 | + const saveNotFunction = { |
| 73 | + lookup: function() {}, |
| 74 | + save: 'notGonnaWork', |
| 75 | + }; |
| 76 | + |
| 77 | + expect(() => validate(saveNotFunction)).toThrowError(OptimizelyError); |
| 78 | + |
| 79 | + try { |
| 80 | + validate(saveNotFunction); |
| 81 | + } catch(err) { |
| 82 | + expect(err).instanceOf(OptimizelyError); |
| 83 | + expect(err.baseMessage).toBe(INVALID_USER_PROFILE_SERVICE); |
| 84 | + expect(err.params).toEqual(["Missing function 'save'"]); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return true if the instance is valid', () => { |
| 89 | + const validInstance = { |
| 90 | + save: function() {}, |
| 91 | + lookup: function() {}, |
| 92 | + }; |
| 93 | + |
| 94 | + expect(validate(validInstance)).toBe(true); |
| 95 | + }); |
| 96 | +}); |
0 commit comments