Skip to content

Commit

Permalink
"DRY" up the arrange parts in App.spec.js (#198)
Browse files Browse the repository at this point in the history
"DRY" up the arrange parts in App.spec.js
  • Loading branch information
aoberoi authored May 24, 2019
2 parents 77431a7 + 1332aec commit 9ac97de
Showing 1 changed file with 35 additions and 45 deletions.
80 changes: 35 additions & 45 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,28 @@ describe('App', () => {
assert(fakeSecondMiddleware.calledOnce);
});
});

describe('middleware and listener arguments', () => {

let fakeReceiver: FakeReceiver;
let fakeErrorHandler: SinonSpy;
let dummyAuthorizationResult: { [key: string]: any };
const dummyChannelId = 'CHANNEL_ID';
let overrides: Override;

function buildOverrides(secondOverride: Override) {
fakeReceiver = createFakeReceiver();
fakeErrorHandler = sinon.fake();
dummyAuthorizationResult = { botToken: '', botId: '' };
overrides = mergeOverrides(
withNoopAppMetadata(),
secondOverride,
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware))
);
return overrides;
}

describe('say()', () => {

function createChannelContextualReceiverEvents(channelId: string): ReceiverEvent[] {
Expand Down Expand Up @@ -364,20 +385,12 @@ describe('App', () => {

it('should send a simple message to a channel where the incoming event originates', async () => {
// Arrange
const fakeReceiver = createFakeReceiver();
const fakePostMessage = sinon.fake.resolves({});
const fakeErrorHandler = sinon.fake();
const dummyAuthorizationResult = { botToken: '', botId: '' };
const overrides = buildOverrides(withPostMessage(fakePostMessage));
const App = await importApp(overrides); // tslint:disable-line:variable-name

const dummyMessage = 'test';
const dummyChannelId = 'CHANNEL_ID';
const dummyReceiverEvents = createChannelContextualReceiverEvents(dummyChannelId);
const overrides = mergeOverrides(
withNoopAppMetadata(),
withPostMessage(fakePostMessage),
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware)),
);
const App = await importApp(overrides); // tslint:disable-line:variable-name

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
Expand All @@ -401,23 +414,14 @@ describe('App', () => {
assert(fakeErrorHandler.notCalled);
});

// TODO: DRY up this case with the case above
it('should send a complex message to a channel where the incoming event originates', async () => {
// Arrange
const fakeReceiver = createFakeReceiver();
const fakePostMessage = sinon.fake.resolves({});
const fakeErrorHandler = sinon.fake();
const dummyAuthorizationResult = { botToken: '', botId: '' };
const overrides = buildOverrides(withPostMessage(fakePostMessage));
const App = await importApp(overrides); // tslint:disable-line:variable-name

const dummyMessage = { text: 'test' };
const dummyChannelId = 'CHANNEL_ID';
const dummyReceiverEvents = createChannelContextualReceiverEvents(dummyChannelId);
const overrides = mergeOverrides(
withNoopAppMetadata(),
withPostMessage(fakePostMessage),
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware)),
);
const App = await importApp(overrides); // tslint:disable-line:variable-name

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
Expand Down Expand Up @@ -494,18 +498,12 @@ describe('App', () => {

it('should not exist in the arguments on incoming events that don\'t support say', async () => {
// Arrange
const fakeReceiver = createFakeReceiver();
const assertionAggregator = sinon.fake();
const dummyAuthorizationResult = { botToken: '', botId: '' };
const dummyReceiverEvents = createReceiverEventsWithoutSay('CHANNEL_ID');
const overrides = mergeOverrides(
withNoopAppMetadata(),
withNoopWebClient(),
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware)),
);
const overrides = buildOverrides(withNoopWebClient());
const App = await importApp(overrides); // tslint:disable-line:variable-name

const assertionAggregator = sinon.fake();
const dummyReceiverEvents = createReceiverEventsWithoutSay(dummyChannelId);

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
app.use((args) => {
Expand All @@ -523,20 +521,12 @@ describe('App', () => {

it('should handle failures through the App\'s global error handler', async () => {
// Arrange
const fakeReceiver = createFakeReceiver();
const fakePostMessage = sinon.fake.rejects(new Error('fake error'));
const fakeErrorHandler = sinon.fake();
const dummyAuthorizationResult = { botToken: '', botId: '' };
const overrides = buildOverrides(withPostMessage(fakePostMessage));
const App = await importApp(overrides); // tslint:disable-line:variable-name

const dummyMessage = { text: 'test' };
const dummyChannelId = 'CHANNEL_ID';
const dummyReceiverEvents = createChannelContextualReceiverEvents(dummyChannelId);
const overrides = mergeOverrides(
withNoopAppMetadata(),
withPostMessage(fakePostMessage),
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware)),
);
const App = await importApp(overrides); // tslint:disable-line:variable-name

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
Expand Down Expand Up @@ -570,7 +560,7 @@ async function importApp(
function withNoopWebClient(): Override {
return {
'@slack/web-api': {
WebClient: class {},
WebClient: class { },
},
};
}
Expand Down

0 comments on commit 9ac97de

Please sign in to comment.