11import { createConsentManager } from './consent' ;
2- import { httpSend } from './transport' ;
2+ import type { HttpSend } from './transport' ;
33
4- jest . mock ( './transport' , ( ) => ( {
5- httpSend : jest . fn ( ) . mockResolvedValue ( true ) ,
6- } ) ) ;
7-
8- const mockHttpSend = httpSend as jest . MockedFunction < typeof httpSend > ;
4+ function createMockSend ( ) : jest . MockedFunction < HttpSend > {
5+ return jest . fn < ReturnType < HttpSend > , Parameters < HttpSend > > ( ) . mockResolvedValue ( { ok : true } ) ;
6+ }
97
108function createMockQueue ( ) {
119 return {
@@ -29,19 +27,22 @@ beforeEach(() => {
2927describe ( 'createConsentManager' , ( ) => {
3028 it ( 'defaults to none when no initial level provided' , ( ) => {
3129 const queue = createMockQueue ( ) ;
32- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' ) ;
30+ const send = createMockSend ( ) ;
31+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' ) ;
3332 expect ( manager . level ) . toBe ( 'none' ) ;
3433 } ) ;
3534
3635 it ( 'uses the initial level when provided' , ( ) => {
3736 const queue = createMockQueue ( ) ;
38- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'anonymous' ) ;
37+ const send = createMockSend ( ) ;
38+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'anonymous' ) ;
3939 expect ( manager . level ) . toBe ( 'anonymous' ) ;
4040 } ) ;
4141
4242 it ( 'upgrades consent without modifying queue' , ( ) => {
4343 const queue = createMockQueue ( ) ;
44- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'none' ) ;
44+ const send = createMockSend ( ) ;
45+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'none' ) ;
4546
4647 manager . setLevel ( 'anonymous' ) ;
4748 expect ( manager . level ) . toBe ( 'anonymous' ) ;
@@ -51,7 +52,8 @@ describe('createConsentManager', () => {
5152
5253 it ( 'purges queue on downgrade to none' , ( ) => {
5354 const queue = createMockQueue ( ) ;
54- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'full' ) ;
55+ const send = createMockSend ( ) ;
56+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'full' ) ;
5557
5658 manager . setLevel ( 'none' ) ;
5759 expect ( manager . level ) . toBe ( 'none' ) ;
@@ -64,7 +66,8 @@ describe('createConsentManager', () => {
6466
6567 it ( 'strips userId on downgrade from full to anonymous' , ( ) => {
6668 const queue = createMockQueue ( ) ;
67- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'full' ) ;
69+ const send = createMockSend ( ) ;
70+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'full' ) ;
6871
6972 manager . setLevel ( 'anonymous' ) ;
7073 expect ( manager . level ) . toBe ( 'anonymous' ) ;
@@ -78,13 +81,14 @@ describe('createConsentManager', () => {
7881 expect ( result . anonymousId ) . toBe ( 'a-1' ) ;
7982 } ) ;
8083
81- it ( 'fires PUT to consent endpoint on level change' , ( ) => {
84+ it ( 'fires PUT to consent endpoint on level change via the injected send ' , ( ) => {
8285 const queue = createMockQueue ( ) ;
83- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'none' ) ;
86+ const send = createMockSend ( ) ;
87+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'none' ) ;
8488
8589 manager . setLevel ( 'anonymous' ) ;
8690
87- expect ( mockHttpSend ) . toHaveBeenCalledWith (
91+ expect ( send ) . toHaveBeenCalledWith (
8892 'https://api.dev.immutable.com/v1/audience/tracking-consent' ,
8993 'pk_test' ,
9094 { anonymousId : 'anon-1' , status : 'anonymous' , source : 'pixel' } ,
@@ -94,19 +98,21 @@ describe('createConsentManager', () => {
9498
9599 it ( 'does nothing when setting the same level' , ( ) => {
96100 const queue = createMockQueue ( ) ;
97- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'anonymous' ) ;
101+ const send = createMockSend ( ) ;
102+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' , 'anonymous' ) ;
98103
99104 manager . setLevel ( 'anonymous' ) ;
100105 expect ( queue . purge ) . not . toHaveBeenCalled ( ) ;
101106 expect ( queue . transform ) . not . toHaveBeenCalled ( ) ;
102- expect ( mockHttpSend ) . not . toHaveBeenCalled ( ) ;
107+ expect ( send ) . not . toHaveBeenCalled ( ) ;
103108 } ) ;
104109
105110 it ( 'respects DNT by defaulting to none' , ( ) => {
106111 Object . defineProperty ( navigator , 'doNotTrack' , { value : '1' , configurable : true } ) ;
107112
108113 const queue = createMockQueue ( ) ;
109- const manager = createConsentManager ( queue , 'pk_test' , 'anon-1' , 'dev' , 'pixel' ) ;
114+ const send = createMockSend ( ) ;
115+ const manager = createConsentManager ( queue , send , 'pk_test' , 'anon-1' , 'dev' , 'pixel' ) ;
110116 expect ( manager . level ) . toBe ( 'none' ) ;
111117
112118 Object . defineProperty ( navigator , 'doNotTrack' , { value : '0' , configurable : true } ) ;
0 commit comments