From 4f87db446cf91beab2746930711ff82d36955ea8 Mon Sep 17 00:00:00 2001 From: Ashley Huynh Date: Tue, 29 Nov 2022 15:15:00 -0500 Subject: [PATCH] Add in more obvious aliasing and update tests --- src/SlackFunction.spec.ts | 114 +++++++++++++++++++------------------- src/SlackFunction.ts | 24 +------- 2 files changed, 58 insertions(+), 80 deletions(-) diff --git a/src/SlackFunction.spec.ts b/src/SlackFunction.spec.ts index 35adfb16a..17f31c85d 100644 --- a/src/SlackFunction.spec.ts +++ b/src/SlackFunction.spec.ts @@ -369,71 +369,71 @@ describe('SlackFunction module', () => { const shouldReject = async () => testFunc.runInteractivityHandlers(fakeArgs); assertNode.rejects(shouldReject); }); - }); - it('app.function.blockAction() should execute all provided callbacks', async () => { - const mockFunctionCallbackId = 'reverse_approval'; - const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId)); - const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {}); - const goodConstraints: ActionConstraints = { - action_id: 'my-action', - }; - const mockHandler = async () => Promise.resolve(); - const spy = sinon.spy(mockHandler); - const spy2 = sinon.spy(mockHandler); - // add blockAction handlers - testFunc.blockAction(goodConstraints, spy).blockAction(goodConstraints, spy2); - - // set up event args - const fakeArgs = { - next: () => {}, - payload: { + it('app.function.blockAction() should execute all provided callbacks', async () => { + const mockFunctionCallbackId = 'reverse_approval'; + const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId)); + const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {}); + const goodConstraints: ActionConstraints = { action_id: 'my-action', - }, - body: { - function_data: { - execution_id: 'asdasdas', + }; + const mockHandler = async () => Promise.resolve(); + const spy = sinon.spy(mockHandler); + const spy2 = sinon.spy(mockHandler); + // add blockAction handlers + testFunc.blockAction(goodConstraints, spy).blockAction(goodConstraints, spy2); + + // set up event args + const fakeArgs = { + next: () => {}, + payload: { + action_id: 'my-action', }, - }, - client: {} as WebClient, - } as unknown as AnyMiddlewareArgs & AllMiddlewareArgs; + body: { + function_data: { + execution_id: 'asdasdas', + }, + }, + client: {} as WebClient, + } as unknown as AnyMiddlewareArgs & AllMiddlewareArgs; - // ensure handlers are both called + // ensure handlers are both called - await testFunc.runInteractivityHandlers(fakeArgs); - assert(spy.calledOnce); - assert(spy2.calledOnce); - }); - it('app.function.blockAction() should error if a promise rejects', async () => { - const mockFunctionCallbackId = 'reverse_approval'; - const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId)); - const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {}); - const action_id = 'my-action'; - const goodConstraints: ActionConstraints = { - action_id, - }; - const mockHandler = async () => Promise.reject(); - const spy = sinon.spy(mockHandler); - // add a blockAction handlers - testFunc.blockAction(goodConstraints, spy); - - // set up event args - const fakeArgs = { - next: () => {}, - payload: { + await testFunc.runInteractivityHandlers(fakeArgs); + assert(spy.calledOnce); + assert(spy2.calledOnce); + }); + it('app.function.blockAction() should error if a promise rejects', async () => { + const mockFunctionCallbackId = 'reverse_approval'; + const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId)); + const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {}); + const action_id = 'my-action'; + const goodConstraints: ActionConstraints = { action_id, - }, - body: { - function_data: { - execution_id: 'asdasdas', + }; + const mockHandler = async () => Promise.reject(); + const spy = sinon.spy(mockHandler); + // add a blockAction handlers + testFunc.blockAction(goodConstraints, spy); + + // set up event args + const fakeArgs = { + next: () => {}, + payload: { + action_id, }, - }, - client: {} as WebClient, - } as unknown as AnyMiddlewareArgs & AllMiddlewareArgs; + body: { + function_data: { + execution_id: 'asdasdas', + }, + }, + client: {} as WebClient, + } as unknown as AnyMiddlewareArgs & AllMiddlewareArgs; - // ensure handler call rejects + // ensure handler call rejects - const shouldReject = async () => testFunc.runInteractivityHandlers(fakeArgs); - assertNode.rejects(shouldReject); + const shouldReject = async () => testFunc.runInteractivityHandlers(fakeArgs); + assertNode.rejects(shouldReject); + }); }); }); describe('isFunctionExecutedEvent()', () => { diff --git a/src/SlackFunction.ts b/src/SlackFunction.ts index d023f0e0b..50d13b211 100644 --- a/src/SlackFunction.ts +++ b/src/SlackFunction.ts @@ -156,9 +156,6 @@ export class SlackFunction { /** * Attach a block_actions interactivity handler to your SlackFunction * - * NOTE: blockActions() in this file is a direct copy of this function - * and any changes made here should also be made to that function. - * * ``` * Example: * const actionHandler = async () => {}; @@ -209,9 +206,6 @@ export class SlackFunction { * This function is added as an alias for the action() function to create * consistency with having a blockSuggestion() function. * - * NOTE: This function is a direct copy of the action() function and - * any changes made there should also be made to this function. - * * ``` * Example: * const actionHandler = async () => {}; @@ -238,23 +232,7 @@ export class SlackFunction { actionIdOrConstraints: string | RegExp | Constraints, handler: Middleware, ): this { - // normalize constraints - const constraints: ActionConstraints = ( - typeof actionIdOrConstraints === 'string' || - util.types.isRegExp(actionIdOrConstraints) - ) ? - { action_id: actionIdOrConstraints } : - actionIdOrConstraints; - - // declare our valid constraints keys - const validConstraintsKeys: ActionConstraintsKeys = ['action_id', 'block_id', 'callback_id', 'type']; - // cast to string array for convenience - const validConstraintsKeysAsStrings = validConstraintsKeys as string[]; - - errorIfInvalidConstraintKeys(constraints, validConstraintsKeysAsStrings, handler); - - this.interactivityHandlers.push({ constraints, handler }); - return this; + return this.action.apply(this, [actionIdOrConstraints, handler]); } /**