From 974a0511fa197075fc14e7eeab426d92a85ae46e Mon Sep 17 00:00:00 2001 From: Larry Person Date: Sun, 28 Apr 2024 18:06:07 -0400 Subject: [PATCH] All tests pass again. Maybe prevent test timeout. --- .../extensions/action-handlers/index.test.js | 9 ++++++--- .../updateServiceVendorConfig.test.js | 11 ++++++----- __test__/test_helpers.js | 18 ++++++++++-------- src/extensions/action-handlers/index.js | 6 +++--- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/__test__/extensions/action-handlers/index.test.js b/__test__/extensions/action-handlers/index.test.js index 3995abc85..b7cf9e1ab 100644 --- a/__test__/extensions/action-handlers/index.test.js +++ b/__test__/extensions/action-handlers/index.test.js @@ -3,13 +3,14 @@ import each from "jest-each"; import { setupTest, cleanupTest, - createStartedCampaign + createStartedCampaign, + flushRedis } from "../../test_helpers"; const ActionHandlers = require("../../../src/extensions/action-handlers"); const uuidv4 = require("uuid").v4; const TestAction = require("../../../src/extensions/action-handlers/test-action"); const ComplexTestAction = require("../../../src/extensions/action-handlers/complex-test-action"); -const log = require("../../../src/lib").log; +const { log } = require("../../../src/lib"); describe("action-handlers/index", () => { let organization; @@ -39,6 +40,8 @@ describe("action-handlers/index", () => { }); afterEach(async () => { + jest.restoreAllMocks(); + await flushRedis(); const toReset = ["CACHE_PREFIX", "ACTION_HANDLERS"]; toReset.forEach(thingToReset => { @@ -693,7 +696,7 @@ describe("action-handlers/index", () => { describe("when the items property is not an array", () => { beforeEach(async () => { - fakeAction.getClientChoiceData = () => ({ + fakeAction.getClientChoiceData = async () => ({ data: JSON.stringify({ items: {} }), expiresSeconds: 77 }); diff --git a/__test__/server/api/mutations/updateServiceVendorConfig.test.js b/__test__/server/api/mutations/updateServiceVendorConfig.test.js index 39aa20a8c..ba2c4aa9d 100644 --- a/__test__/server/api/mutations/updateServiceVendorConfig.test.js +++ b/__test__/server/api/mutations/updateServiceVendorConfig.test.js @@ -10,6 +10,7 @@ import { createOrganization, createUser, ensureOrganizationTwilioWithMessagingService, + flushRedis, runGql, setupTest } from "../../../test_helpers"; @@ -84,7 +85,7 @@ describe("updateServiceVendorConfig", () => { const dbOrganization = await Organization.get(organization.id); dbOrganization.features = null; await dbOrganization.save(); - if (r.redis) r.redis.FLUSHDB(); + await flushRedis(); }); it("returns an error", async () => { const gqlResult = await runGql(updateServiceVendorConfigGql, vars, user); @@ -160,7 +161,7 @@ describe("updateServiceVendorConfig", () => { dbOrganization = await Organization.get(organization.id); dbOrganization.features = JSON.stringify({ service: "twilio" }); await dbOrganization.save(); - if (r.redis) r.redis.FLUSHDB(); + await flushRedis(); expectedFeatures = { service, @@ -218,7 +219,7 @@ describe("updateServiceVendorConfig", () => { [configKey]: "it doesn't matter" }); await dbOrganization.save(); - if (r.redis) r.redis.FLUSHDB(); + await flushRedis(); }); it("writes message service config in features.configKey", async () => { const gqlResult = await runGql( @@ -248,7 +249,7 @@ describe("updateServiceVendorConfig", () => { TWILIO_MESSAGE_SERVICE_SID: "the_former_fake_message_service_sid" }); await dbOrganization.save(); - if (r.redis) r.redis.FLUSHDB(); + await flushRedis(); }); it("writes individual config components to the top level of features", async () => { const gqlResult = await runGql( @@ -280,7 +281,7 @@ describe("updateServiceVendorConfig", () => { TWILIO_MESSAGE_SERVICE_SID: "the_former_fake_message_service_sid" }); await dbOrganization.save(); - if (r.redis) r.redis.FLUSHDB(); + await flushRedis(); extremelyFakeService = { updateConfig: jest.fn().mockImplementation(() => { diff --git a/__test__/test_helpers.js b/__test__/test_helpers.js index 88d9a6af7..f111833ec 100644 --- a/__test__/test_helpers.js +++ b/__test__/test_helpers.js @@ -24,19 +24,21 @@ export async function setupTest() { await createTables(); } -export async function cleanupTest() { - await dropTables(); +export async function flushRedis() { if (r.redis) { - let needFlush = false; - for await (const key of r.redis.scanIterator({ COUNT: 1 })) { - needFlush = true; - } - if (needFlush) { - await r.redis.FLUSHDB(); + for await (const key of r.redis.scanIterator({ MATCH: "*", COUNT: 1 })) { + r.redis.FLUSHDB(); } } } +export async function cleanupTest(doFlushRedis = true) { + await dropTables(); + if (doFlushRedis) { + await flushRedis(); + } +} + export function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } diff --git a/src/extensions/action-handlers/index.js b/src/extensions/action-handlers/index.js index a67a41baa..9079d8748 100644 --- a/src/extensions/action-handlers/index.js +++ b/src/extensions/action-handlers/index.js @@ -197,10 +197,10 @@ export async function getActionChoiceData(actionHandler, organization, user) { parsedData = {}; } - let items = parsedData.items; - if (items && !(items instanceof Array)) { + let { items } = parsedData; + if (items && !Array.isArray(items)) { log.error( - `Data received from ${actionHandler.name}.getClientChoiceData is not an array` + s`Data received from ${actionHandler.name}.getClientChoiceData is not an array` ); items = undefined; }