Skip to content

Commit

Permalink
Add in more obvious aliasing and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hello-ashleyintech committed Nov 29, 2022
1 parent a339a03 commit 4f87db4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 80 deletions.
114 changes: 57 additions & 57 deletions src/SlackFunction.spec.ts
Expand Up @@ -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()', () => {
Expand Down
24 changes: 1 addition & 23 deletions src/SlackFunction.ts
Expand Up @@ -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 () => {};
Expand Down Expand Up @@ -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 () => {};
Expand All @@ -238,23 +232,7 @@ export class SlackFunction {
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;
return this.action.apply(this, [actionIdOrConstraints, handler]);
}

/**
Expand Down

0 comments on commit 4f87db4

Please sign in to comment.