diff --git a/src/actions/processor/index.js b/src/actions/processor/index.js index a258a84..49685c8 100644 --- a/src/actions/processor/index.js +++ b/src/actions/processor/index.js @@ -99,7 +99,7 @@ function appendToSheet(ctx, sheet, record, sheetName = 'private-data') { // if all properties of the first record are empty, it's a new sheet with no data // use the ordered record as the header row to avoid an empty row between the header and data // otherwise, append the ordered record to the data array - if (Object.values(dest.data[0]).every((value) => value === '')) { + if (Object.values(dest.data[0] ?? {}).every((value) => value === '')) { dest.data[0] = orderedRecord; } else { dest.data.push(orderedRecord); diff --git a/test/processor.test.js b/test/processor.test.js index cfe84f1..e96b39b 100644 --- a/test/processor.test.js +++ b/test/processor.test.js @@ -227,13 +227,24 @@ describe('processor action', () => { }); test('uses deadletter path when folder has no entries', async () => { - mockMakeContext.mockResolvedValue(makeCtx()); + const ctx = makeCtx(); + mockMakeContext.mockResolvedValue(ctx); mockListFolder.mockResolvedValue([]); const result = await main({}); - // empty entries → deadletter path; also means new sheet (INITIAL_SHEET) - // which has an empty data array, causing appendToSheet to error - expect(result.error.statusCode).toBe(500); + expect(result.statusCode).toBe(200); + expect(mockUpdateSheet).toHaveBeenCalledWith( + ctx, + `/incoming/deadletter/contact-us/${YEAR}.json`, + expect.objectContaining({ + ':private': expect.objectContaining({ + 'private-data': expect.objectContaining({ + total: 1, + data: [expect.objectContaining({ name: 'John' })], + }), + }), + }), + ); }); });