11/**
2- * Copyright 2018-2019, Optimizely
2+ * Copyright 2018-2019, 2023, Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http ://www.apache.org/licenses/LICENSE-2.0
8+ * https ://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
@@ -170,7 +170,7 @@ describe('<OptimizelyExperiment>', () => {
170170 await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'correct variation' ) ) ;
171171 } ) ;
172172
173- it ( 'should render using <OptimizelyVariation default>' , async ( ) => {
173+ it ( 'should render using <OptimizelyVariation default> in last position ' , async ( ) => {
174174 const { container } = render (
175175 < OptimizelyProvider optimizely = { optimizelyMock } >
176176 < OptimizelyExperiment experiment = "experiment1" >
@@ -196,6 +196,56 @@ describe('<OptimizelyExperiment>', () => {
196196 await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'default variation' ) ) ;
197197 } ) ;
198198
199+ it ( 'should NOT render using <OptimizelyVariation default> in first position when matching variation' , async ( ) => {
200+ const { container } = render (
201+ < OptimizelyProvider optimizely = { optimizelyMock } >
202+ < OptimizelyExperiment experiment = "experiment1" >
203+ < OptimizelyVariation default >
204+ < span data-testid = "variation-key" > default variation</ span >
205+ </ OptimizelyVariation >
206+ < OptimizelyVariation variation = "variationResult" >
207+ < span data-testid = "variation-key" > matching variation</ span >
208+ </ OptimizelyVariation >
209+ </ OptimizelyExperiment >
210+ </ OptimizelyProvider >
211+ ) ;
212+
213+ // while it's waiting for onReady()
214+ expect ( container . innerHTML ) . toBe ( '' ) ;
215+
216+ // Simulate client becoming ready
217+ resolver . resolve ( { success : true } ) ;
218+
219+ await optimizelyMock . onReady ( ) ;
220+
221+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matching variation' ) ) ;
222+ } ) ;
223+
224+ it ( 'should render using <OptimizelyVariation default> in first position when NO matching variation' , async ( ) => {
225+ const { container } = render (
226+ < OptimizelyProvider optimizely = { optimizelyMock } >
227+ < OptimizelyExperiment experiment = "experiment1" >
228+ < OptimizelyVariation default >
229+ < span data-testid = "variation-key" > default variation</ span >
230+ </ OptimizelyVariation >
231+ < OptimizelyVariation variation = "otherVariation" >
232+ < span data-testid = "variation-key" > other non-matching variation</ span >
233+ </ OptimizelyVariation >
234+ </ OptimizelyExperiment >
235+ </ OptimizelyProvider >
236+ ) ;
237+
238+ // while it's waiting for onReady()
239+ expect ( container . innerHTML ) . toBe ( '' ) ;
240+
241+ // Simulate client becoming ready
242+ resolver . resolve ( { success : true } ) ;
243+
244+ await optimizelyMock . onReady ( ) ;
245+
246+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'default variation' ) ) ;
247+ } ) ;
248+
199249 it ( 'should render an empty string when no default or matching variation is provided' , async ( ) => {
200250 const { container } = render (
201251 < OptimizelyProvider optimizely = { optimizelyMock } >
0 commit comments