Skip to content

Commit e5f4ba5

Browse files
committed
Rename mdn strategies
1 parent a6a6ca7 commit e5f4ba5

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

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
@@ -10,7 +10,7 @@ import handleFormattingRequest from './commands/formatting';
1010
import handleGithubQuery from './commands/github';
1111
import handleJQueryCommand from './commands/jquery';
1212
import handleLeaderboardRequest from './commands/leaderboard';
13-
import handleMDNQuery from './commands/mdn';
13+
import handleMDNQuery from './commands/mdn/dom';
1414
import handleNPMQuery from './commands/npm';
1515
import handlePHPQuery from './commands/php';
1616
import handlePointsRequest from './commands/points';

0 commit comments

Comments
 (0)