|
| 1 | +import { RequestClient } from '@segment/actions-core' |
| 2 | +import { Settings } from '../generated-types' |
| 3 | +import { getCategories } from '../sfmc-operations' |
| 4 | +import { xml2js } from 'xml-js' |
| 5 | + |
| 6 | +jest.mock('xml-js', () => ({ |
| 7 | + xml2js: jest.fn() |
| 8 | +})) |
| 9 | + |
| 10 | +const settings: Settings = { |
| 11 | + subdomain: 'test123', |
| 12 | + client_id: 'test123', |
| 13 | + client_secret: 'test123', |
| 14 | + account_id: 'test123' |
| 15 | +} |
| 16 | + |
| 17 | +describe('Salesforce Marketing Cloud Category List Operations', () => { |
| 18 | + describe('getCategoryList with ContentType filter', () => { |
| 19 | + it('should get categories filtered by ContentType=dataextension', async () => { |
| 20 | + ;(xml2js as jest.Mock).mockImplementation(() => { |
| 21 | + return { |
| 22 | + 'soap:Envelope': { |
| 23 | + 'soap:Body': { |
| 24 | + RetrieveResponseMsg: { |
| 25 | + Results: [ |
| 26 | + { |
| 27 | + ID: { |
| 28 | + _text: '123' |
| 29 | + }, |
| 30 | + Name: { |
| 31 | + _text: 'Test Category 1' |
| 32 | + }, |
| 33 | + ContentType: { |
| 34 | + _text: 'dataextension' |
| 35 | + } |
| 36 | + }, |
| 37 | + { |
| 38 | + ID: { |
| 39 | + _text: '456' |
| 40 | + }, |
| 41 | + Name: { |
| 42 | + _text: 'Test Category 2' |
| 43 | + }, |
| 44 | + ContentType: { |
| 45 | + _text: 'dataextension' |
| 46 | + } |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + let soapRequestBody = '' |
| 56 | + const requestSpy = jest.fn().mockImplementation((url, options) => { |
| 57 | + if (url.includes('auth.marketingcloudapis.com/v2/token')) { |
| 58 | + return Promise.resolve({ |
| 59 | + data: { |
| 60 | + access_token: 'mock-access-token', |
| 61 | + soap_instance_url: 'https://mock-instance.soap.marketingcloudapis.com' |
| 62 | + } |
| 63 | + }) |
| 64 | + } else if (url.includes('soap.marketingcloudapis.com/Service.asmx')) { |
| 65 | + soapRequestBody = options?.body || '' |
| 66 | + return Promise.resolve({ |
| 67 | + content: 'mock-xml-content' |
| 68 | + }) |
| 69 | + } |
| 70 | + return Promise.resolve({ data: {} }) |
| 71 | + }) as unknown as RequestClient |
| 72 | + |
| 73 | + const response = await getCategories(requestSpy, settings) |
| 74 | + |
| 75 | + expect(soapRequestBody).toBeDefined() |
| 76 | + expect(soapRequestBody).toContain('<Filter xsi:type="SimpleFilterPart">') |
| 77 | + expect(soapRequestBody).toContain('<Property>ContentType</Property>') |
| 78 | + expect(soapRequestBody).toContain('<SimpleOperator>equals</SimpleOperator>') |
| 79 | + expect(soapRequestBody).toContain('<Value>dataextension</Value>') |
| 80 | + |
| 81 | + expect(response).toBeDefined() |
| 82 | + expect(response.choices).toHaveLength(2) |
| 83 | + expect(response.choices?.[0].value).toBe('123') |
| 84 | + expect(response.choices?.[0].label).toBe('Test Category 1') |
| 85 | + expect(response.choices?.[0].description).toBe('ContentType: dataextension') |
| 86 | + expect(response.choices?.[1].value).toBe('456') |
| 87 | + expect(response.choices?.[1].label).toBe('Test Category 2') |
| 88 | + expect(response.choices?.[1].description).toBe('ContentType: dataextension') |
| 89 | + }) |
| 90 | + |
| 91 | + it('should handle error when getting categories', async () => { |
| 92 | + ;(xml2js as jest.Mock).mockClear() |
| 93 | + |
| 94 | + let soapRequestBody = '' |
| 95 | + const requestSpy = jest.fn().mockImplementation((url, options) => { |
| 96 | + if (url.includes('auth.marketingcloudapis.com/v2/token')) { |
| 97 | + return Promise.resolve({ |
| 98 | + data: { |
| 99 | + access_token: 'mock-access-token', |
| 100 | + soap_instance_url: 'https://mock-instance.soap.marketingcloudapis.com' |
| 101 | + } |
| 102 | + }) |
| 103 | + } else if (url.includes('soap.marketingcloudapis.com/Service.asmx')) { |
| 104 | + soapRequestBody = options?.body || '' |
| 105 | + throw { |
| 106 | + response: { |
| 107 | + data: { |
| 108 | + message: 'Error retrieving categories' |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return Promise.resolve({ data: {} }) |
| 114 | + }) as unknown as RequestClient |
| 115 | + |
| 116 | + const response = await getCategories(requestSpy, settings) |
| 117 | + |
| 118 | + expect(soapRequestBody).toBeDefined() |
| 119 | + expect(soapRequestBody).toContain('<Filter xsi:type="SimpleFilterPart">') |
| 120 | + expect(soapRequestBody).toContain('<Property>ContentType</Property>') |
| 121 | + expect(soapRequestBody).toContain('<SimpleOperator>equals</SimpleOperator>') |
| 122 | + expect(soapRequestBody).toContain('<Value>dataextension</Value>') |
| 123 | + |
| 124 | + expect(response).toBeDefined() |
| 125 | + expect(response.choices).toHaveLength(0) |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments