-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from danbilokha/feature/dbilokha/code-quality
code quality
- Loading branch information
Showing
50 changed files
with
693 additions
and
374 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
## Usage | ||
|
||
We use <a href='https://github.com/yagop/node-telegram-bot-api'>Node Telegram bot api</a> for Telegram. | ||
Here you can find <a href='https://github.com/yagop/node-telegram-bot-api/blob/release/doc/api.md'>API</a>. |
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
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
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
64 changes: 64 additions & 0 deletions
64
server/src/bots/telegram/services/registry/getParameterAfterCommandFromMessage.test.ts
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,64 @@ | ||
import { getParameterAfterCommandFromMessage } from './getParameterAfterCommandFromMessage'; | ||
import { | ||
Continents, | ||
CustomSubscriptions, | ||
UserMessages, | ||
UserRegExps, | ||
} from '../../../../models/constants'; | ||
|
||
const singleParameterAfterCommandMock = [ | ||
...Object.keys(UserMessages).filter( | ||
(key) => !isNaN(Number(UserMessages[key])) | ||
), | ||
...Object.keys(Continents).filter((key) => !isNaN(Number(Continents[key]))), | ||
...Object.keys(CustomSubscriptions).filter( | ||
(key) => !isNaN(Number(CustomSubscriptions[key])) | ||
), | ||
// Add countries by flag checkout | ||
]; | ||
|
||
const singleParameterAfterCommandBulkMock = [ | ||
...Object.keys(UserRegExps).map((v) => [UserRegExps[v]]), | ||
...Object.keys(Continents).map((v) => [Continents[v]]), | ||
]; | ||
|
||
describe('getParameterAfterCommandFromMessage', () => { | ||
it('should return undefined if unsupported command', () => { | ||
const expectation = getParameterAfterCommandFromMessage( | ||
singleParameterAfterCommandMock, | ||
'/CommandDoesnotExist' | ||
); | ||
|
||
expect(expectation).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if no arguments', () => { | ||
const expectation = getParameterAfterCommandFromMessage( | ||
singleParameterAfterCommandMock, | ||
'/country' | ||
); | ||
|
||
expect(expectation).toBeUndefined(); | ||
}); | ||
|
||
it('should return Ukraine', () => { | ||
const expectation = getParameterAfterCommandFromMessage( | ||
singleParameterAfterCommandMock, | ||
'/country Ukraine' | ||
); | ||
|
||
expect(expectation).toBe('Ukraine'); | ||
}); | ||
|
||
test.each(singleParameterAfterCommandBulkMock)( | ||
'.singleParameterAfterCommandBulkMock(%s)', | ||
(command) => { | ||
const expectation = getParameterAfterCommandFromMessage( | ||
singleParameterAfterCommandMock, | ||
`${command} PARAMETER` | ||
); | ||
|
||
expect(expectation).toBe('PARAMETER'); | ||
} | ||
); | ||
}); |
33 changes: 33 additions & 0 deletions
33
server/src/bots/telegram/services/registry/getParameterAfterCommandFromMessage.ts
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,33 @@ | ||
/** | ||
* Check out how it works here | ||
* https://codepen.io/belokha/pen/xxwOdWg?editors=0012 | ||
*/ | ||
export function getParameterAfterCommandFromMessage( | ||
singleParameterAfterCommands: Array<string>, | ||
userFullInput: string | undefined | ||
): string | undefined { | ||
const makeMagicOverUserFullInput: string = singleParameterAfterCommands.find( | ||
(parameter) => parameter === userFullInput | ||
) | ||
? userFullInput + ' ' // Problem is that userInput is the same as RegExp it returns null, but when it has | ||
: // at least one whitespace it is not null | ||
// https://codepen.io/belokha/pen/xxwOdWg?editors=0012, Example 5. | ||
userFullInput; | ||
|
||
const execResult = new RegExp( | ||
`(?<command>${singleParameterAfterCommands.join( | ||
'|\\' | ||
)})\\s(?<firstargument>.*)` | ||
).exec(makeMagicOverUserFullInput); | ||
if (!execResult) { | ||
return undefined; | ||
} | ||
|
||
/* tslint:disable:no-string-literal */ | ||
if (execResult.groups['command'] && !execResult.groups['firstargument']) { | ||
return undefined; | ||
} | ||
|
||
return execResult.groups['firstargument']; | ||
/* tslint:enable:no-string-literal */ | ||
} |
16 changes: 16 additions & 0 deletions
16
server/src/bots/telegram/services/registry/messageHandlerRegistry.test.ts
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,16 @@ | ||
import { MessageHandlerRegistry } from './messageHandlerRegistry'; | ||
|
||
describe('MessageHandlerRegistry', () => { | ||
let messageHandlerRegistry: MessageHandlerRegistry; | ||
const botMock: any = { | ||
sendMessage: jest.fn().mockReturnValue(Promise.resolve('message')), | ||
on: jest.fn(), | ||
}; | ||
beforeAll(() => { | ||
messageHandlerRegistry = new MessageHandlerRegistry(botMock); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(!!messageHandlerRegistry).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.