Skip to content

Commit 98c4ad2

Browse files
committed
Rename mdn strategies
1 parent cd6873c commit 98c4ad2

File tree

7 files changed

+45
-243
lines changed

7 files changed

+45
-243
lines changed

src/commands/mdn/api.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { updatedQueryBuilder } from './api';
2+
3+
import { searchResponse } from './__fixtures__/responses';
4+
import useData from '../../utils/useData';
5+
import { getChosenResult } from '../../utils/discordTools';
6+
7+
describe('updatedMDNQuery', () => {
8+
const mockUseData: jest.MockedFunction<typeof useData> = jest.fn();
9+
const mockChoose: jest.MockedFunction<typeof getChosenResult> = jest.fn();
10+
11+
const editMsg = {
12+
edit: jest.fn(),
13+
};
14+
const sendMock = jest.fn();
15+
const replyMock = jest.fn();
16+
const msg: any = {
17+
channel: { send: sendMock },
18+
reply: replyMock,
19+
};
20+
21+
test('should work', async () => {
22+
mockUseData.mockResolvedValueOnce({
23+
error: false,
24+
text: null,
25+
json: searchResponse,
26+
});
27+
28+
mockChoose.mockResolvedValueOnce({
29+
title: 'DOM (Document Object Model)',
30+
slug: 'Glossary/DOM',
31+
locale: 'en-US',
32+
excerpt:
33+
'The DOM (Document Object Model) is an API that represents and interacts with any HTML or XML document. The DOM is a document model loaded in the browser and representing the document as a node tree, where each node represents part of the document (e.g. an element, text string, or comment).',
34+
});
35+
36+
sendMock.mockResolvedValue(editMsg);
37+
const handler = updatedQueryBuilder(mockUseData, mockChoose);
38+
39+
await handler(msg, 'Search Term');
40+
expect(msg.channel.send.mock.calls).toMatchSnapshot();
41+
expect(editMsg.edit.mock.calls).toMatchSnapshot();
42+
});
43+
});
File renamed without changes.

src/commands/mdn/index.test.ts renamed to src/commands/mdn/dom.test.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import * as errors from '../../utils/errors';
44
import { getSearchUrl } from '../../utils/urlTools';
55
import useData from '../../utils/useData';
66

7-
import { queryBuilder, updatedQueryBuilder } from '.';
7+
import { queryBuilder } from './dom';
88
import { getChosenResult } from '../../utils/discordTools';
9-
import { searchResponse } from './__fixtures__/responses';
109

1110
jest.mock('dom-parser');
1211
jest.mock('../../utils/urlTools');
@@ -118,28 +117,3 @@ describe('handleMDNQuery', () => {
118117
expect(sentMessage).toMatchSnapshot();
119118
});
120119
});
121-
122-
describe('updatedMDNQuery', () => {
123-
const sendMock = jest.fn();
124-
const replyMock = jest.fn();
125-
const msg: any = {
126-
channel: { send: sendMock },
127-
reply: replyMock,
128-
};
129-
130-
test('should work', async () => {
131-
mockGetSearchUrl.mockReturnValue('http://example.com');
132-
mockUseData.mockResolvedValueOnce({
133-
error: false,
134-
text: null,
135-
json: searchResponse,
136-
});
137-
138-
const handler = updatedQueryBuilder(
139-
mockGetSearchUrl,
140-
mockUseData,
141-
mockChoose
142-
);
143-
await handler(msg, 'Search Term');
144-
});
145-
});

src/commands/mdn/index.ts renamed to src/commands/mdn/dom.ts

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -107,65 +107,6 @@ const extractMetadataFromResult = (result: ResultMeta) => {
107107
};
108108
};
109109

110-
export const updatedQueryBuilder = (
111-
searchUrl: typeof getSearchUrl = getSearchUrl,
112-
fetch: typeof useData = useData,
113-
waitForChosenResult: typeof getChosenResult = getChosenResult
114-
) => async (msg: Message, searchTerm: string) => {
115-
const url = searchUrl(provider, searchTerm);
116-
const { error, json } = await fetch<SearchResponse>(url, 'json');
117-
if (!error) {
118-
return msg.reply(errors.invalidResponse);
119-
}
120-
121-
if (json.documents.length === 0) {
122-
const sentMsg = await msg.reply(errors.noResults(searchTerm));
123-
return delayedMessageAutoDeletion(sentMsg);
124-
}
125-
126-
let preparedDescription = json.documents.map(
127-
({ title, excerpt, slug }, index) =>
128-
createMarkdownListItem(
129-
index,
130-
createMarkdownLink(
131-
adjustTitleLength([`**${title}**`, excerpt].join(' - ')),
132-
buildDirectUrl(provider, slug)
133-
)
134-
)
135-
);
136-
137-
const expectedLength = preparedDescription.reduce(
138-
(sum, item) => sum + item.length,
139-
0
140-
);
141-
if (expectedLength + BASE_DESCRIPTION.length + 10 * '\n'.length > 2048) {
142-
preparedDescription = preparedDescription.map(string => {
143-
// split at markdown link ending
144-
const [title, ...rest] = string.split('...]');
145-
146-
// split title on title - excerpt glue
147-
// concat with rest
148-
// fix broken markdown link ending
149-
return [title.split(' - ')[0], rest.join('')].join(']');
150-
});
151-
}
152-
153-
const sentMsg = await msg.channel.send(
154-
createListEmbed({
155-
description: createDescription(preparedDescription),
156-
footerText: 'Powered by the search API',
157-
provider,
158-
searchTerm,
159-
url,
160-
})
161-
);
162-
163-
const result = await waitForChosenResult(sentMsg, msg, json.documents);
164-
if (!result) {
165-
return;
166-
}
167-
};
168-
169110
/**
170111
* Poor man's dependency injection without introducing classes, just use closures
171112
* and higher order functions instead. Also provides a default so we don't have

src/commands/mdn/updated.test.ts

Lines changed: 0 additions & 156 deletions
This file was deleted.

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import handleComposerQuery from './commands/composer';
77
import handleFormattingRequest from './commands/formatting';
88
import handleGithubQuery from './commands/github';
99
import handleJQueryCommand from './commands/jquery';
10-
import handleMDNQuery from './commands/mdn';
10+
import handleMDNQuery from './commands/mdn/dom';
1111
import handleNPMQuery from './commands/npm';
1212
import handlePHPQuery from './commands/php';
1313
import handleJobPostingRequest from './commands/post';

0 commit comments

Comments
 (0)