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 all 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 @@ -328,6 +366,71 @@ describe('SlackFunction module', () => {

// ensure handler call rejects

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: {
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);
});
Expand Down
35 changes: 34 additions & 1 deletion src/SlackFunction.ts
Expand Up @@ -202,9 +202,42 @@ 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.
*
* ```
* 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 {
return this.action.apply(this, [actionIdOrConstraints, handler]);
}

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