|
| 1 | +/** |
| 2 | + * Copyright 2024 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 | + * https://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 | + |
| 17 | +/// <reference types="jest" /> |
| 18 | + |
| 19 | +//jest.mock('./client'); |
| 20 | + |
| 21 | +import React from 'react'; |
| 22 | +import { render, act } from '@testing-library/react'; |
| 23 | +import { OptimizelyProvider } from './Provider'; |
| 24 | +import { DefaultUser, ReactSDKClient, createInstance } from './client'; |
| 25 | + |
| 26 | +describe('OptimizelyProvider', () => { |
| 27 | + let mockReactClient: ReactSDKClient; |
| 28 | + const config = { |
| 29 | + datafile: {}, |
| 30 | + }; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + mockReactClient = ({ |
| 34 | + user: { |
| 35 | + id: 'test-id', |
| 36 | + attributes: {}, |
| 37 | + }, |
| 38 | + setUser: jest.fn().mockResolvedValue(undefined), |
| 39 | + } as unknown) as ReactSDKClient; |
| 40 | + }); |
| 41 | + |
| 42 | + it('should render successfully with user provided', () => { |
| 43 | + act(() => { |
| 44 | + render(<OptimizelyProvider optimizely={mockReactClient} user={{ id: 'user1' }} />); |
| 45 | + }); |
| 46 | + |
| 47 | + expect(mockReactClient.setUser).toHaveBeenCalledWith({ |
| 48 | + id: 'user1', |
| 49 | + attributes: {}, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should render successfully with userId provided', () => { |
| 54 | + act(() => { |
| 55 | + render(<OptimizelyProvider optimizely={mockReactClient} userId="user1" />); |
| 56 | + }); |
| 57 | + |
| 58 | + expect(mockReactClient.setUser).toHaveBeenCalledWith({ |
| 59 | + id: 'user1', |
| 60 | + attributes: {}, |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should render successfully without user or userId provided', () => { |
| 65 | + act(() => { |
| 66 | + render(<OptimizelyProvider optimizely={mockReactClient} />); |
| 67 | + }); |
| 68 | + |
| 69 | + expect(mockReactClient.setUser).toHaveBeenCalledWith(DefaultUser); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should render successfully with user id & attributes provided', () => { |
| 73 | + act(() => { |
| 74 | + render( |
| 75 | + <OptimizelyProvider optimizely={mockReactClient} user={{ id: 'user1', attributes: { attr1: 'value1' } }} /> |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + expect(mockReactClient.setUser).toHaveBeenCalledWith({ |
| 80 | + id: 'user1', |
| 81 | + attributes: { attr1: 'value1' }, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should succeed just userAttributes provided', () => { |
| 86 | + act(() => { |
| 87 | + render(<OptimizelyProvider optimizely={mockReactClient} userAttributes={{ attr1: 'value1' }} />); |
| 88 | + }); |
| 89 | + |
| 90 | + expect(mockReactClient.setUser).toHaveBeenCalledWith({ |
| 91 | + id: DefaultUser.id, |
| 92 | + attributes: { attr1: 'value1' }, |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments