@@ -1116,6 +1116,130 @@ describe("LoopsClient", () => {
11161116 } ) ;
11171117 } ) ;
11181118
1119+ describe ( "createAudienceSegment" , ( ) => {
1120+ it ( "should create an audience segment" , async ( ) => {
1121+ const filter = {
1122+ match : "all" as const ,
1123+ conditions : [
1124+ {
1125+ type : "property" as const ,
1126+ key : "planName" ,
1127+ operator : "equals" as const ,
1128+ value : "pro" ,
1129+ } ,
1130+ ] ,
1131+ } ;
1132+ const mockResponse = {
1133+ id : "cls6e8g0i2k4m6o8q0s2u4w6" ,
1134+ name : "Active users" ,
1135+ description : "Contacts on the pro plan" ,
1136+ createdAt : "2025-06-29T07:47:39.370Z" ,
1137+ updatedAt : "2025-06-29T07:47:39.370Z" ,
1138+ filter,
1139+ } ;
1140+
1141+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
1142+ ok : true ,
1143+ text : ( ) => Promise . resolve ( JSON . stringify ( mockResponse ) ) ,
1144+ } ) ;
1145+
1146+ const result = await client . createAudienceSegment ( {
1147+ name : "Active users" ,
1148+ description : "Contacts on the pro plan" ,
1149+ filter,
1150+ } ) ;
1151+
1152+ expect ( result ) . toEqual ( mockResponse ) ;
1153+ expect ( fetch ) . toHaveBeenCalledWith (
1154+ expect . stringContaining ( "v1/audience-segments" ) ,
1155+ expect . objectContaining ( {
1156+ method : "POST" ,
1157+ body : JSON . stringify ( {
1158+ name : "Active users" ,
1159+ filter,
1160+ description : "Contacts on the pro plan" ,
1161+ } ) ,
1162+ } )
1163+ ) ;
1164+ } ) ;
1165+
1166+ it ( "should handle error when segment name already exists" , async ( ) => {
1167+ const filter = {
1168+ match : "all" as const ,
1169+ conditions : [
1170+ {
1171+ type : "property" as const ,
1172+ key : "planName" ,
1173+ operator : "equals" as const ,
1174+ value : "pro" ,
1175+ } ,
1176+ ] ,
1177+ } ;
1178+ const mockResponse = {
1179+ message : "An audience segment with this name already exists" ,
1180+ } ;
1181+
1182+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
1183+ ok : false ,
1184+ status : 400 ,
1185+ text : ( ) => Promise . resolve ( JSON . stringify ( mockResponse ) ) ,
1186+ } ) ;
1187+
1188+ await expect (
1189+ client . createAudienceSegment ( {
1190+ name : "Active users" ,
1191+ filter,
1192+ } )
1193+ ) . rejects . toThrow ( APIError ) ;
1194+
1195+ expect ( fetch ) . toHaveBeenCalledWith (
1196+ expect . stringContaining ( "v1/audience-segments" ) ,
1197+ expect . objectContaining ( {
1198+ method : "POST" ,
1199+ body : JSON . stringify ( {
1200+ name : "Active users" ,
1201+ filter,
1202+ } ) ,
1203+ } )
1204+ ) ;
1205+ } ) ;
1206+
1207+ it ( "should handle error for an invalid filter" , async ( ) => {
1208+ const filter = {
1209+ match : "all" as const ,
1210+ conditions : [
1211+ {
1212+ type : "property" as const ,
1213+ key : "planName" ,
1214+ operator : "equals" as const ,
1215+ value : "pro" ,
1216+ } ,
1217+ ] ,
1218+ } ;
1219+ const mockResponse = {
1220+ message : "Invalid filter" ,
1221+ } ;
1222+
1223+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
1224+ ok : false ,
1225+ status : 400 ,
1226+ text : ( ) => Promise . resolve ( JSON . stringify ( mockResponse ) ) ,
1227+ } ) ;
1228+
1229+ try {
1230+ await client . createAudienceSegment ( {
1231+ name : "Active users" ,
1232+ filter,
1233+ } ) ;
1234+ fail ( "Should have thrown" ) ;
1235+ } catch ( error ) {
1236+ expect ( error ) . toBeInstanceOf ( APIError ) ;
1237+ expect ( ( error as APIError ) . statusCode ) . toBe ( 400 ) ;
1238+ expect ( ( error as APIError ) . json ) . toEqual ( mockResponse ) ;
1239+ }
1240+ } ) ;
1241+ } ) ;
1242+
11191243 describe ( "listThemes" , ( ) => {
11201244 it ( "should list themes with pagination" , async ( ) => {
11211245 const mockResponse = {
0 commit comments