-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend app.function to register action + view handlers
- Loading branch information
Showing
16 changed files
with
1,077 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'mocha'; | ||
import sinon from 'sinon'; | ||
import { assert } from 'chai'; | ||
import rewiremock from 'rewiremock'; | ||
import { Override, mergeOverrides } from './test-helpers'; | ||
import { | ||
Receiver, | ||
ReceiverEvent, | ||
} from './types'; | ||
import App from './App'; | ||
import importSlackFunctionModule from './SlackFunction.spec'; | ||
|
||
// Fakes | ||
class FakeReceiver implements Receiver { | ||
private bolt: App | undefined; | ||
|
||
public init = (bolt: App) => { | ||
this.bolt = bolt; | ||
}; | ||
|
||
public start = sinon.fake((...params: any[]): Promise<unknown> => Promise.resolve([...params])); | ||
|
||
public stop = sinon.fake((...params: any[]): Promise<unknown> => Promise.resolve([...params])); | ||
|
||
public async sendEvent(event: ReceiverEvent): Promise<void> { | ||
return this.bolt?.processEvent(event); | ||
} | ||
} | ||
|
||
describe('App SlackFunction middleware', () => { | ||
let fakeReceiver: FakeReceiver; | ||
let dummyAuthorizationResult: { botToken: string; botId: string }; | ||
|
||
beforeEach(() => { | ||
fakeReceiver = new FakeReceiver(); | ||
dummyAuthorizationResult = { botToken: '', botId: '' }; | ||
}); | ||
|
||
let app: App; | ||
|
||
beforeEach(async () => { | ||
const MockAppNoOverrides = await importApp(); | ||
app = new MockAppNoOverrides({ | ||
receiver: fakeReceiver, | ||
authorize: sinon.fake.resolves(dummyAuthorizationResult), | ||
}); | ||
}); | ||
|
||
it('should add a middleware for each SlackFunction passed to app.function', async () => { | ||
const mockFunctionCallbackId = 'reverse_approval'; | ||
const { SlackFunction } = await importSlackFunctionModule(withMockValidManifestUtil(mockFunctionCallbackId)); | ||
const slackFn = new SlackFunction(mockFunctionCallbackId, () => new Promise((resolve) => resolve())); | ||
|
||
/* middleware is a private property on App. Since app.function relies on app.use, | ||
and app.use is fully tested above, we're opting just to ensure that the step listener | ||
is added to the global middleware array, rather than repeating the same tests. */ | ||
const { middleware } = (app as any); | ||
|
||
assert.equal(middleware.length, 2); | ||
|
||
app.function(slackFn); | ||
|
||
assert.equal(middleware.length, 3); | ||
}); | ||
}); | ||
|
||
/* Testing Harness */ | ||
|
||
// Loading the system under test using overrides | ||
async function importApp( | ||
overrides: Override = mergeOverrides( | ||
withNoopAppMetadata(), | ||
withNoopWebClient(), | ||
), | ||
): Promise<typeof import('./App').default> { | ||
return (await rewiremock.module(() => import('./App'), overrides)).default; | ||
} | ||
|
||
// Composable overrides | ||
function withNoopWebClient(): Override { | ||
return { | ||
'@slack/web-api': { | ||
WebClient: class {}, | ||
}, | ||
}; | ||
} | ||
|
||
function withNoopAppMetadata(): Override { | ||
return { | ||
'@slack/web-api': { | ||
addAppMetadata: sinon.fake(), | ||
}, | ||
}; | ||
} | ||
|
||
export default function withMockValidManifestUtil(functionCallbackId: string): Override { | ||
const mockManifestOutput = JSON.parse(`{"functions": {"${functionCallbackId}": {}}}`); | ||
return { | ||
'./cli/hook-utils/manifest': { | ||
getManifestData: () => mockManifestOutput, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.