@@ -12,18 +12,16 @@ import React from 'react';
1212import { render } from '@testing-library/react' ;
1313import { LDFlagChangeset , LDOptions , LDUser } from 'launchdarkly-js-client-sdk' ;
1414import initLDClient from './initLDClient' ;
15- import { fetchFlags } from './utils' ;
16- import { AsyncProviderConfig , defaultReactOptions , LDReactOptions } from './types' ;
15+ import { AsyncProviderConfig , LDReactOptions } from './types' ;
1716import { Consumer } from './context' ;
1817import asyncWithLDProvider from './asyncWithLDProvider' ;
1918
2019const clientSideID = 'deadbeef' ;
2120const user : LDUser = { key : 'yus' , name : 'yus ng' } ;
2221const App = ( ) => < > My App</ > ;
2322const mockInitLDClient = initLDClient as jest . Mock ;
24- const mockFetchFlags = fetchFlags as jest . Mock ;
25- const mockFlags = { testFlag : true , anotherTestFlag : true } ;
26- let mockLDClient : { on : jest . Mock } ;
23+ const rawFlags = { 'test-flag' : true , 'another-test-flag' : true } ;
24+ let mockLDClient : { on : jest . Mock ; off : jest . Mock ; variation : jest . Mock } ;
2725
2826const renderWithConfig = async ( config : AsyncProviderConfig ) => {
2927 const LDProvider = await asyncWithLDProvider ( config ) ;
@@ -43,13 +41,15 @@ describe('asyncWithLDProvider', () => {
4341 on : jest . fn ( ( e : string , cb : ( ) => void ) => {
4442 cb ( ) ;
4543 } ) ,
44+ off : jest . fn ( ) ,
45+ // tslint:disable-next-line: no-unsafe-any
46+ variation : jest . fn ( ( _ : string , v ) => v ) ,
4647 } ;
4748
4849 mockInitLDClient . mockImplementation ( ( ) => ( {
4950 ldClient : mockLDClient ,
51+ flags : rawFlags ,
5052 } ) ) ;
51-
52- mockFetchFlags . mockReturnValue ( mockFlags ) ;
5353 } ) ;
5454
5555 afterEach ( ( ) => {
@@ -72,7 +72,7 @@ describe('asyncWithLDProvider', () => {
7272 const reactOptions : LDReactOptions = { useCamelCaseFlagKeys : false } ;
7373 await asyncWithLDProvider ( { clientSideID, user, options, reactOptions } ) ;
7474
75- expect ( mockInitLDClient ) . toHaveBeenCalledWith ( clientSideID , user , reactOptions , options , undefined ) ;
75+ expect ( mockInitLDClient ) . toHaveBeenCalledWith ( clientSideID , user , options , undefined ) ;
7676 } ) ;
7777
7878 test ( 'subscribe to changes on mount' , async ( ) => {
@@ -97,17 +97,17 @@ describe('asyncWithLDProvider', () => {
9797 } ) ;
9898
9999 test ( 'subscribe to changes with kebab-case' , async ( ) => {
100- mockFetchFlags . mockReturnValue ( { 'another-test-flag' : true , 'test-flag' : true } ) ;
101100 mockInitLDClient . mockImplementation ( ( ) => ( {
102101 ldClient : mockLDClient ,
102+ flags : rawFlags ,
103103 } ) ) ;
104104 mockLDClient . on . mockImplementation ( ( e : string , cb : ( c : LDFlagChangeset ) => void ) => {
105105 cb ( { 'another-test-flag' : { current : false , previous : true } , 'test-flag' : { current : false , previous : true } } ) ;
106106 } ) ;
107107 const receivedNode = await renderWithConfig ( { clientSideID, reactOptions : { useCamelCaseFlagKeys : false } } ) ;
108108
109109 expect ( mockLDClient . on ) . toHaveBeenNthCalledWith ( 1 , 'change' , expect . any ( Function ) ) ;
110- expect ( receivedNode ) . toHaveTextContent ( '{"another- test-flag":false,"test-flag":false}' ) ;
110+ expect ( receivedNode ) . toHaveTextContent ( '{"test-flag":false,"another- test-flag":false}' ) ;
111111 } ) ;
112112
113113 test ( 'consecutive flag changes gets stored in context correctly' , async ( ) => {
@@ -180,31 +180,31 @@ describe('asyncWithLDProvider', () => {
180180 } ) ;
181181
182182 test ( 'ldClient is initialised correctly with target flags' , async ( ) => {
183- mockFetchFlags . mockReturnValue ( { devTestFlag : true , launchDoggly : true } ) ;
184183 mockInitLDClient . mockImplementation ( ( ) => ( {
185184 ldClient : mockLDClient ,
185+ flags : rawFlags ,
186186 } ) ) ;
187187
188188 const options : LDOptions = { } ;
189- const flags = { 'dev- test-flag' : false , 'launch-doggly ' : false } ;
189+ const flags = { 'test-flag' : false } ;
190190 const receivedNode = await renderWithConfig ( { clientSideID, user, options, flags } ) ;
191191
192- expect ( mockInitLDClient ) . toHaveBeenCalledWith ( clientSideID , user , defaultReactOptions , options , flags ) ;
193- expect ( receivedNode ) . toHaveTextContent ( '{"devTestFlag":true,"launchDoggly ":true}' ) ;
192+ expect ( mockInitLDClient ) . toHaveBeenCalledWith ( clientSideID , user , options , flags ) ;
193+ expect ( receivedNode ) . toHaveTextContent ( '{"testFlag ":true}' ) ;
194194 } ) ;
195195
196196 test ( 'only updates to subscribed flags are pushed to the Provider' , async ( ) => {
197- mockFetchFlags . mockReturnValue ( { testFlag : 2 } ) ;
198197 mockInitLDClient . mockImplementation ( ( ) => ( {
199198 ldClient : mockLDClient ,
199+ flags : rawFlags ,
200200 } ) ) ;
201201 mockLDClient . on . mockImplementation ( ( e : string , cb : ( c : LDFlagChangeset ) => void ) => {
202- cb ( { 'test-flag' : { current : 3 , previous : 2 } , 'another-test-flag' : { current : false , previous : true } } ) ;
202+ cb ( { 'test-flag' : { current : false , previous : true } , 'another-test-flag' : { current : false , previous : true } } ) ;
203203 } ) ;
204204 const options : LDOptions = { } ;
205- const subscribedFlags = { 'test-flag' : 1 } ;
205+ const subscribedFlags = { 'test-flag' : true } ;
206206 const receivedNode = await renderWithConfig ( { clientSideID, user, options, flags : subscribedFlags } ) ;
207207
208- expect ( receivedNode ) . toHaveTextContent ( '{"testFlag":3 }' ) ;
208+ expect ( receivedNode ) . toHaveTextContent ( '{"testFlag":false }' ) ;
209209 } ) ;
210210} ) ;
0 commit comments