@@ -1575,3 +1575,190 @@ describe('flag config polling interval config', () => {
15751575 expect ( client [ 'config' ] . flagConfigPollingIntervalMillis ) . toEqual ( 900000 ) ;
15761576 } ) ;
15771577} ) ;
1578+
1579+ describe ( 'setTracksAssignment' , ( ) => {
1580+ beforeEach ( async ( ) => {
1581+ await safeGlobal . localStorage . clear ( ) ;
1582+ jest . restoreAllMocks ( ) ;
1583+ } ) ;
1584+
1585+ afterEach ( ( ) => {
1586+ jest . restoreAllMocks ( ) ;
1587+ } ) ;
1588+
1589+ test ( 'setTracksAssignment() no call ok' , async ( ) => {
1590+ const client = new ExperimentClient ( API_KEY , { } ) ;
1591+
1592+ // Mock the evaluationApi.getVariants method
1593+ const getVariantsSpy = jest . spyOn (
1594+ ( client as any ) . evaluationApi ,
1595+ 'getVariants' ,
1596+ ) ;
1597+ getVariantsSpy . mockResolvedValue ( {
1598+ 'test-flag' : { key : 'on' , value : 'on' } ,
1599+ } ) ;
1600+
1601+ // Fetch variants to trigger the API call
1602+ await client . fetch ( testUser ) ;
1603+
1604+ // Verify getVariants was called with trackingOption: 'track'
1605+ expect ( getVariantsSpy ) . toHaveBeenCalledWith (
1606+ expect . objectContaining ( {
1607+ user_id : testUser . user_id ,
1608+ library : expect . stringContaining ( 'experiment-js-client' ) ,
1609+ } ) ,
1610+ expect . objectContaining ( {
1611+ trackingOption : undefined ,
1612+ timeoutMillis : expect . any ( Number ) ,
1613+ } ) ,
1614+ ) ;
1615+ } ) ;
1616+
1617+ test ( 'setTracksAssignment() sets trackingOption to track and getVariants is called with correct options' , async ( ) => {
1618+ const client = new ExperimentClient ( API_KEY , { } ) ;
1619+
1620+ // Mock the evaluationApi.getVariants method
1621+ const getVariantsSpy = jest . spyOn (
1622+ ( client as any ) . evaluationApi ,
1623+ 'getVariants' ,
1624+ ) ;
1625+ getVariantsSpy . mockResolvedValue ( {
1626+ 'test-flag' : { key : 'on' , value : 'on' } ,
1627+ } ) ;
1628+
1629+ // Set track assignment event to true
1630+ await client . setTracksAssignment ( true ) ;
1631+
1632+ // Fetch variants to trigger the API call
1633+ await client . fetch ( testUser ) ;
1634+
1635+ // Verify getVariants was called with trackingOption: 'track'
1636+ expect ( getVariantsSpy ) . toHaveBeenCalledWith (
1637+ expect . objectContaining ( {
1638+ user_id : testUser . user_id ,
1639+ library : expect . stringContaining ( 'experiment-js-client' ) ,
1640+ } ) ,
1641+ expect . objectContaining ( {
1642+ trackingOption : 'track' ,
1643+ timeoutMillis : expect . any ( Number ) ,
1644+ } ) ,
1645+ ) ;
1646+
1647+ // Set track assignment event to false
1648+ await client . setTracksAssignment ( false ) ;
1649+
1650+ // Fetch variants to trigger the API call
1651+ await client . fetch ( testUser ) ;
1652+
1653+ // Verify getVariants was called with trackingOption: 'no-track'
1654+ expect ( getVariantsSpy ) . toHaveBeenCalledWith (
1655+ expect . objectContaining ( {
1656+ user_id : testUser . user_id ,
1657+ library : expect . stringContaining ( 'experiment-js-client' ) ,
1658+ } ) ,
1659+ expect . objectContaining ( {
1660+ trackingOption : 'no-track' ,
1661+ timeoutMillis : expect . any ( Number ) ,
1662+ } ) ,
1663+ ) ;
1664+ } ) ;
1665+
1666+ test ( 'setTracksAssignment persists the setting to storage' , async ( ) => {
1667+ const client = new ExperimentClient ( API_KEY , { } ) ;
1668+
1669+ // Set track assignment event to true
1670+ await client . setTracksAssignment ( true ) ;
1671+
1672+ // Create a new client instance to verify persistence
1673+ const client2 = new ExperimentClient ( API_KEY , { } ) ;
1674+
1675+ // Mock the evaluationApi.getVariants method for the second client
1676+ const getVariantsSpy = jest . spyOn (
1677+ ( client2 as any ) . evaluationApi ,
1678+ 'getVariants' ,
1679+ ) ;
1680+ getVariantsSpy . mockResolvedValue ( {
1681+ 'test-flag' : { key : 'on' , value : 'on' } ,
1682+ } ) ;
1683+
1684+ // Fetch variants with the second client
1685+ await client2 . fetch ( testUser ) ;
1686+
1687+ // Verify the setting was persisted and loaded by the second client
1688+ expect ( getVariantsSpy ) . toHaveBeenCalledWith (
1689+ expect . objectContaining ( {
1690+ user_id : testUser . user_id ,
1691+ library : expect . stringContaining ( 'experiment-js-client' ) ,
1692+ } ) ,
1693+ expect . objectContaining ( {
1694+ trackingOption : 'track' ,
1695+ timeoutMillis : expect . any ( Number ) ,
1696+ } ) ,
1697+ ) ;
1698+ } ) ;
1699+
1700+ test ( 'multiple calls to setTracksAssignment uses the latest setting' , async ( ) => {
1701+ const client = new ExperimentClient ( API_KEY , { } ) ;
1702+
1703+ // Mock the evaluationApi.getVariants method
1704+ const getVariantsSpy = jest . spyOn (
1705+ ( client as any ) . evaluationApi ,
1706+ 'getVariants' ,
1707+ ) ;
1708+ getVariantsSpy . mockResolvedValue ( {
1709+ 'test-flag' : { key : 'off' , value : 'off' } ,
1710+ } ) ;
1711+
1712+ // Set track assignment event to true, then false
1713+ await client . setTracksAssignment ( true ) ;
1714+ await client . setTracksAssignment ( false ) ;
1715+
1716+ // Fetch variants to trigger the API call
1717+ await client . fetch ( testUser ) ;
1718+
1719+ // Verify getVariants was called with the latest setting (no-track)
1720+ expect ( getVariantsSpy ) . toHaveBeenCalledWith (
1721+ expect . objectContaining ( {
1722+ user_id : testUser . user_id ,
1723+ library : expect . stringContaining ( 'experiment-js-client' ) ,
1724+ } ) ,
1725+ expect . objectContaining ( {
1726+ trackingOption : 'no-track' ,
1727+ timeoutMillis : expect . any ( Number ) ,
1728+ } ) ,
1729+ ) ;
1730+ } ) ;
1731+
1732+ test ( 'setTracksAssignment preserves other existing options while updating trackingOption' , async ( ) => {
1733+ const client = new ExperimentClient ( API_KEY , { } ) ;
1734+
1735+ // Mock the evaluationApi.getVariants method
1736+ const getVariantsSpy = jest . spyOn (
1737+ ( client as any ) . evaluationApi ,
1738+ 'getVariants' ,
1739+ ) ;
1740+ getVariantsSpy . mockResolvedValue ( {
1741+ 'test-flag' : { key : 'on' , value : 'on' } ,
1742+ } ) ;
1743+
1744+ // Set track assignment event to true
1745+ await client . setTracksAssignment ( true ) ;
1746+
1747+ // Fetch variants with specific flag keys to ensure other options are preserved
1748+ const fetchOptions = { flagKeys : [ 'test-flag' ] } ;
1749+ await client . fetch ( testUser , fetchOptions ) ;
1750+
1751+ // Verify getVariants was called with both trackingOption and flagKeys
1752+ expect ( getVariantsSpy ) . toHaveBeenCalledWith (
1753+ expect . objectContaining ( {
1754+ user_id : testUser . user_id ,
1755+ library : expect . stringContaining ( 'experiment-js-client' ) ,
1756+ } ) ,
1757+ expect . objectContaining ( {
1758+ trackingOption : 'track' ,
1759+ flagKeys : [ 'test-flag' ] ,
1760+ timeoutMillis : expect . any ( Number ) ,
1761+ } ) ,
1762+ ) ;
1763+ } ) ;
1764+ } ) ;
0 commit comments