Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in blockAction handler to SlackFunction #1657

Merged
merged 4 commits into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/SlackFunction.spec.ts
Expand Up @@ -200,6 +200,44 @@ describe('SlackFunction module', () => {
assert.doesNotThrow(shouldNotThrow, SlackFunctionInitializationError);
});
});
describe('app.function.blockAction() adds a handler to interactivity handlers', () => {
it('should not error when valid handler constraints supplied', async () => {
const mockFunctionCallbackId = 'reverse_approval';
const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId));
const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {});
const goodConstraints: ActionConstraints = {
action_id: '',
};
const shouldNotThrow = () => testFunc.blockAction(goodConstraints, async () => {});
assert.doesNotThrow(shouldNotThrow, SlackFunctionInitializationError);
});
it('should error when invalid handler constraints supplied', async () => {
const mockFunctionCallbackId = 'reverse_approval';
const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId));
const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {});
const badConstraints = {
bad_id: '',
action_id: '',
} as ActionConstraints;
const shouldThrow = () => testFunc.blockAction(badConstraints, async () => {});
assert.throws(shouldThrow, SlackFunctionInitializationError);
});
it('should return the instance of slackfunction', async () => {
const mockFunctionCallbackId = 'reverse_approval';
const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId));
const testFunc = new SlackFunction(mockFunctionCallbackId, async () => {});
const goodConstraints: ActionConstraints = {
action_id: '',
};
const mockHandler = async () => {};
// expect that the return value of blockAction is a Slack function
assert.instanceOf(testFunc.blockAction(goodConstraints, mockHandler), SlackFunction);
// chained valid handlers should not error
const shouldNotThrow = () => testFunc.blockAction(goodConstraints, mockHandler)
.blockAction(goodConstraints, mockHandler);
assert.doesNotThrow(shouldNotThrow, SlackFunctionInitializationError);
});
});
describe('runInteractivityHandlers', () => {
it('app.function.action() should execute all provided callbacks', async () => {
const mockFunctionCallbackId = 'reverse_approval';
Expand Down Expand Up @@ -332,6 +370,71 @@ describe('SlackFunction module', () => {
assertNode.rejects(shouldReject);
});
});
it('app.function.blockAction() should execute all provided callbacks', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on locating this test and the one below it within `describe('runInteractivityHandlers', ....) on ln241?

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: {
action_id: 'my-action',
},
body: {
function_data: {
execution_id: 'asdasdas',
},
},
client: {} as WebClient,
} as unknown as AnyMiddlewareArgs & AllMiddlewareArgs;

// 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: {
action_id,
},
body: {
function_data: {
execution_id: 'asdasdas',
},
},
client: {} as WebClient,
} as unknown as AnyMiddlewareArgs & AllMiddlewareArgs;

// ensure handler call rejects

const shouldReject = async () => testFunc.runInteractivityHandlers(fakeArgs);
assertNode.rejects(shouldReject);
});
});
describe('isFunctionExecutedEvent()', () => {
it('returns true when args contain function_executed', () => {
Expand Down
57 changes: 56 additions & 1 deletion src/SlackFunction.ts
Expand Up @@ -156,6 +156,9 @@ 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 () => {};
Expand Down Expand Up @@ -202,9 +205,61 @@ export class SlackFunction {
}

/**
* Attach a block_suggestion interactivity handler to your SlackFunction
* Attach a block_actions interactivity handler to your 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to use the .action() function within blockAction instead of copying the code? If we can do this, it would probably reduce the likelihood of accidental drift between the two interfaces, since we expect them to be the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely! I'll try what you suggested above and have this updated by later today! 🙌

* any changes made there should also be made to this function.
*
* ```
* Example:
* const actionHandler = async () => {};
* const actionHandler1 = async () => {};
* myFunc.blockAction("id", actionHandler).blockAction("id1", actionHandler1);
* ```
*
* @param actionIdOrConstraints Provide an action_id string
* corresponding to the value supplied in your blocks or a
* constraint object of type ActionConstraints<SlackAction>
*
* ```
* Example:
* myFunc.blockAction({ type: "action_submission" });
* myFunc.blockAction({ action_id: "id" }, actionHandler);
* ```
* @param handler Provide a handler function
* @returns SlackFunction instance
*/
public blockAction<
Action extends SlackAction = SlackAction,
Constraints extends ActionConstraints<Action> = ActionConstraints<Action>,
>(
actionIdOrConstraints: string | RegExp | Constraints,
handler: Middleware<SlackActionMiddlewareArgs>,
): 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;
}

/**
* Attach a block_suggestion interactivity handler to your SlackFunction
*
* @param handler Provide a handler function
* @returns SlackFunction instance
*/
Expand Down