|
| 1 | +import { afterEach, describe, expect, it, jest } from '@jest/globals'; |
| 2 | + |
| 3 | +import LinkedApi, { Operation, TOperationName } from '../../index'; |
| 4 | + |
| 5 | +const LIST_ID = '7420000000000000001' as const; |
| 6 | +const PERSON_URL = 'https://www.linkedin.com/in/example-person' as const; |
| 7 | +const COMPANY_URL = 'https://www.linkedin.com/company/example-company' as const; |
| 8 | +const SEARCH_URL = 'https://www.linkedin.com/sales/search/people?query=example' as const; |
| 9 | +const LEAD = { |
| 10 | + name: 'Example Person', |
| 11 | + urn: 'urn:li:member:123', |
| 12 | + hashedUrl: PERSON_URL, |
| 13 | + position: null, |
| 14 | + companyName: null, |
| 15 | + location: null, |
| 16 | + addedToListAt: null, |
| 17 | +}; |
| 18 | +const ACCOUNT = { |
| 19 | + name: 'Example Company', |
| 20 | + urn: 'urn:li:organization:123', |
| 21 | + hashedUrl: COMPANY_URL, |
| 22 | + industry: null, |
| 23 | + employeesCount: null, |
| 24 | +}; |
| 25 | +const CREATED = { id: '7420000000000000002', |
| 26 | +name: 'Example', |
| 27 | +type: 'lead', |
| 28 | +url: SEARCH_URL }; |
| 29 | +const SAVED_SEARCH = { |
| 30 | + ...CREATED, |
| 31 | + keywords: null, |
| 32 | + createdAt: null, |
| 33 | + lastViewedAt: null, |
| 34 | + newResultsCount: null, |
| 35 | +}; |
| 36 | +const CASES: ReadonlyArray<{ |
| 37 | + operationName: TOperationName; |
| 38 | + actionType: string; |
| 39 | + params: Record<string, unknown>; |
| 40 | + data?: unknown; |
| 41 | +}> = [ |
| 42 | + { |
| 43 | + operationName: 'nvRetrieveListLeads', |
| 44 | + actionType: 'nv.retrieveListLeads', |
| 45 | + params: { listId: LIST_ID, |
| 46 | +limit: 26 }, |
| 47 | + data: [LEAD], |
| 48 | + }, |
| 49 | + { |
| 50 | + operationName: 'nvRetrieveListAccounts', |
| 51 | + actionType: 'nv.retrieveListAccounts', |
| 52 | + params: { listId: LIST_ID }, |
| 53 | + data: [ACCOUNT], |
| 54 | + }, |
| 55 | + { |
| 56 | + operationName: 'nvRemoveLeadFromList', |
| 57 | + actionType: 'nv.removeLeadFromList', |
| 58 | + params: { personHashedUrl: PERSON_URL, |
| 59 | +listId: LIST_ID }, |
| 60 | + }, |
| 61 | + { |
| 62 | + operationName: 'nvRemoveAccountFromList', |
| 63 | + actionType: 'nv.removeAccountFromList', |
| 64 | + params: { companyHashedUrl: COMPANY_URL, |
| 65 | +listId: LIST_ID }, |
| 66 | + }, |
| 67 | + { |
| 68 | + operationName: 'nvRenameList', |
| 69 | + actionType: 'nv.renameList', |
| 70 | + params: { listId: LIST_ID, |
| 71 | +name: 'Prospects' }, |
| 72 | + data: { |
| 73 | + id: LIST_ID, |
| 74 | + name: 'Prospects', |
| 75 | + type: 'lead', |
| 76 | + url: `https://www.linkedin.com/sales/lists/people/${LIST_ID}`, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + operationName: 'nvRetrieveSavedSearches', |
| 81 | + actionType: 'nv.retrieveSavedSearches', |
| 82 | + params: { type: 'lead' }, |
| 83 | + data: [SAVED_SEARCH], |
| 84 | + }, |
| 85 | + { |
| 86 | + operationName: 'nvCreateSavedSearch', |
| 87 | + actionType: 'nv.createSavedSearch', |
| 88 | + params: { type: 'lead', |
| 89 | +customSearchUrl: SEARCH_URL }, |
| 90 | + data: CREATED, |
| 91 | + }, |
| 92 | + { |
| 93 | + operationName: 'nvDeleteSavedSearch', |
| 94 | + actionType: 'nv.deleteSavedSearch', |
| 95 | + params: { savedSearchId: CREATED.id }, |
| 96 | + }, |
| 97 | +]; |
| 98 | + |
| 99 | +afterEach((): void => { |
| 100 | + jest.restoreAllMocks(); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('Sales Navigator backlog contract', (): void => { |
| 104 | + it.each(CASES)( |
| 105 | + 'maps $operationName root requests and results', |
| 106 | + async ({ operationName, actionType, params, data }): Promise<void> => { |
| 107 | + const fetchMock = jest.spyOn(globalThis, 'fetch'); |
| 108 | + fetchMock.mockResolvedValueOnce( |
| 109 | + new Response( |
| 110 | + JSON.stringify({ |
| 111 | + success: true, |
| 112 | + result: { workflowId: 'nv', |
| 113 | +workflowStatus: 'pending' }, |
| 114 | + }), |
| 115 | + ), |
| 116 | + ); |
| 117 | + fetchMock.mockResolvedValueOnce( |
| 118 | + new Response( |
| 119 | + JSON.stringify({ |
| 120 | + success: true, |
| 121 | + result: { |
| 122 | + workflowStatus: 'completed', |
| 123 | + completion: { actionType, |
| 124 | +success: true, |
| 125 | +...(data === undefined ? {} : { data }) }, |
| 126 | + }, |
| 127 | + }), |
| 128 | + ), |
| 129 | + ); |
| 130 | + const sdk = new LinkedApi({ |
| 131 | + linkedApiToken: 'test', |
| 132 | + identificationToken: 'test', |
| 133 | + baseUrl: 'https://api.test', |
| 134 | + }); |
| 135 | + const operation = sdk.operations.find( |
| 136 | + (entry): boolean => entry.operationName === operationName, |
| 137 | + ) as Operation<Record<string, unknown>, unknown>; |
| 138 | + const started = await operation.execute(params); |
| 139 | + expect(JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body))).toEqual({ |
| 140 | + actionType, |
| 141 | + ...params, |
| 142 | + }); |
| 143 | + expect(await operation.result(started.workflowId)).toEqual({ data, |
| 144 | +errors: [] }); |
| 145 | + }, |
| 146 | + ); |
| 147 | + |
| 148 | + it.each([ |
| 149 | + { |
| 150 | + operationName: 'nvRenameList', |
| 151 | + actionType: 'nv.renameList', |
| 152 | + type: 'listNotRenamable', |
| 153 | + params: { listId: LIST_ID, |
| 154 | +name: 'Prospects' }, |
| 155 | + }, |
| 156 | + { |
| 157 | + operationName: 'nvDeleteSavedSearch', |
| 158 | + actionType: 'nv.deleteSavedSearch', |
| 159 | + type: 'savedSearchNotFound', |
| 160 | + params: { savedSearchId: CREATED.id }, |
| 161 | + }, |
| 162 | + ] as const)( |
| 163 | + 'preserves %s action errors', |
| 164 | + async ({ operationName, actionType, type, params }): Promise<void> => { |
| 165 | + const error = { type, |
| 166 | +message: 'Fixture error' }; |
| 167 | + const fetchMock = jest.spyOn(globalThis, 'fetch'); |
| 168 | + fetchMock.mockResolvedValueOnce( |
| 169 | + new Response( |
| 170 | + JSON.stringify({ |
| 171 | + success: true, |
| 172 | + result: { workflowId: 'nv', |
| 173 | +workflowStatus: 'pending' }, |
| 174 | + }), |
| 175 | + ), |
| 176 | + ); |
| 177 | + fetchMock.mockResolvedValueOnce( |
| 178 | + new Response( |
| 179 | + JSON.stringify({ |
| 180 | + success: true, |
| 181 | + result: { |
| 182 | + workflowStatus: 'completed', |
| 183 | + completion: { actionType, |
| 184 | +success: false, |
| 185 | +error }, |
| 186 | + }, |
| 187 | + }), |
| 188 | + ), |
| 189 | + ); |
| 190 | + const sdk = new LinkedApi({ linkedApiToken: 'test', |
| 191 | +identificationToken: 'test' }); |
| 192 | + const operation = sdk.operations.find( |
| 193 | + (entry): boolean => entry.operationName === operationName, |
| 194 | + ) as Operation<Record<string, unknown>, unknown>; |
| 195 | + const started = await operation.execute(params); |
| 196 | + expect(await operation.result(started.workflowId)).toEqual({ |
| 197 | + data: undefined, |
| 198 | + errors: [error], |
| 199 | + }); |
| 200 | + }, |
| 201 | + ); |
| 202 | + |
| 203 | + it('preserves comma-bearing alert identities and list scope', async (): Promise<void> => { |
| 204 | + const fetchMock = jest.spyOn(globalThis, 'fetch').mockResolvedValueOnce( |
| 205 | + new Response( |
| 206 | + JSON.stringify({ |
| 207 | + success: true, |
| 208 | + result: { workflowId: 'nv', |
| 209 | +workflowStatus: 'pending' }, |
| 210 | + }), |
| 211 | + ), |
| 212 | + ); |
| 213 | + const sdk = new LinkedApi({ linkedApiToken: 'test', |
| 214 | +identificationToken: 'test' }); |
| 215 | + const filter = { |
| 216 | + listId: LIST_ID, |
| 217 | + subjects: [ |
| 218 | + 'urn:li:fs_salesProfile:(ACwAAAExample,NAME_SEARCH,xyz)', |
| 219 | + 'urn:li:fs_salesCompany:123', |
| 220 | + ], |
| 221 | + }; |
| 222 | + await sdk.nvRetrieveAlerts.execute({ sort: 'latest', |
| 223 | +filter }); |
| 224 | + expect(JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body))).toEqual({ |
| 225 | + actionType: 'nv.retrieveAlerts', |
| 226 | + sort: 'latest', |
| 227 | + filter, |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments